Thursday, September 16, 2010

One wide index or multiple narrow indexes?

Or “If one index is good, surely many indexes (indexes? indices? indi?) will be better”
This is a question that comes up very often on the forums. Something along the lines of:
I have a query with multiple where clause conditions on a table. Should I create one index for each condition, or one index with all the columns in it?
The question basically boils down to this: Which is more optimal and more likely for the optimiser to pick, a single seek operation against a wide index that seeks on all three conditions in one go, or three seek operations against three indexes followed by a join to get back the final set of rows.
One thing to keep in mind is that one of the jobs of an index is to reduce the number of rows in consideration for a query as early as possible in the query’s execution.
So let’s take a made-up example. Let’s say we have a table with a number of columns in it. A query is run against that table with three conditions in the where clause
WHERE ColA = @A AND ColB = @B AND ColC = @C
Read more: SQL in the Wild