select * from table1 where id in(1,2,3,4,5,6,7,8,9)
怎么才能提高效率呢?不用in?不会,求指教。

解决方案 »

  1.   

    --id 列上有索引的情况下select * from table1 where id = 1
    union all
    select * from table1 where id = 2
    union all
    select * from table1 where id = 3
    union all
    select * from table1 where id = 4
    union all
    select * from table1 where id = 5
    union all
    select * from table1 where id = 6
    union all
    select * from table1 where id = 7
    union all
    select * from table1 where id = 8
    union all
    select * from table1 where id = 9
      

  2.   


    或者创建带索引的表,存放in里面的数据。
    然后与table1 inner Join 也可以。 inner join 相关字段加好索引
      

  3.   

    还是另外建立一个码表 然后JOIN比较快。
      

  4.   

    得看数据发布的情况,如table1总记录数10万,id in(1,2,3,4,5,6,7,8,9)的记录有9.9万.
    此时即使id上有索引也是没用的,基本上无法优化.
      

  5.   

    3,4楼说的都对。建议你按照三楼的做法处理。join是比较快的
      

  6.   

     
    select * from #temp where   
     exists(
          select NULL from(
    select 1 as ID UNION 
    select 2 as ID UNION 
    select 3 as ID UNION 
    select 4 as ID UNION 
    select 5 as ID UNION 
    select 6 as ID UNION 
    select 7 as ID UNION 
    select 8 as ID UNION 
    select 9) a where a.ID=#temp.ID
       )
      

  7.   

     
    select * from #temp where   
     exists(
          select NULL from(
    select 1 as ID UNION 
    select 2 as ID UNION 
    select 3 as ID UNION 
    select 4 as ID UNION 
    select 5 as ID UNION 
    select 6 as ID UNION 
    select 7 as ID UNION 
    select 8 as ID UNION 
    select 9) a where a.ID=#temp.ID
       )
      

  8.   

    select * from table1 where id <10
      

  9.   

    楼主让我想起了 大于 小于  between ... and 以及 join 的各种神马连接。
      

  10.   

    select * from table1 where id = 1 or id = 2 or id =3 
    这样效率会高很多
      

  11.   

    改join效果可能好点.BETWEEN 能用起来当然不错。