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

求帮忙优化SQL
select   distinct   a.file_name   as   bqyname,a.unique_id   as   menuid   from   briocat2   a,bqy_roleright   b,portal_userroles   c   where  

c.userid=@userId   and   b.role_id=c.roleid   and   a.unique_id=b.resource_id   and   a.parent_id   is   not   null   and   a.unique_id   not   in   (select   aa.menuid   from  

portal_indivdetail   aa,briocat2   bb   where   aa.userid=@userId   and   aa.menuid=bb.unique_id   and   aa.tabid=2   and   bb.parent_id   is   not   null)

这个SQL中,各个表中记录都很少,最多一个表不过3000条左右,因为数据少所有都没有建索引。但是在这个查询中速度却慢得奇怪,竟然需要30~40秒才能查出来。请问大家有何解决办法?谢谢。

------解决方案--------------------
坐关联,建索引,尽量不用not语句!