朋友们,T-sql语法查询是,like与in,哪个查询方法效率最高?
望达人给予指导。

解决方案 »

  1.   

    like 和 in的功能是不一样的,like是支持通配符的。
    举重都是分不同级别的,like和in不是同一个级别的。
    create table test0824(name varchar(4) primary key)
    insert into test0824 
    select '张三' union all
    select '张四' union all
    select '张五' union all
    select '李三' union all
    select '李四' union all
    select '李五'select * from test0824 where name in('张三','张四','张五')
    select * from test0824 where name like '张%'数据量小的时候,开销各占50%。不同的数据量,不同的索引,like不同的通配符效果不同。
      

  2.   

    关键,like和in很难写出功能完全相同的语句,
    如果有功能完全相同的,只能具体来分析了一般来说,in是精确的,利用索引更高效
      

  3.   

    create table test082401 (col varchar(8))
    insert into test082401
    select a.col*1000+b.col*100+c.col*10+d.col+1 as col
    from 
    (select 0 as col union all select 1 union all select 2 union all 
    select 3 union all select 4 union all select 5 union all 
    select 6 union all select 7 union all select 8 union all select 9)a
    cross join 
    (select 0 as col union all select 1 union all select 2 union all 
    select 3 union all select 4 union all select 5 union all 
    select 6 union all select 7 union all select 8 union all select 9)b
    cross join 
    (select 0 as col union all select 1 union all select 2 union all 
    select 3 union all select 4 union all select 5 union all 
    select 6 union all select 7 union all select 8 union all select 9)c
    cross join 
    (select 0 as col union all select 1 union all select 2 union all 
    select 3 union all select 4 union all select 5 union all 
    select 6 union all select 7 union all select 8 union all select 9)d
    order by colselect * from (
    select a.col as acol,b.col as bcol from test082401 a cross join
    test082401 b  )aa
    where bcol in ('1000','2000','3000','4000','5000','6000','7000','8000','9000')select * from (
    select a.col as acol,b.col as bcol from test082401 a cross join
    test082401 b  )aa
    where bcol like '_000'
    用10000数据和10000数据进行笛卡尔积后加where条件,查看执行计划。
      

  4.   


    --可以开启这些统计开关,分别下语句测试分析结果
    DBCC DROPCLEANBUFFERS
    go
    --从过程高速缓存中删除所有元素(执行计划等等)。
    DBCC   FREEPROCCACHE 
    go
    --针对即席查询显示查询执行的配置文件
    set statistics profile on
    --开启即席查询生成的磁盘活动量的信息显示
    set statistics io on
    --开启即席查询语句的时间统计信息显示
    set statistics time on 
      

  5.   

    具体情况具体分析 直接判定谁高谁低是不行的
    有时候 in用不了索引  比如 select * from a where id not in(select id from b where id=xxx)
    有时候 like利用不了索引  select * from tb where col like '%aa%'