Wednesday, March 21, 2012

Index on Portion of a Field

Is it possible to index a portion of a field. I need to search a large table and look for the left 4 characters in two fields.

1) Does a 'normal' index on the entire fields get used in a search like this?
2) Can you build an index on just the left 4?
3) I could use an indexed view, but the table wasn't built with the right settings and I don't want to rebuild and repopulate the table if I don't have to

Thanks for the insight.

PeteNo, you cannot index a portion of a field.

If you did a search like " WHERE field LIKE 'abcd%' and field2 LIKE 'efgh%' " then I believe you will find that SQL Server will use the index (use Query Analyzer to make sure). SQL Server changes these kinds queries into SEARCHABLE ARGUMENTS (SARGS) that can use indexes (note that this is NOT true of things like " LIKE '%abcd%' )|||If this is something that you really need (indexing on only a prtion of a filed), you could create a computed field that just contains the portion you want to index on.

No comments:

Post a Comment