我有一个包含大约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)
我们可以说非clusted索引是一个重复索引,我们可以通过仅在Col25上创建NC索引来微调性能和存储,它将以相同的方式工作吗?
答案 0 :(得分:0)
您可以运行脚本来确定数据库的索引使用情况:
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:clustered,heap或nonclustered
INDEX_NAME
total_reads
scan_percentage:扫描的读取百分比(查询计划错误)
total_writes
writes_percentage:检测更新但未使用的索引(> 100%)
锁定& latchs:显示单用户延迟(latch)和多用户延迟(锁)
avg_fragmentation_in_percent& p.page_count:告诉你索引碎片(索引运行状况)
运行它,您可以看到SQL使用index1,index2,index3等的次数。
除了您的问题,您还会看到索引的延迟和维护成本。我认为,有7300万条记录是重要的价值。