November 9, 2017 at 1:25 am
Dear Experts,
Have a question about duplicate indexes . Is an Index considered duplicate if the two column keys (Index A with col1+col2) it has are in reverse order of the same two columns used in another index (Index B with col2+col1) ? Thank you.
November 9, 2017 at 1:36 am
No they are not duplicates as the key order is different.
Key order is important as its the way the optimizer searches the index
So IndexA (Col1,Col2), IndexB (Col2,Col1) are not considered duplicated
But IndexA (Col1, Col2), IndexB (Col1, Col2, Col3) then IndexA would be considered the duplicate
November 9, 2017 at 6:43 am
anthony.green - Thursday, November 9, 2017 1:36 AMNo they are not duplicates as the key order is different.Key order is important as its the way the optimizer searches the index
So IndexA (Col1,Col2), IndexB (Col2,Col1) are not considered duplicated
But IndexA (Col1, Col2), IndexB (Col1, Col2, Col3) then IndexA would be considered the duplicate
Thank you so much Anthony. Very helpful.
November 9, 2017 at 6:43 am
anthony.green - Thursday, November 9, 2017 1:36 AMNo they are not duplicates as the key order is different.Key order is important as its the way the optimizer searches the index
So IndexA (Col1,Col2), IndexB (Col2,Col1) are not considered duplicated
But IndexA (Col1, Col2), IndexB (Col1, Col2, Col3) then IndexA would be considered the duplicate
I'd say a potential duplicate. Everything say is 100% correct, but it's possible that, depending on the query, IndexB is more useful than IndexA in a given situation because we're filtering on Col1-Col3, not just on Col1 & Col2. That also goes the other way. The first index is smaller than the second, so may be more useful situationally. All that even though, within a pure definition, IndexA is duplicating what IndexB does in your second example. That's why this all gets so hard.
Another point on duplicates is that the first column is the one used to create the histogram for the index, which is one of the primary (but not the only) drivers for the optimizer to determine which index is useful. In that case, depending on your queries, one of those two indexes may never get used, even though it's the better index for a given situation.
Ain't SQL Server fun.
"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
November 9, 2017 at 7:35 am
Grant Fritchey - Thursday, November 9, 2017 6:43 AManthony.green - Thursday, November 9, 2017 1:36 AMNo they are not duplicates as the key order is different.Key order is important as its the way the optimizer searches the index
So IndexA (Col1,Col2), IndexB (Col2,Col1) are not considered duplicated
But IndexA (Col1, Col2), IndexB (Col1, Col2, Col3) then IndexA would be considered the duplicate
I'd say a potential duplicate. Everything say is 100% correct, but it's possible that, depending on the query, IndexB is more useful than IndexA in a given situation because we're filtering on Col1-Col3, not just on Col1 & Col2. That also goes the other way. The first index is smaller than the second, so may be more useful situationally. All that even though, within a pure definition, IndexA is duplicating what IndexB does in your second example. That's why this all gets so hard.
Another point on duplicates is that the first column is the one used to create the histogram for the index, which is one of the primary (but not the only) drivers for the optimizer to determine which index is useful. In that case, depending on your queries, one of those two indexes may never get used, even though it's the better index for a given situation.
Ain't SQL Server fun.
Adding a little bit more.
The reversed order column indexed might be duplicates if you always query by both columns or just one of them but never the other by itself. Although, technically, they're not duplicates, one becomes redundant and might never be used.
November 9, 2017 at 7:41 am
By the same token, there is a type of duplicate index that can provide huge performance increases. They usually (but not always) are considered to be
"Covering Indexes". You can have the "perfect" Clustered Index for your query but, unless you're using a huge number of columns from a very wide table in your query, a much narrower Non-Clustered Index with the exact same key as the Clustered Index and the correct INCLUDES (think of it as being very similar to a much smaller Clustered Index) can make the queries that use the Non-Clustered Index absolutely fly compared to when the query uses the Clustered Index simply because of the greatly reduced page count thanks to more rows per page in the Non_Clustered Index.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply