首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql in not in where 混搭 效率低上

2013-01-08 
sqlinnot inwhere 混搭 效率低下一个 旧项目 出现的 能源问题:出现问题的 原句: select * from stock wher

sql in not in where 混搭 效率低下
 

  一个 旧项目 出现的 能源问题:

     出现问题的 原句:
 


select * from stock where  leechdomId 
   in
    (
      select leechdomId from collect 
         where id 
            not in
              (
              select collectId from quote 
                  where type=0 and date >'2010-06-01 10:00:02'
               ) and date>'2010-06-01 10:00:02'
     )  and date>'2010-06-01 10:00:02'


    in  嵌套 not in  加 三个where  有两个and   运行多次 一般需要 7秒;
-------------------------------------------

  in 里面的句子  

 select leechdomId from collect 
         where id 
        not in
              (
              select collectId from quote 
                  where type=0 and date >'2010-06-01 10:00:02'
               ) and date>'2010-06-01 10:00:02'

   也就是 在in 里 嵌套的句子同样需要 7秒
--------------------------------------
  not in  最深层的句子

   select collectId from quote 
                  where type=0 and date >'2010-06-01 10:00:02'

     运行 显示为 0秒
--------------------------------------
第二层  in 里面的句子 去掉一个条件
   去掉时间判断:

select leechdomId from collect 
         where id 
        not in
              (
              select collectId from quote 
                  where type=0 and date >'2010-06-01 10:00:02'
               ) 

  或者去掉 not in:

  select leechdomId from collect 
         where  date>'2010-06-01 10:00:02'



  时间都会显示为 0秒
------------------------------------

   综合以上的结果来看,in 并没有显现降低效率(费解),如果把第二层的 not in  和判断时间的语句 放在一起的话 会降低效率,去掉一个就不会产生效率问题(同样费解)。
  费解中。。。。 求指教,怎么样才能解决掉这种效率问题,还有今后怎么去避免这种问题,现在的数据是 大约是 3万条。。。。。。。。。
[解决办法]
id 和 date 是单独建的索引。有没有id,date的联合索引
[解决办法]
最好改成NOT EXISTS,
[解决办法]
set statistics profile on

把执行计划贴出来看看。
[解决办法]
引用:
引用:
最好改成NOT EXISTS,


 n-o-t- -e-x-i-s-t-s- -需-要-5-s .... -而-且-还-查-不-出-结-果-来-。-。。。

  买-火-车-票-去-了-。。。。 回-来-继-续-等-答-案-。。。。。


您的回复正文中有非法词或词组!
回复太快,请先休息一下


你怎么改的,
[解决办法]
还没到那个层面,看看学习下
[解决办法]
in , not in 查询很慢.

建议改为exists , not exists.
不过exists , not exists也比较慢,只是比in,not in稍好一些.

其实查询速度的快慢原则取决于你的需求,如果你的需求如此,则没有好的办法.

热点排行