February 11, 2014 at 7:42 am
Hello,
If I create an index
CREATE INDEX IX_myindex1 ON mytable (ColumnA, ColumnB);
Do I/should I need to create a second index?
CREATE INDEX IX_myindex2 ON mytable (ColumnA);
Thank you,
djj
February 11, 2014 at 7:47 am
no; since the leading edge of the first index has the same column, you don't need another index;
if there are WHERE statments on ColumnB only, then an idnex on just that column might make sense, but it depends on how unique the column is as to whether the optimizer might use the index vs just scannign the table.
Lowell
February 11, 2014 at 7:49 am
It depends.
What do you plan these indexes will be used for? Are you going to use these columns in JOINS, sometimes one sometimes two? SELECTS?
There is nothing wrong in having both indexes if you really need them.
February 11, 2014 at 7:49 am
Thank you. I had thought that was the case, thus the question.
February 11, 2014 at 9:04 am
It's extremely unlikely that the second index will get used. It's possible it will get used if the optimizer determines that it needs to do a scan of the entire index only because the index will be a little smaller since it only has a single key. But, that seems a little bit of an edge case since the histogram within the statistics for both indexes will be identical.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 11, 2014 at 9:19 am
please note, the the following is a bit different setup...
CREATE INDEX IX_myindex1 ON mytable (ColumnA, ColumnB);
CREATE INDEX IX_myindex2 ON mytable (ColumnB);
As, Grant mentioned, the Index(A,B) and Index(A) will have identical histogram in the statistics.
Index(A,B) and Index(B) pair is a more common scenario, as it gives quite different picture.
February 11, 2014 at 9:26 am
Thank you all.
I knew that ColumnB would need its own if it required an index, I just wanted clarification on the first column in an index list. But it bears repeating for those who may not know.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply