Finding Unused Index in SQL Server

Hello everyone,

In this article, I will try to give information about finding Unused Index values in SQL Server.

In SQL Server you may want to find unused Index values in some cases.

You can easily do this by using the script below.

SELECT TOP 25
       o.name AS ObjectName,
       i.name AS IndexName,
       i.index_id AS IndexID,
       dm_ius.user_seeks AS UserSeek,
       dm_ius.user_scans AS UserScans,
       dm_ius.user_lookups AS UserLookups,
       dm_ius.user_updates AS UserUpdates,
       p.TableRows,
       'DROP INDEX ' + QUOTENAME(i.name) + ' ON ' + QUOTENAME(s.name) + '.' + QUOTENAME(OBJECT_NAME(dm_ius.object_id)) AS 'drop statement'
FROM sys.dm_db_index_usage_stats dm_ius
    INNER JOIN sys.indexes i
        ON i.index_id = dm_ius.index_id
           AND dm_ius.object_id = i.object_id
    INNER JOIN sys.objects o
        ON dm_ius.object_id = o.object_id
    INNER JOIN sys.schemas s
        ON o.schema_id = s.schema_id
    INNER JOIN
    (
        SELECT SUM(p.rows) TableRows,
               p.index_id,
               p.object_id
        FROM sys.partitions p
        GROUP BY p.index_id,
                 p.object_id
    ) p
        ON p.index_id = dm_ius.index_id
           AND dm_ius.object_id = p.object_id
WHERE OBJECTPROPERTY(dm_ius.object_id, 'IsUserTable') = 1
      AND dm_ius.database_id = DB_ID()
      AND i.type_desc = 'nonclustered'
      AND i.is_primary_key = 0
      AND i.is_unique_constraint = 0
ORDER BY (dm_ius.user_seeks + dm_ius.user_scans + dm_ius.user_lookups) ASC;

Note: It goes without saying, please test all the script on your development server and after they pass your test, deploy them on production server.

Good luck to everyone in business and life.

93 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!