|
from http://sqlserverpedia.com/wiki/Find_Indexes_Not_In_Use
See Also: Main_Page - Transact SQL Code Library - Index Performance Tuning
This query can help identify indexes that have not been used since the last restart. You can also switch the sort order to see your heavily used indexes.
The "reads_per_write" field helps to find indexes that aren't helping to improve performance. For every 1 write to the index, you want to see as many reads as possible. Indexes with a reads_per_write score of 1 mean that for every 1 write, the index is also used 1 time to help with performance. Ideally, you want to see scores much higher than that. Consider dropping indexes with a reads_per_write score under zero, and strongly consider dropping ones with scores under .1.
This isn't a hard-and-fast rule: for example, you may have an index that's only used once per month for a single report, but that report is run by the CEO and he wants it instantaneously. Before dropping indexes, know what they're used for, or make sure alternate indexes exist. Alternate indexes would be indexes that are wider than the index you're dropping, and include enough fields to serve the query's needs.
SELECT
o.name
, indexname=i.name
, i.index_id
, reads=user_seeks + user_scans + user_lookups
, writes = user_updates
, rows = (SELECT SUM(p.rows) FROM sys.partitions p WHERE p.index_id = s.index_id AND s.object_id = p.object_id)
, 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 reads_per_write
, 'DROP INDEX ' + QUOTENAME(i.name)
+ ' ON ' + QUOTENAME(c.name) + '.' + QUOTENAME(OBJECT_NAME(s.object_id)) as 'drop statement'
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
ORDER BY reads
- Audit a SQL Server Configuration - this script helps you take control of a SQL Server you've never seen before.
- Backup and Restore Scripts - examples of how to restore individual filegroups or files.
- CMDB Examples - a sample Configuration Management Database using SQL Server 2008's Central Management Server.
- Conversion Functions - convert dates, strings, numbers, etc.
- Current Activity - shows what's going on your server right now.
- Date Management Functions - functions to add/remove business days to a date.
- Developer Utilities - search for a string in database objects, list extended properties for objects.
- Error Management - set up Operators and Alerts to let you know when the server breaks
- Error Handling with TSQL - an intricate example of how to trap errors with SQL 2005.
- Execute T-SQL in Parallel - use CLR to execute T-SQL code across several databases at the same time.
- File Activity - find out which files are the most heavily accessed.
- Find Detached Databases - find detached mdf/ndf/ldf files sitting on your servers taking up space.
- Index Maintenance - defragmenting your indexes gets you free performance.
- Index Performance Tuning - find missing indexes, find unused indexes.
- Memory Performance Tuning - look at how SQL Server is using its memory.
- Procedure Cache Related Queries - check what queries have been running lately and why they've been slow.
- Policy Based Management Samples
- Size Calculations - calculate current table size, count rows in all tables, predict future table sizes, etc.
- SQL Server Agent Job Query Samples - query for failed SQL agent jobs, disable all jobs, sort all job steps alphabetically.
- String Manipulation Functions - replace substrings, word wrapping, trim patterns.
- Last clean DBCC CHECKDB date - find the last known clean copy of the database without corruption.
- Server Audit Examples in SQL Server 2008 - Introduction and sample scripts.
- Reporting Services Meta-data scripts - Introduction and sample scripts.
- The Object Within - Finding tables referenced in a stored procedure.
- Password Generation - query to create a random password of variable character length.
|