select top 10 showname,company from info where info_id in (select max(info_id) as info_id from info group by company ) and flag=1 order by dateandtime desc
...全文
1036打赏收藏
优化查询时间。
info表有8w条信息,最新通过审核(flag=1)的求购信息。。info_id 为自动增加字段,dateandtime为添加时间。格式:2008-08-08 24:00:00 select top 10 showname,company from info where info_id in (select max(info_id) as info_id from info group by company ) and flag=1 order by dateandtime desc
少了一个表前缀,完整测试:
select top 10 info.showname,info.company from info,(select company,max(info_id) as info_id from info group by company) as Temp
where info.info_id= Temp.info_id and info.company = Temp.company order by dateandtime desc
用这个:这个语句表面逻辑上是扫描表两次,但是实际上优化器处理后只扫描了一次。
select top 10 showname,company from info,(select company,max(info_id) as info_id from info group by company) as Temp
where info.info_id= Temp.info_id and info.company = Temp.company order by dateandtime desc
--估计差不多
select top 10 t.showname,t.company from info t where flag = 1 and info_id = (select max(info_id) as info_id from info where company = t.company) order by dateandtime desc