SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding and Eliminating Duplicate or Overlapping Indexes


Finding and Eliminating Duplicate or Overlapping Indexes

Author
Message
Ed Pollack
Ed Pollack
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1096 Visits: 513
Comments posted to this topic are about the item Finding and Eliminating Duplicate or Overlapping Indexes
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7371 Visits: 3395
Thanks for the Article.
I voted it with 5 stars.
But there's only a hole:
Your query doesn't consider the order on columns.
Example:
create index idx_MyTab on MyTab(col1,col2)
and
create index idx_MyTab_1 on MyTab(col1 DESC,col2)


So, in the case above, it should be listed with a "warning".

I run on tuttopodismo
grzegorz.mozejko
grzegorz.mozejko
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 25
very usefull, thx
a small correction, when name of first index column is begining part of second index column (add comma):



AND ( DUPE1.key_column_list+',' LIKE LEFT(DUPE2.key_column_list,
LEN(DUPE1.key_column_list)+1)
OR DUPE2.key_column_list+',' LIKE LEFT(DUPE1.key_column_list,
LEN(DUPE2.key_column_list)+1)
OR DUPE2.key_column_list=DUPE1.key_column_list
)
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7371 Visits: 3395
Replace:
STUFF((SELECT  ', ' + COLUMN_DATA_KEY_COLS.name


with:
STUFF((SELECT  ', ' + COLUMN_DATA_KEY_COLS.name + ' ' +SUBSTRING('+-',INDEX_COLUMN_DATA_KEY_COLS.is_descending_key+1,1)



I run on tuttopodismo
grzegorz.mozejko
grzegorz.mozejko
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 25
grzegorz.mozejko (6/16/2014)
very usefull, thx
a small correction, when name of first index column is begining part of second index column (add comma):



AND ( DUPE1.key_column_list+',' LIKE LEFT(DUPE2.key_column_list,
LEN(DUPE1.key_column_list)+1)
OR DUPE2.key_column_list+',' LIKE LEFT(DUPE1.key_column_list,
LEN(DUPE2.key_column_list)+1)
OR DUPE2.key_column_list=DUPE1.key_column_list
)


better
AND ( DUPE1.key_column_list+',' LIKE LEFT(DUPE2.key_column_list+',',
LEN(DUPE1.key_column_list)+1)
OR DUPE2.key_column_list+',' LIKE LEFT(DUPE1.key_column_list+',',
LEN(DUPE2.key_column_list)+1)
)

I'm looking now for solution for column names with '_'
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7371 Visits: 3395
grzegorz.mozejko (6/16/2014)
grzegorz.mozejko (6/16/2014)
very usefull, thx
a small correction, when name of first index column is begining part of second index column (add comma):



AND ( DUPE1.key_column_list+',' LIKE LEFT(DUPE2.key_column_list,
LEN(DUPE1.key_column_list)+1)
OR DUPE2.key_column_list+',' LIKE LEFT(DUPE1.key_column_list,
LEN(DUPE2.key_column_list)+1)
OR DUPE2.key_column_list=DUPE1.key_column_list
)


better
AND ( DUPE1.key_column_list+',' LIKE LEFT(DUPE2.key_column_list+',',
LEN(DUPE1.key_column_list)+1)
OR DUPE2.key_column_list+',' LIKE LEFT(DUPE1.key_column_list+',',
LEN(DUPE2.key_column_list)+1)
)

I'm looking now for solution for column names with '_'

Replace(LEFT(DUPE2.key_column_list+',',LEN(DUPE1.key_column_list)+1),'_','[_]')



I run on tuttopodismo
grzegorz.mozejko
grzegorz.mozejko
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 25
Your correction: SUBSTRING('+-',INDEX_COLUMN_DATA_KEY_COLS.is_descending_key+1,1)
eliminates both problems I found

thx
Ed Pollack
Ed Pollack
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1096 Visits: 513
Both are good points! In my effort to keep this "simple" I left out a few use-cases, which you both have covered. I'll modify the article shortly to take them into account (once I've tested and verified there's nothing else missing here).

There's definitely no harm in completeness!

EDIT: New version published. While I can't include every single possible use-case for index properties, these additions provide good examples of how to customize these scripts & ideas to specific situations.
grzegorz.mozejko
grzegorz.mozejko
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 25
Is it a good idea to add column "is_disabled" from sys.indexes to row set ?
Ed Pollack
Ed Pollack
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1096 Visits: 513
The is_disabled flag could be worked in, but I would venture that a disabled index and an enabled index that are identical should be flagged as dupes.

For just display purposes at the end, sure---no harm in adding it, and it would help troubleshoot the oddball case where we would rather drop a duplicate that is disabled, rather than its enabled counterpart.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search