Home Forums SQL Server 2008 T-SQL (SS2K8) how to find duplicate indexes in all the tables in a database RE: how to find duplicate indexes in all the tables in a database

  • PiMané (9/17/2012)


    Hi,

    The query doesn't differentiate a "normal" index column from an "include" index column...

    In following statements idx_dupIndexes_03 and idx_dupIndexes_06 are considered duplicate but they're not.

    CREATE TABLE dupIndexes (id1 INT, id2 INT, id3 INT, id4 INT)

    CREATE INDEX idx_dupIndexes_01 ON dupIndexes (id1, id2)

    CREATE INDEX idx_dupIndexes_02 ON dupIndexes (id2, id1)

    CREATE INDEX idx_dupIndexes_03 ON dupIndexes (id2, id1) INCLUDE (id3)

    CREATE INDEX idx_dupIndexes_04 ON dupIndexes (id2, id1) INCLUDE (id3, id4)

    CREATE INDEX idx_dupIndexes_05 ON dupIndexes (id2, id1) INCLUDE (id4, id3)

    CREATE INDEX idx_dupIndexes_06 ON dupIndexes (id2, id1, id3)

    One more question, and in this one I might be saying nonsense...

    In the following statements:

    CREATE TABLE dupIndexes2 (id1 INT, id2 INT, id3 INT, id4 INT)

    CREATE INDEX idx_dupIndexes2_01 ON dupIndexes2 (id1, id2)

    CREATE INDEX idx_dupIndexes2_02 ON dupIndexes2 (id1, id2, id3)

    should both indexes be considered equal since 02 has all the columns 01 has?

    When I run "SELECT id1 FROM dupIndexes2 WHERE id1 = 1 AND id2 = 2" the sql uses idx_dupIndexes2_02 (according to the execution plan).

    Thks,

    Pedro

    Perhaps I should have noted that the query I provided was just something I threw together.

    The two points you mention are probably valid for consideration. I wouldn't want to use the results of the query to simply DROP indexes that the script says are duplicates. A little analysis should be performed on the results to decide what is appropriate to DROP and what not.

    Having said that though, the INCLUDEs can probably be resolved. An interesting point that I may look into, as I happen to need this for something else anyway.

    I believe in the second case, the two indexes would not be considered duplicates (by my script). I believe I would call that a case of "overlapping indexes" rather than identical ones. A case that could probably be handled if needed.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St