PLEASE REVIEW THIS POST FOR THE UPDATED SCRIPTS: USE THESE: Updates to SQLskills Index Procedures
SQL Server has a horrible habit of letting bad habits move forward (for the sake of backward compatibility [e.g. autoshrink]). And, I do understand *some* of the reasons for allowing some of these “features/options” to move forward. However, there are a few that frustrate me and I’ve talked about this one quite often.
SQL Server lets you create completely redundant and totally duplicate indexes.
Yes, you can even create the same index 999 times! Try it:
<br />SET NOCOUNT ON<br />GO</p><p>CREATE TABLE TestTable<br />(<br />col1 int identity<br />);<br />GO</p><p>DECLARE @IndexID smallint,<br />@ExecStr nvarchar(500);</p><p>SELECT @IndexID = 1;</p><p>WHILE @IndexID <= 999<br />BEGIN<br />SELECT @ExecStr = 'CREATE INDEX [Test' + right('00' + convert(varchar(5), @IndexID), 3) + '] ON TestTable (col1)';</p><p>EXEC(@ExecStr);</p><p>SELECT @IndexID = @IndexID + 1;<br />END<br />GO</p><p>SELECT count(*)<br />FROM sys.indexes<br />WHERE object_id = object_id('TestTable');<br />GO</p><p>--DROP TABLE TestTable<br />GO<br />
For somewhat obvious reasons having 999 duplicate indexes does seem completely useless… so, why does SQL Server allow it at all? And, what’s the backward compatibility argument? Well, I’ve written about that before on our SQL Server Magazine Q&A blog (Kimberly & Paul – Questions Answered): Why SQL Server Lets You Create Redundant Indexes.
Having said that – and, for those of you that are only checking YOUR code, you don’t use index hints and/or you don’t care about backward compat – then, you can use my proc to help you find duplicate indexes (and drop them)! It’s all based on my “sp_helpindex rewrites” stored procedures but I had to do some “tweaking” to get the procedures to understand that the order of the columns in the INCLUDE list are irrelevant (in terms of order). So, you can’t use the sps as written. They give you the exact (and correct) structure – and, that’s exactly how SQL Server stores them. For example, the following two indexes have a different structure. However, they are completely redundant and you do NOT need both.
<br />CREATE INDEX Test1 ON dbo.Member (LastName)<br />INCLUDE (FirstName, MiddleInitial);</p><p>CREATE INDEX Test2 ON Member (LastName)<br />INCLUDE (MiddleInitial, FirstName);<br />GO<br />
Here’s what you need:
- First, my modified sp_ that exposes all of the columns in all levels of the index. This one does NOT replace the one needed by sp_SQLskills_SQL2008_helpindex. You need both. This version is used to reorder the INCLUDEd columns so that the “finddupes” proc can remove indexes that are different solely because of the order of the columns in the INCLUDE clause (which is irrelevant for navigation/index usage).
Install this one first:
20110715_sp_SQLskills_ExposeColsInIndexLevels_INCLUDE_UNORDERED.sql (6.95 kb)
- Second, my modifed sp_helpindex that is used SOLELY by finddupes. Basically, it’s just modified to use the UNORDERED version of “exposing the columns” in the nonclustered indexes. Again, this does NOT replace the other sp – you need both.
Install this one second:
20110715_sp_SQLskills_SQL2008_finddupes_helpindex.sql (10.95 kb)
- Finally, here’s the “finddupes” procedure
Install this one last:
20110720_sp_SQLskills_SQL2008_finddupes.sql (5.30 kb)
- NOTES: At some point in the not-too-distant future I’ll modify this for the following:
1) One set of code that will work for 2005/2008
2) I’ll combine the two versions of the “expose columns in index levels” so that there’s only one.
3) I’ll make this work for indexed views
Finally, how can you use this:
To find the duplicates for just one table – you can use one-part or two-part naming:
<br />USE [AdventureWorks2008];<br />GO</p><p>EXECUTE sp_SQLskills_SQL2008_finddupes @ObjName='Production.Document';<br />GO<br />
OR… to find all duplicates in the entire database:
<br />USE [AdventureWorks2008];<br />GO</p><p>EXECUTE sp_SQLskills_SQL2008_finddupes;<br />GO<br />
OR… if you’re really motivated, you can run it for all databases – but, before you do – see Aaron Bertrand’s comment to this post for his replacement for sp_msforeachdb and use that instead!!
<br />EXECUTE sp_msforeachdb 'USE ?; exec sp_SQLskills_SQL2008_finddupes';<br />GO<br />
Have fun and let me know how many duplicates you find?
I found one in AdventureWorks2008 and a few in Northwind. I suspect you’ll find a few! This isn’t something that will find tons of indexes (to drop) but if there are even a couple of dupes (especially on large tables) you’ll have savings in database modification statements, logging, caching, maintenance, storage (and therefore backups, etc.) and this is why dupes are HORRIBLE! I do wish that SQL Server had an option to prevent their being created! Maybe someday?!