Technical Article

Identify Obsolete Indexes

,

Often times as an application sits in a production environment for many years, through hundreds of releases, indexes that were once created for a particular query or process are no longer used or maybe the Stored Procedure that calls them are using a different query as a result of a release. Maintaining indexes takes resources on the server, as well as space on the disk. My thoughts are why waste resources of our index maintenance plan rebuilding and updating statistics on indexes that are no longer being accessed. I came up with a query that lists candidates to be dropped based on the DMV's for indexes no longer being used. I filtered it to only nonclustered that are not Primary Keys and are not Unique Indexes. Remember this is based off of DMV's so if your SQL instance is restarted often, you could potentially drop something used in a monthly or weeky process. This is primarily geared towards servers that are up most of the time

If you run this script in a particular database, it will generate the drop statements.

This will help the ease in index maintenance and un-needed overhead on maintaining indexes that are not serving a purpose

select
 'drop index ' + stats.table_name + '.' + i.name as DropIndexStatement,
 stats.table_name as TableName,
 i.name as IndexName,
 i.type_desc as IndexType,
 stats.seeks + stats.scans + stats.lookups as TotalAccesses,
 stats.seeks as Seeks,
 stats.scans as Scans,
 stats.lookups as Lookups
 from
 (select
 i.object_id,
 object_name(i.object_id) as table_name,
 i.index_id,
 sum(i.user_seeks) as seeks,
 sum(i.user_scans) as scans,
 sum(i.user_lookups) as lookups
 from
 sys.tables t
 inner join sys.dm_db_index_usage_stats i
 on t.object_id = i.object_id
 group by
 i.object_id,
 i.index_id
 ) as stats
 inner join sys.indexes i
 on stats.object_id = i.object_id
 and stats.index_id = i.index_id
 where stats.seeks + stats.scans + stats.lookups = 0 --Finds indexes not being used
 and i.type_desc = 'NONCLUSTERED' --Only NONCLUSTERED indexes
 and i.is_primary_key = 0 --Not a Primary Key
 and i.is_unique = 0 --Not a unique index
 and stats.table_name not like 'sys%'
 order by stats.table_name, i.name

Rate

4 (9)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (9)

You rated this post out of 5. Change rating