一个20W条数据的表,查询条件语句
哪个快一点.
1.where id in (1,2,3,4) 
2.where id=1 or id=2 or id=3 or id=4 

解决方案 »

  1.   

    select * from tb where id=1
    union all
    select * from tb where id=2
    union all
    select * from tb where id=3
    union all
    select * from tb where id=4
      

  2.   

    看数据库分布情况了,in可以用上索引,如果过滤的比较多的话,可能会快一点。==============================================================
    国产数据库第一品牌:http://bbs.dameng.com
    ==============================================================
      

  3.   

    三种求法我都测试一下
    环境 SQL 2008
    每次运行完SQL语句后都重启SQL服务
    测试的数据行数为698945行,大概有50个字段,查询的字段没有建索引
    第一种方法
    where id in (1,2,3,4)  用时51秒 71951 行受影响
    第二种方法
    where id=1 or id=2 or id=3 or id=4   用时56秒 71951 行受影响
    第三种方法
    select * from tb where id=1
    union all
    select * from tb where id=2
    union all
    select * from tb where id=3
    union all
    select * from tb where id=4  用时1分10秒 71951 行受影响
    第一,二种方法的速度差不多
    第三种方法的速度要慢一些
      

  4.   

    ID列上建立索引的话,IN(1,2,3,4)应该快些。
      

  5.   

    理论上来说是一样快的。因为in操作可以等价与or操作。