Sql server 如何在sql server中识别重复索引

Sql server 如何在sql server中识别重复索引,sql-server,indexing,Sql Server,Indexing,我有一个大约有100列的表,总行数约为7300万行 e.g. Table(Col1, Col2, Col3, Col4,....Col100) Composite Clusterd Index(Col1, Col2, Col3, Col4, Col5) Composite Non Clustered Index(Col25, Col1, Col2, Col3, Col4, Col5) 我们是否可以说非聚集索引是一个重复的索引,我们可以通过仅在Col25上创建NC索引来微调性能和存储,并以相同

我有一个大约有100列的表,总行数约为7300万行

e.g. Table(Col1, Col2, Col3, Col4,....Col100)

Composite Clusterd Index(Col1, Col2, Col3, Col4, Col5)
Composite Non Clustered Index(Col25, Col1, Col2, Col3, Col4, Col5)

我们是否可以说非聚集索引是一个重复的索引,我们可以通过仅在Col25上创建NC索引来微调性能和存储,并以相同的方式工作?

您可以运行脚本来确定数据库的索引使用情况:

select 
object_name(s.object_id) as table_name,
i.type_desc as index_type_desc,
case when s.index_id=0 then  '' else i.name end as index_name, 
s.user_seeks + s.user_scans + s.user_lookups as total_reads,
case when (s.user_seeks + s.user_scans + s.user_lookups)=0 then 0
else (convert(float,s.user_scans)) / (s.user_seeks + s.user_scans + s.user_lookups) * 100.00 end as scan_percentage,
s.user_updates as total_writes,
case when (s.user_seeks + s.user_scans + s.user_lookups)=0 then 0 else 
(convert(float,s.user_updates)) / (s.user_seeks + s.user_scans + s.user_lookups) * 100 end as writes_percentage,
ios.lock_count, ios.lock_wait_in_ms, ios.latch_wait_in_ms, ios.io_latch_wait_in_ms, ios.index_lock_promotion_count,
ph.avg_fragmentation_in_percent, ph.page_count
from 
sys.dm_db_index_usage_stats s
left join  sys.indexes i on s.object_id=i.object_id and s.index_id=i.index_id
left join (
select 
database_id, object_id, index_id,
row_lock_count + page_lock_count as lock_count,
row_lock_wait_in_ms + page_lock_wait_in_ms as lock_wait_in_ms,
page_latch_wait_in_ms + tree_page_latch_wait_in_ms as latch_wait_in_ms,
page_io_latch_wait_in_ms + tree_page_io_latch_wait_in_ms as io_latch_wait_in_ms,
index_lock_promotion_count
from sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL)
where object_id>100 and database_id=DB_ID()
) ios on s.database_id=ios.database_id and s.object_id=ios.object_id and s.index_id=ios.index_id
left join(
select 
p.database_id,
p.object_id,
p.index_id,
p.avg_fragmentation_in_percent,
p.page_count
from 
sys.dm_db_index_physical_stats(db_id(),null,null,null,'limited') p
where 
p.avg_fragmentation_in_percent > 0
) ph on ph.database_id=s.database_id and ph.object_id=s.object_id and ph.index_id=s.index_id
where
s.database_id=DB_ID() and s.object_id>100 OPTION (RECOMPILE)
其中列为:

  • 表名称

  • 索引类型描述:集群、堆或非集群

  • 索引名称

  • 总读数

  • 扫描百分比:扫描的读取百分比(错误的查询计划)

  • 总写入数

  • 写入百分比:检测更新但未使用的索引(>100%)

  • 锁定和锁定:显示单用户延迟(锁定)和多用户延迟(锁定)

  • 平均碎片百分比和p.page计数:告诉您有关索引碎片(索引运行状况)的信息

运行它,您可以看到SQL使用index1、index2、index3等的次数


除了您的问题,您还将看到索引的延迟和维护成本。在7300万条记录中,我认为这是重要的价值观。

我创建这个脚本是为了我的工具来调整ERP系统。