zoukankan      html  css  js  c++  java
  • Sqlserver 2005+:查看索引【index】的【使用情况】:无效的索引、高成本索引

    set nocount on
    go
    
    --无效索引排序
    select top 20
        db_name() as db_name
        ,schema_name(o.schema_id) as schema_name
        ,o.name as object_name
        ,i.name as index_name,i.is_unique_constraint,(case i.index_id when 1 then 'is_clustered' else '' end) as is_clustered
        ,ir.rowcnt
        ,s.user_updates
        ,(s.user_lookups + s.user_scans + s.user_seeks) as [retrieval usage] --查询使用次数
        ,s.system_seeks + s.system_scans + s.system_lookups as [system usage]
        ,'DROP INDEX ' + quotename(o.name) + '.' + quotename(i.name) as [ -- dsql]
    from sys.dm_db_index_usage_stats s
        inner join sys.indexes i on s.database_id=db_id() and s.[object_id] = i.[object_id] and s.index_id = i.index_id
        inner join sys.objects o on i.object_id = o.object_id
        inner join sysindexes ir on ir.id=i.object_id and ir.indid=i.index_id
    where s.database_id = db_id()
        and o.is_ms_shipped = 0
        and i.name is not null
        --and s.user_seeks = 0
        --and s.user_scans = 0
        --and s.user_lookups = 0
        and (s.user_lookups + s.user_scans + s.user_seeks)=0
    order by s.user_updates desc
    
    go
    
    -- 维护成本 排序
    select top 20
        db_name() as db_name
        ,schema_name(o.schema_id) as schema_name
        ,o.name as object_name
        ,i.name as index_name,i.is_unique_constraint,(case i.index_id when 1 then 'is_clustered' else '' end) as is_clustered
        ,ir.rowcnt
        ,s.user_updates -- as [update usage] --更新成本
        ,(s.user_seeks + s.user_scans + s.user_lookups) as [retrieval usage] --查询使用次数
        ,(s.user_updates) - (s.user_seeks + user_scans + s.user_lookups) as [maintenance cost] --用户维护成本
        ,s.system_seeks + s.system_scans + s.system_lookups as [system usage] --系统内部维护次数,--内部维护成本
        ,s.last_user_seek
        ,s.last_user_scan
        ,s.last_user_lookup
        ,'DROP INDEX ' + quotename(o.name) + '.' + quotename(i.name) as [ -- dsql]
    from sys.dm_db_index_usage_stats s
        inner join sys.indexes i on s.database_id=db_id() and s.[object_id] = i.[object_id] and s.index_id = i.index_id
        inner join sys.objects o on i.object_id = o.object_id
        inner join sysindexes ir on ir.id=i.object_id and ir.indid=i.index_id
    where s.database_id = db_id()
        and o.is_ms_shipped = 0
        and i.name is not null
        and (s.user_seeks + s.user_scans + s.user_lookups) > 0
    order by [maintenance cost] desc
  • 相关阅读:
    解决QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'问题
    error while loading shared libraries:libQt5SerialPort.so问题解决
    洛谷P4343 [SHOI2015]自动刷题机
    十一届山东省大学生程序设计竞赛 部分题解
    洛谷P4185 [USACO18JAN]MooTube G 题解
    洛谷P4588 [TJOI2018]数学计算
    洛谷P2085《最小函数值》
    多项式小记
    CF932F 【Escape Through Leaf】
    'ipconfig' 不是内部或外部命令,也不是可运行的程序 或批处理文件。持续思考ing
  • 原文地址:https://www.cnblogs.com/jinzhenshui/p/2779435.html
Copyright © 2011-2022 走看看