您可以运行脚本来确定数据库的索引使用情况:
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)
列在哪里:
表名
index_type_desc:集群、堆或非集群
index_name
total_reads
scan_percentage:读取的百分比是扫描(错误的查询计划)
total_writes
writes_percentage:检测更新但未使用的索引 (>100%)
锁定和闩锁:显示单用户延迟(闩锁)和多用户延迟(锁定)
avg_fragmentation_in_percent & p.page_count:告诉你索引碎片(索引健康)
运行它,你可以看到 SQL 使用你的 index1、index2、index3 等的次数。
除了您的问题之外,您还会看到索引的延迟和维护成本。我认为,在 7300 万条记录中,它是重要的价值。