比如我有某个字段 uid
他的值可能是     88
               99,88,100
               99,88,100,110,120,130,140
               等我现在要查询 该字段不包含 88 的值
语句为 where uid not like '%88%'请问有没有比这个 like 效率高点的语句字段里面都是 整数 每个整数用,分割

解决方案 »

  1.   

    select * from tb
    where charindex(',88,',uid)=0
      

  2.   

    写错……
    select * from tb
    where charindex(',88,',','+uid+',')=0
      

  3.   

    select * from tb
    where charindex(',88,',uid)=0效率真的比 like 高吗?
      

  4.   

    效率不一定比like高,如果 uid上建了索引,可能效率反而低了一些。
    但是 where uid not like '%88%' 是不正确的写法。
    反例:uid='12,188,222'
      

  5.   

    而且前后都有百分号 '% %'的like好像也没法用到索引。
      

  6.   

    SQL codeselect * from tb
    where ','+uid+',' not like '%,88,%'SQL codeselect * from tb
    where uid not like '%,88,%'
        and uid not like '88,%'
        and uid not like '%,88'select * from tb
    where charindex(',88,',','+uid+',')=0效率那个高?
      

  7.   

    应该是最后一种吧,理由是:
    1.由于'%%',没有索引优势
    2.NOT关键字会降低效率
      

  8.   

    charindex 和 like '%%' 效率差不多,都是全表扫描
    SQL codeselect * from tb
    where ','+uid+',' not like '%,88,%'
    SQL codeselect * from tb
    where uid not like '%,88,%'
        and uid not like '88,%'
        and uid not like '%,88'
    后者绝对不会比前者快
      

  9.   

    charindex 和 like '%%' 效率差不多,都是全表扫描
    SQL codeselect * from tb
    where ','+uid+',' not like '%,88,%'
    SQL codeselect * from tb
    where uid not like '%,88,%'
        and uid not like '88,%'
        and uid not like '%,88'
    后者绝对不会比前者快
      

  10.   

    首先,在uid有索引的前提下like的写法,SQL SERVER采用索引的可能性还是存在的呀,而且like  '???%'比你like '%???%'和like '%???'高效得多的
      

  11.   


    测试一下patindex
    select * from tb
    where patindex('%,88,%',','+uid+',')=0
    [/code]
      

  12.   

    如果UID有索引,在数据平均密度低,条件选择性良好的前提下,SQL server还是会选择index seek,如果你的查询条件不理想造成嵌套迭代那么的RID成本加大,可能反而是负担了,在选择索引扫描后如果你表是还有聚集的话并且碎片又很差时不如让查询放弃索引扫描了个人观点:在UID上有索引,LIKE还是比charindex高效的
      

  13.   

    看海的总结 :
    http://blog.csdn.net/Haiwer/archive/2008/08/25/2826881.aspxA、 不要对索引字段进行运算,而要想办法做变换
    B、 不要对索引字段进行格式转换
    C、 不要对索引字段使用函数
      

  14.   

    学习了一下。
    那么比较7楼的3种方法:
    第一种显然不可取,在uid上进行了运算,并有not,'%%'这些符号,肯定用不到索引。
    第二种,存在'%,88,%'这种条件。网上说这种形式会显式阻止使用索引……
      

  15.   

    谢谢大家 讨论  给均分吧!!!
    另:SQL codeselect * from tb
    where uid not like '%,88,%'
        and uid not like '88,%'
        and uid not like '%,88'
    是不对的 
    应该是
    SQL codeselect * from tb
    where uid not like '%,88,%'
        and uid not like '88,%'
        and uid not like '%,88'
        and uid not like '88'
    charindex(',88,',','+uid+',')=0这个很牛