获取当某个表所有索引使用情况
DECLARE @dbName SYSNAME
,@schemaName SYSNAME
,@ObjectName SYSNAME
,@sql NVARCHAR(max)
SELECT @dbName='DBname'
,@schemaName='dbo'
,@ObjectName='tablename'
SET @sql='SELECT COUNT(1) AS DataCount FROM '+@dbName+'.'+@schemaName+'.'+@ObjectName+' WITH (NOLOCK)'
EXEC sp_executesql @sql
;with data
as (
SELECT
DB_NAME(A.database_id)
+'.'+SCHEMA_NAME(C.schema_id)
+'.'+OBJECT_NAME(A.object_id) AS TableName
,B.Name AS IndexName
,D.Name AS ColumnName
,A.user_seeks AS IndexSeek
,A.user_scans AS IndexScan
,A.user_lookups AS IndexKeyLook
,A.user_updates AS IndexUpdate
,CAST(CAST(A.user_seeks*100.00/CASE WHEN (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates)=0 THEN 1 else (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) end
AS decimal(6,2))
AS VARCHAR(6))+'%' AS IndexSeekPersent
,CAST(CAST(A.user_scans*100.00/CASE when (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) =0 then 1 else (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) end
AS decimal(6,2))
AS VARCHAR(6))+'%' AS IndexScanPersent
,CAST(CAST(A.user_lookups*100.00/ case when (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) =0 then 1 else (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) end
AS decimal(6,2))
AS VARCHAR(6))+'%' AS IndexKeyLookPersent
,CAST(CAST(A.user_updates*100.00/case when (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) = 0 then 1 else (A.user_seeks+A.user_scans+A.user_lookups+A.user_updates) end
AS decimal(6,2))
AS VARCHAR(6))+'%' AS IndexUpdatePersent
FROM sys.dm_db_index_usage_stats AS A WITH (NOLOCK)
INNER JOIN sys.indexes AS B WITH (nolock)
ON A.index_id=B.index_id
INNER JOIN sys.tables AS C WITH (NOLOCK)
ON B.object_id=C.object_id
AND A.object_id=C.object_id
INNER JOIN sys.columns AS D WITH (NOLOCK)
ON A.object_id=D.object_id
INNER JOIN sys.index_columns AS E WITH (NOLOCK)
ON B.index_id=E.index_id
AND D.column_id=E.column_id
AND A.object_id=E.object_id
WHERE DB_NAME(A.database_id)= @dbName
AND OBJECT_NAME(A.object_id) = @ObjectName
AND OBJECT_NAME(A.object_id) not like '%Subscription%' --排除Replication相关的系统表
AND B.is_primary_key = 0 --排除PK
AND SCHEMA_NAME(C.schema_id)=@schemaName
AND B.name IS NOT NULL
AND B.is_disabled=0
AND B.is_hypothetical=0
)
select *
from data
where (cast(replace(IndexSeekPersent,'%','') as decimal(6,2)) <= 0.1
or cast(replace(IndexUpdatePersent,'%','') as decimal(6,2)) >=0.9)
and
IndexSeek + IndexScan + IndexKeyLook + IndexUpdate <>0
ORDER BY cast(replace(IndexUpdatePersent,'%','') as decimal(6,2)) DESC
--IndexSeek DESC
-- ,IndexScan DESC
-- ,IndexKeyLook DESC
-- ,IndexUpdate DESC
select o.name ,i.name ,i.type from
sys.indexes i inner join sys.objects o on i.object_id = o.object_id
inner join sys.partitions p on p.index_id = i.index_id and p.object_id = i.object_id
where p.partition_id = 72057594065518592
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
2010-11-09 DXperience-8.2.6 注册
2010-11-09 VS2005最近项目和最近文件清除