Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

A place to share commonly used scripts for SQL Server Database Administrators.

Moderators

SQL Server DBA Scripts

TSQL Script to identify unused indexes

Mar 10 2012 12:00AM by Jacob Sebastian   

This is a script to to identify the unused indexes within a database. The column 'ReadsPerWrite' is an important field to consider.

I am not the original author of this script. It ended up in my personal collection from some source (may be from MSDN or from another online resource). I have made slight changes to the original script

WITH cte AS (
    SELECT 
    	'['+ c.name + '].[' + o.name + ']' AS TableName,
    	i.name AS IndexName,
    	i.index_id AS IndexID,   
    	user_seeks + user_scans + user_lookups AS Reads,
    	user_updates AS Writes,
    	(
    		SELECT SUM(p.rows) 
    		FROM sys.partitions p 
    		WHERE p.index_id = s.index_id 
    			AND s.object_id = p.object_id
    	) AS TotalRows,
    	CASE
    		WHEN s.user_updates < 1 THEN 100
    		ELSE 1.00 * (s.user_seeks + s.user_scans + s.user_lookups) 
    			/ s.user_updates
    	END AS ReadsPerWrite,
    	'DROP INDEX ' + QUOTENAME(i.name) 
    		+ ' ON ' + QUOTENAME(c.name) 
    		+ '.' + QUOTENAME(OBJECT_NAME(s.object_id)) 
    		AS 'DropSQL'
    FROM sys.dm_db_index_usage_stats s  
    INNER JOIN sys.indexes i ON i.index_id = s.index_id 
    	AND s.object_id = i.object_id   
    INNER JOIN sys.objects o on s.object_id = o.object_id
    INNER JOIN sys.schemas c on o.schema_id = c.schema_id
    WHERE OBJECTPROPERTY(s.object_id,'IsUserTable') = 1
    	AND s.database_id = DB_ID()   
    	AND i.type_desc = 'nonclustered'
    	AND i.is_primary_key = 0
    	AND i.is_unique_constraint = 0
    AND 
    (
    	SELECT SUM(p.rows) 
    	FROM sys.partitions p 
    	WHERE p.index_id = s.index_id 
    		AND s.object_id = p.object_id
    ) > 10000
)
SELECT * FROM cte
/*
TableName   Name  ID Reads   Writes Rows  ReadsPerWrite DropSQL
----------  ----- -- ------- ------ ----- ------------- -------
[dbo].[t1]  IDX_1 20 1227972 996081 78599 1.23280335635 DROP ..
[dbo].[t2]  IDX_2 21 51359   33912  78599 1.51447865062 DROP ..
[dbo].[t3]  IDX_3 22 235280  159808 78599 1.47226672006 DROP ..
*/


Jacob Sebastian
1 · 100% · 32002
1 Readers found it to be lifesaver
Nakul Vachhrajani found it to be lifesaver on 3/14/2012 1:07:00 PM
Profile · Blog · Facebook · Twitter
11
 
1
 
0
Failed
 
1
Lifesaver
 
 
1



Submit

2  Comments  

  • Its great script. Works perfectly.

    Thanks Jacob.

    commented on Mar 10 2012 9:54PM
    Abhishek Sur
    121 · 1% · 426
  • This isn't necessarily true. The sys.dm_db_index_usage_stats DMV is cleared by a restart of SQL or any time the database is closed, so if you've had the instance running for a day then the best you can say is that the indexes that have no seeks or scans is that they haven't been used in a day.

    Better to monitor the sys.dm_db_index_usage_stats DMV over a longer period of time. Can't really suggest a time frame because it depends on your particular database usage - e.g. if you hit the database consistently in the same way then probably only need to monitor for a few days, if you hit the database in an irregular fashion then we could be looking at a much longer time period.

    commented on Mar 28 2012 6:33AM
    Cadavre
    206 · 1% · 225

Your Comment


Sign Up or Login to post a comment.

"TSQL Script to identify unused indexes" rated 5 out of 5 by 11 readers
TSQL Script to identify unused indexes , 5.0 out of 5 based on 11 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]