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
  • 相关阅读:
    排序算法(2)-冒泡排序及优化
    [转]Java泛型详解
    [转]Java泛型详解
    02-机器学习_(knn分类算法与应用)
    01-机器学习_(python数据类型详解)
    流量运营项目说明
    数据仓库星型模型vs雪花模型
    数据仓库命名规范
    数据仓库建模
    数据仓库
  • 原文地址:https://www.cnblogs.com/jinzhenshui/p/2779435.html
Copyright © 2011-2022 走看看