大概有10万条数据。查出id=1或者id=2或者......我写的是 select * from A where id in (1,2,......)这样貌似很慢,朋友说用EXISTS 应该怎么写啊?请大神帮忙写个sql语句

解决方案 »

  1.   

    就看你的 in (1,2,......)这个数据是什么来的?
    如果是在一个table 里面
    select *
    from ta a
    where exists(select 1 from tb b where a.id=b.id)如果是个字符串
    select * 
    from tb 
    where charindex(','+cast(id as varchar)+',',',1,2,3,4,5,...')>0
      

  2.   

    先确认条件字段有索引
    再把条件写入一个临时表,如#t然后:
    select * from A where exists(select 1 from #t where a.id=临时表字段名)
    或:
    select a.*
    from A 
      join #t b 
        on a.ID=b.临时表字段名
      

  3.   

    如果是连续的话 用between and