SQL Server: Index columns used in like?

后端 未结 3 443
春和景丽
春和景丽 2020-11-28 09:28

Is it a good idea to index varchar columns only used in LIKE opertations? From what I can read from query analytics I get from the following query:

SELECT *          


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 09:33

    Given the data 'abcdefg'

    WHERE Column1 LIKE '%cde%'  --can't use an index
    
    WHERE Column1 LIKE 'abc%' --can use an index
    
    WHERE Column1 Like '%defg' --can't use an index, but see note below
    

    Note: If you have important queries that require '%defg', you could use a persistent computed column where you REVERSE() the column and then index it. Your can then query on:

    WHERE Column1Reverse Like REVERSE('defg')+'%' --can use the persistent computed column's index
    

提交回复
热议问题