以前一直用in  也没学过sql什么的就是照着用,网上说exists效率高想改改,但是思想就是变不过去,想大家来个例子。例如select * from table_1 where id in(‘1’,‘3’,‘5’)上面的如何改写 希望能帮忙写下

解决方案 »

  1.   

    这个例子还是用in好了
    数据集比较大时用exists效率比较高
    比如a表和b表,b表较大
    select * from a
    where exists(select 1 from b where id=a.id)
    性能要比
    select * from a
    where id in (select id from b)
    好exists验证存在性,会走索引。而in要对后面的表进行全表扫描
      

  2.   

    我只是举个例子  实际上table_1是有50万条左右 而后面的in中有1000个左右  用哪种会效率高些 或者有什么别的好的方法  谢谢 大家
      

  3.   

    我是希望exists改写一下 看看哪个快一些  如何写呢 
      

  4.   

    select * from table_1
    where exists(select 1 from table_1 where id = 1 or id = 2 or id =3);
      

  5.   

    如果你的表很多,要有索引,使用exists肯定快
      

  6.   

    我要找的有1000多个   那我就要where exists(select 1 from table_1 where id = 1 or id = 2 or id =3。。);吗?
      

  7.   

    EXISTS和IN、IN和'='、NOT IN和NOT EXISTS
    楼主可以参考下这篇文章http://blog.csdn.net/wh62592855/archive/2009/11/13/4806741.aspx