I have some old tables with, what I think, are nearly worthless indexes. How can I easily be sure that are not ever being used before I drop them.
Based on Joe's answer I came up with this:
SELECT
row_number() over(order by user_seeks,user_lookups,user_scans),
[Database] = d.name,
[Schema]= s.name,
[Table]= o.name,
[Index]= x.name,
[Scans] = user_scans,
[Seeks] = user_seeks,
[Lookups] = user_lookups,
[Last Scan] = last_user_scan,
[System Scans] = system_scans
FROM sys.dm_db_index_usage_stats u
INNER JOIN sys.sysdatabases d on u.database_id = d.dbid
INNER JOIN sys.sysindexes x on u.object_id = x.id and u.index_id = x.indid
INNER JOIN sys.objects o on u.object_id = o.object_id
INNER JOIN sys.schemas s on s.schema_id = o.schema_id
where x.name is not null
order by 1 desc
Take a look at the sys.dm_db_index_usage_stats DMV. Just be careful to keep in mind that these counters are reset every time SQL Server is restarted.
Check if the user seeks of the index are increasing with every select on the table u r using unless the server is restarted this will give you a clear idea which index is being used or not.
SELECT DISTINCT OBJECT_NAME(sis.OBJECT_ID) TableName, si.name AS IndexName, sc.Name AS ColumnName,
sic.Index_ID, sis.user_seeks, sis.user_scans, sis.user_lookups, sis.user_updates
FROM sys.dm_db_index_usage_stats sis
INNER JOIN sys.indexes si ON sis.OBJECT_ID = si.OBJECT_ID AND sis.Index_ID = si.Index_ID
INNER JOIN sys.index_columns sic ON sis.OBJECT_ID = sic.OBJECT_ID AND sic.Index_ID = si.Index_ID
INNER JOIN sys.columns sc ON sis.OBJECT_ID = sc.OBJECT_ID AND sic.Column_ID = sc.Column_ID
WHERE sis.Database_ID = DB_ID('database_name') AND sis.OBJECT_ID = OBJECT_ID('table_name');
GO
Check out this section of this article by Ian Stirk in 2007. Very nice set of tools, including finding your unused indexes.
精彩评论