【问题标题】:How to identify duplicate index in sql server如何识别sql server中的重复索引
【发布时间】:2014-10-02 19:04:12
【问题描述】:

我有一个大约 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 索引来微调性能和存储,它会以相同的方式工作吗?

【问题讨论】:

    标签: sql-server indexing


    【解决方案1】:

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

    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 万条记录中,它是重要的价值。

    【讨论】:

    • 我为我的工具创建了这个脚本来调整 ERP 系统。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2019-01-24
    • 2010-10-26
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多