现有一字段 内容如下child_id
10,14
11,
11,13,14,15
22,23
11,12,14,15,17,18,22
22,24针对它的查询:
select *  from 表名  where  ','+child_id+','   like ',%'+ CAST(@CountryID AS VARCHAR) +',%' )要求:
表记录较多,30万条左右还在快速增长
表在别的系统中被调用,表的结构不能动(也不能在表的字段前后加逗号)
问题:如何利用到child_id这个字段的索引。以改进查询性能?

解决方案 »

  1.   

    你这个根本都走不到 SCAN。。因为你用了LIKE %% 始终是全表扫描
      

  2.   

    select *  from 表名 with(index=索引名)  where  ','+child_id+','   like ',%'+ CAST(@CountryID AS VARCHAR) +',%' )
      

  3.   

    select * ,','+child_id+',' as id into #t from 表名 
    create index idx ..(id)select * from #t  where  id  like ',%'+ CAST(@CountryID AS VARCHAR) +',%' )表结构不清楚,这样顶多也就索引扫描,
      

  4.   

    like ',%'+改為
    like '%,'+
      

  5.   

    select *  from 表名  with(index=索引名)  where ( child_id   like  CAST(@CountryID AS VARCHAR) +',%' )
    ) or  ( child_id   like  ',%'+CAST(@CountryID AS VARCHAR))
    or  ( child_id   like  ',%'CAST(@CountryID AS VARCHAR)+'%,')
    )