Is it a duplicate Index?

  • 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.

  • 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

  • anthony.green - Thursday, November 9, 2017 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

    Thank you so much Anthony. Very helpful.

  • anthony.green - Thursday, November 9, 2017 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

    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

  • Grant Fritchey - Thursday, November 9, 2017 6:43 AM

    anthony.green - Thursday, November 9, 2017 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

    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.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply