日期:2014-05-18  浏览次数:20530 次

请大家帮忙优化一条sql语句,合并查询


COMP表里存着所有用户的注册信息
COMP_PIC表里存着所有用户的展示图片

我现在想把COMP_PIC表里TID值最高的那张图片调出来做LOGO

select   ID,NID,SID,SNAME,(select   top   1   PICFILE   from   COMP_PIC   where   SID=COMP.ID   order   by   TID   desc)   as   LOGO   from   COMP   where   VID> 0   and   VID <6   order   by   YID   desc

效果能实现,可是速度挺慢的,能在不改变现有数据库结构的情况下优化一下嘛?

------解决方案--------------------
在COMP上VID,YID comp_pic的TID列上建索引试试吧。
------解决方案--------------------
select m.* , n.* from COMP m,
(select a.* from comp_pic a,
(select sid , max(tid) tid from COMP_PIC) b
where a.sid = b.sid and a.tid = b.tid) n
where m.id = n.sid