Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

SQL Server - Get List of Ununsed Index using SYS.DM_DB_INDEX_USAGE_STATS

Oct 12 2011 6:46AM by Nirav   

DMV "SYS.DMDBINDEXUSAGESTATS" provides index usage stats. By using this we can find the unused indexes. Below script will gives you unused indexes

USE YourDatabseName
GO
DECLARE @dbid INT
SELECT @dbid = DB_ID(DB_NAME())
SELECT OBJECTNAME = OBJECT_NAME(I.OBJECT_ID),
INDEXNAME = I.NAME,
I.INDEX_ID
FROM SYS.INDEXES I
JOIN SYS.OBJECTS O
ON I.OBJECT_ID = O.OBJECT_ID
WHERE OBJECTPROPERTY(O.OBJECT_ID,'IsUserTable') = 1
AND I.INDEX_ID NOT IN (
SELECT S.INDEX_ID
FROM SYS.DM_DB_INDEX_USAGE_STATS S
WHERE S.OBJECT_ID = I.OBJECT_ID
AND I.INDEX_ID = S.INDEX_ID
AND DATABASE_ID = @dbid)
ORDER BY OBJECTNAME,
I.INDEX_ID,
INDEXNAME ASC
GO
Read More..   [0 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


Nirav
37 · 5% · 1593
4
 
1
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

1  Comments  

  • I had to adapt the script, as my servers are using a 'case sensitive' collation :

    DECLARE @dbid INT;
    SELECT @dbid = DB_ID(DB_NAME())
    SELECT  OBJECTNAME = OBJECT_NAME(I.object_id),
        	INDEXNAME = I.NAME,
        	I.INDEX_ID
    FROM sys.indexes I
    JOIN sys.objects O ON (I.object_id = O.object_id)
    WHERE OBJECTPROPERTY(O.object_id,'IsUserTable') = 1
      AND I.index_id NOT IN (
        SELECT S.index_id
        FROM sys.dm_db_index_usage_stats S
        WHERE S.object_id = I.object_id
        AND I.index_id = S.index_id
        AND database_id = @dbid)
    ORDER BY OBJECTNAME,I.index_id,INDEXNAME ASC
    GO
    
    commented on Oct 13 2011 2:03AM
    Bert
    57 · 3% · 1038

Your Comment


Sign Up or Login to post a comment.

"SQL Server - Get List of Ununsed Index using SYS.DM_DB_INDEX_USAGE_STATS " rated 5 out of 5 by 4 readers
SQL Server - Get List of Ununsed Index using SYS.DM_DB_INDEX_USAGE_STATS , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]