MSSQL·查询数据库中所有索引的相关信息

阅文时长 | 0.45分钟 字数统计 | 784字符
主要内容 | 1、引言&背景 2、声明与参考资料
『mssql·查询数据库中所有索引的相关信息』
编写人 | scschero 编写时间 | 2021/5/16 am1:56
文章类型 | 系列 完成度 | 已完成
座右铭 每一个伟大的事业,都有一个微不足道的开始。

一、引言&背景   完成度:100%

a) 应对问题&场景

查询db中所有索引的相关信息。

b) 解决原理&方法

select case
           when t.[type] = 'u' then
               '表'
           when t.[type] = 'v' then
               '视图'
       end as '类型',
       schema_name(t.schema_id) + '.' + t.[name] as '(表/视图)名称',
       i.[name] as 索引名称,
       substring(column_names, 1, len(column_names) - 1) as '列名',
       case
           when i.[type] = 1 then
               '聚集索引'
           when i.[type] = 2 then
               '非聚集索引'
           when i.[type] = 3 then
               'xml索引'
           when i.[type] = 4 then
               '空间索引'
           when i.[type] = 5 then
               '聚簇列存储索引'
           when i.[type] = 6 then
               '非聚集列存储索引'
           when i.[type] = 7 then
               '非聚集哈希索引'
       end as '索引类型',
       case
           when i.is_unique = 1 then
               '唯一'
           else
               '不唯一'
       end as '索引是否唯一'
from sys.objects t
    inner join sys.indexes i
        on t.object_id = i.object_id
    cross apply
(
    select col.[name] + ', '
    from sys.index_columns ic
        inner join sys.columns col
            on ic.object_id = col.object_id
               and ic.column_id = col.column_id
    where ic.object_id = t.object_id
          and ic.index_id = i.index_id
    order by col.column_id
    for xml path('')
) d(column_names)
where t.is_ms_shipped <> 1
      and index_id > 0
order by i.[name];

二、声明与参考资料   完成度:100%

原创博文,未经许可请勿转载。

如有帮助,欢迎点赞、收藏、关注。如有问题,请评论留言!如需与博主联系的,直接博客私信scschero即可。

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐