• 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