set nocount on go --无效索引排序 select top 20 db_name() as db_name ,schema_name(o.schema_id) as schema_name ,o.name as object_name ,i.name as index_name,i.is_unique_constraint,(case i.index_id when 1 then 'is_clustered' else '' end) as is_clustered ,ir.rowcnt ,s.user_updates ,(s.user_lookups + s.user_scans + s.user_seeks) as [retrieval usage] --查询使用次数 ,s.system_seeks + s.system_scans + s.system_lookups as [system usage] ,'DROP INDEX ' + quotename(o.name) + '.' + quotename(i.name) as [ -- dsql] from sys.dm_db_index_usage_stats s inner join sys.indexes i on s.database_id=db_id() and s.[object_id] = i.[object_id] and s.index_id = i.index_id inner join sys.objects o on i.object_id = o.object_id inner join sysindexes ir on ir.id=i.object_id and ir.indid=i.index_id where s.database_id = db_id() and o.is_ms_shipped = 0 and i.name is not null --and s.user_seeks = 0 --and s.user_scans = 0 --and s.user_lookups = 0 and (s.user_lookups + s.user_scans + s.user_seeks)=0 order by s.user_updates desc go -- 维护成本 排序 select top 20 db_name() as db_name ,schema_name(o.schema_id) as schema_name ,o.name as object_name ,i.name as index_name,i.is_unique_constraint,(case i.index_id when 1 then 'is_clustered' else '' end) as is_clustered ,ir.rowcnt ,s.user_updates -- as [update usage] --更新成本 ,(s.user_seeks + s.user_scans + s.user_lookups) as [retrieval usage] --查询使用次数 ,(s.user_updates) - (s.user_seeks + user_scans + s.user_lookups) as [maintenance cost] --用户维护成本 ,s.system_seeks + s.system_scans + s.system_lookups as [system usage] --系统内部维护次数,--内部维护成本 ,s.last_user_seek ,s.last_user_scan ,s.last_user_lookup ,'DROP INDEX ' + quotename(o.name) + '.' + quotename(i.name) as [ -- dsql] from sys.dm_db_index_usage_stats s inner join sys.indexes i on s.database_id=db_id() and s.[object_id] = i.[object_id] and s.index_id = i.index_id inner join sys.objects o on i.object_id = o.object_id inner join sysindexes ir on ir.id=i.object_id and ir.indid=i.index_id where s.database_id = db_id() and o.is_ms_shipped = 0 and i.name is not null and (s.user_seeks + s.user_scans + s.user_lookups) > 0 order by [maintenance cost] desc