首页 > 资讯列表 > 编程/数据库 >> 数据库操作教程

SQL语句实现查询Index使用状况

数据库操作教程 2022-09-23 17:32:03 转载来源: 网络整理/侵权必删

SELECTsch.name+'

SELECT   sch.name + '.' + t.name AS [Table Name],   i.name AS[Index Name],   i.type_desc,    ISNULL(user_updates,0) AS [Total Writes],   ISNULL(user_seeks +user_scans + user_lookups,0) AS [Total Reads],   s.last_user_seek,   s.last_user_scan ,   s.last_user_lookup,   ISNULL(user_updates,0) - ISNULL((user_seeks+ user_scans +user_lookups),0)AS [Difference],    p.reserved_page_count * 8.0 / 1024 as SpaceInMBFROM sys.indexes AS i WITH (NOLOCK)    LEFT OUTERJOIN sys.dm_db_index_usage_statsAS s   WITH (NOLOCK) ON s.object_id = i.object_id AND i.index_id = s.index_id AND s.database_id=db_id() AND objectproperty(s.object_id,'IsUserTable') = 1    INNER JOIN    sys.tables             AS t WITH (NOLOCK) ON i.object_id = t.object_id    INNER JOIN    sys.schemas             AS sch   WITH (NOLOCK) ON t.schema_id = sch.schema_id    LEFT OUTERJOIN sys.dm_db_partition_stats AS p WITH (NOLOCK) ON i.index_id = p.index_id and i.object_id = p.object_idWHERE (1=1)   --AND ISNULL(user_updates,0) >=ISNULL((user_seeks + user_scans + user_lookups),0) --shows all indexesincluding those that have not been used    --AND ISNULL(user_updates,0) -ISNULL((user_seeks + user_scans + user_lookups),0)>0 --only shows thoseindexes which have been used    --AND i.index_id > 1        -- Only non-first indexes (I.E.non-primary key)   --AND i.is_primary_key<>1     -- Only those that are not defined asa Primary Key)   --AND i.is_unique_constraint<>1-- Only those that are not classed as "UniqueConstraints". ORDER BY [Table Name],[index name]

标签: SQL 语句 实现 查询 Index 使用 状况


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持