日期:2014-05-16  浏览次数:20785 次

这个SQL查询很慢,求优化,谢谢啊
有一张表,里面数据是9000+,字段很少

id
typeid  
hid(里面有很多重复数据,此字段类型为int)
size  
path
uploadtime  
descrip  
creator 


select path,uploadtime from pw_house_image where id in(SELECT MAX(id) AS id FROM pw_house_image GROUP BY hid) order by uploadtime desc limit 0,10
就上面的sql,一执行,就不懂了,过了30秒,mysql查询超时就停止了

------解决方案--------------------
改成分部执行
1
create table tmp_tb SELECT MAX(id) AS id FROM pw_house_image GROUP BY hid) order by uploadtime desc limit 0,10;
2
select path,uploadtime from pw_house_image from pw_house_image A,tmp_tb B where A.id = B.id;
3
drop table tmp_tb;
------解决方案--------------------
SQL code
select path,uploadtime 
from pw_house_image a ,(SELECT MAX(id) AS id FROM pw_house_image GROUP BY hid) b
where a.id=b.id
order by uploadtime desc 
limit 0,10