sql - Index on a column with like and where pattern? -


i've came conclusion in below cases seek/scan didn't why scan in 1st case , seek in 2nd case. understood 3rd case.

select c.contactname sales.customers c c.contactname '%a'-- scan 1st case  select c.contactname sales.customers c c.contactname 'a%'-- seek 2nd case  select c.contactname sales.customers c c.contactname '%a%'-- scan 

if build index on contactname has below sample data how index tree.. if build numbers compare less greater , traversing how index tree traverse in below case.

c.contactname  mark anna krishna nadejda allen bob cab 

this is, indeed, way sql server works. asking why.

think index. functionally, can think of storing contactname in alphabetical order -- dictionary or telephone book. (yes, more complicated data structure, typically b-tree, result items in order).

when contactname 'a%', query optimizer knows needs @ entries start letter "a". index knows are, optimizer can use seek them. sql server implements optimization like (not databases this).

when contactname '%a', saying "find me entries end in 'a'". lot looking through dictionary words end in "a". ordering not of help. there might entries start "a" , end "a'. there might entries start "z" , end "a". so, these types of expressions require scan instead of seek.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -