SqlServer中查看索引的使用情况

—在优化查询SQL语句,查看索引使用情况SQL语句:

select db_name(database_id) as N数据库名称,
       object_name(a.object_id) as N表名,
       b.name N索引名称,
       user_seeks N用户索引查找次数,
       user_scans N用户索引扫描次数,
       last_user_seek N最后查找时间,
       last_user_scan N最后扫描时间,
       rows as N表中的行数
from sys.dm_db_index_usage_stats a join 
     sys.indexes b
     on a.index_id = b.index_id
     and a.object_id = b.object_id
     join sysindexes c
     on c.id = b.object_id
where database_id=db_id(数据库名)   --指定数据库
     and object_name(a.object_id) not like sys%
     and object_name(a.object_id) like 表名  --指定索引表
     and b.name like 索引名 --指定索引名称 可以先使用 sp_help 你的表名 查看表的结构和所有的索引信息
order by user_seeks,user_scans,object_name(a.object_id)

—在优化查询SQL语句,查看索引使用情况SQL语句:

select db_name(database_id) as N数据库名称,
       object_name(a.object_id) as N表名,
       b.name N索引名称,
       user_seeks N用户索引查找次数,
       user_scans N用户索引扫描次数,
       last_user_seek N最后查找时间,
       last_user_scan N最后扫描时间,
       rows as N表中的行数
from sys.dm_db_index_usage_stats a join 
     sys.indexes b
     on a.index_id = b.index_id
     and a.object_id = b.object_id
     join sysindexes c
     on c.id = b.object_id
where database_id=db_id(数据库名)   --指定数据库
     and object_name(a.object_id) not like sys%
     and object_name(a.object_id) like 表名  --指定索引表
     and b.name like 索引名 --指定索引名称 可以先使用 sp_help 你的表名 查看表的结构和所有的索引信息
order by user_seeks,user_scans,object_name(a.object_id)
经验分享 程序员 微信小程序 职场和发展