Sqlserver 2005+:查看索引【index】的【使用情况】:无效的索引、高成本索引
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
分类:
Sqlserver
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)