获取数据库中所有的索引信息
类别:数据库 浏览量:475
时间:2016-2-18 获取数据库中所有的索引信息
获取数据库中所有的索引信息SQL SERVER中数据库中所有的索引信息的方法
WITH tx AS
(
SELECT a.object_id
,b.name AS schema_name
,a.name AS table_name
,c.name as ix_name
,c.is_unique AS ix_unique
,c.type_desc AS ix_type_desc
,d.index_column_id
,d.is_included_column
,e.name AS column_name
,f.name AS fg_name
,d.is_descending_key AS is_descending_key
,c.is_primary_key
,c.is_unique_constraint
FROM sys.tables AS a
INNER JOIN sys.schemas AS b ON a.schema_id = b.schema_id AND a.is_ms_shipped = 0
INNER JOIN sys.indexes AS c ON a.object_id = c.object_id
INNER JOIN sys.index_columns AS d ON d.object_id = c.object_id AND d.index_id = c.index_id
INNER JOIN sys.columns AS e ON e.object_id = d.object_id AND e.column_id = d.column_id
INNER JOIN sys.data_spaces AS f ON f.data_space_id = c.data_space_id
where a.name<>'sysdiagrams'
)
SELECT
Drop_Index = CASE WHEN (a.is_primary_key = 1 OR a.is_unique_constraint = 1)
THEN 'ALTER TABLE ' + a.table_name + ' DROP CONSTRAINT ' + a.ix_name
ELSE 'DROP INDEX ' + a.ix_name collate SQL_Latin1_General_CP1_CI_AS + ' ON ' + a.schema_name + '.' + a.table_name END
,Create_Index = CASE WHEN (a.is_primary_key = 1 OR a.is_unique_constraint = 1)
THEN 'ALTER TABLE ' + a.table_name + ' ADD CONSTRAINT ' + a.ix_name
+ CASE WHEN a.is_primary_key = 1 THEN ' PRIMARY KEY' ELSE ' UNIQUE' END + '(' + indexColumns.ix_index_column_name + ')'
ELSE 'CREATE ' + CASE WHEN a.ix_unique = 1 THEN 'UNIQUE ' ELSE '' END
+ a.ix_type_desc + ' INDEX ' + a.ix_name collate SQL_Latin1_General_CP1_CI_AS + ' ON ' + a.schema_name
+ '.' + a.table_name + '(' + indexColumns.ix_index_column_name + ')'
+ CASE WHEN IncludeIndex.ix_included_column_name IS NOT NULL THEN ' INCLUDE (' + IncludeIndex.ix_included_column_name + ')' ELSE '' END
+ ' ON [' + a.fg_name +']' END
,CASE WHEN a.ix_unique = 1 THEN 'UNIQUE' else '' END AS IXUnique
,case when a.ix_type_desc='CLUSTERED' then '聚集索引' when a.ix_type_desc='NONCLUSTERED' then '非聚集索引' else '' end as IXtype
,a.ix_name as IXName
,a.object_id as TableId
,a.table_name as TableName
,indexColumns.ix_index_column_name as ColumnsName
,IncludeIndex.ix_included_column_name as IncludeColumnsName
,a.fg_name
,a.is_primary_key
,a.is_unique_constraint
FROM
(
SELECT DISTINCT
ix_unique
,ix_type_desc
,object_id
,ix_name
,schema_name
,table_name
,fg_name
,is_primary_key
,is_unique_constraint
FROM tx
) AS a
OUTER APPLY
(
SELECT ix_index_column_name
= STUFF((
SELECT ',' + column_name + CASE WHEN is_descending_key = 1 THEN ' DESC' ELSE '' END
FROM tx AS b
WHERE schema_name = a.schema_name
AND table_name=a.table_name
AND ix_name=a.ix_name
AND ix_type_desc=a.ix_type_desc
AND fg_name=a.fg_name
AND is_included_column=0
ORDER BY index_column_id
FOR XML PATH('')
),1,1,'')
)IndexColumns
OUTER APPLY
(
SELECT ix_included_column_name
= STUFF((
SELECT ',' + column_name
FROM tx AS b
WHERE schema_name = a.schema_name
AND table_name=a.table_name
AND ix_name=a.ix_name
AND ix_type_desc=a.ix_type_desc
AND fg_name=a.fg_name
AND is_included_column=1
ORDER BY index_column_id
FOR XML PATH('')
), 1,1,'')
)IncludeIndex
ORDER BY a.schema_name,a.table_name,a.ix_name
例如
标签:索引
您可能感兴趣
- 查看mysql索引缓存(MySQL查询缓存的小知识)
- 深入理解mysql索引(MySQL中冗余和重复索引的区别说明)
- mysql索引失效原因(MySQL索引失效的几种情况详析)
- mysql索引原理和优化(Mysql 索引该如何设计与优化)
- 关于mysql性能优化的描述(Mysql性能优化之索引下推)
- numpy如何获取索引(NumPy 基本切片和索引的具体使用方法)
- mysql 索引使用总结(MySQL复合索引的深入探究)
- pandas获取对应数值的索引(对pandas通过索引提取dataframe的行方法详解)
- mysql索引的弊端(MySQL 全文索引的原理与缺陷)
- 适合小白入门的mysql教程索引简介(一篇带给你MySQL高性能索引)
- mysql 索引表空间(MySQL如何构建数据表索引)
- mysql索引原理及调优(mysql高级学习之索引的优劣势及规则使用)
- mysql索引详解及基本用法(Mysql普通索引与唯一索引的选择详析)
- 最全面的mysql索引详解(MySQL 全文索引使用指南)
- mysql的索引及其介绍总结(浅析MysQL B-Tree 索引)
- mysql索引提高效率(MySql如何查看索引并实现优化)
- 太鸡贼了,这老小区轻松搞定了停车问题(这老小区轻松搞定了停车问题)
- 节日我在岗|警景相融 平安相伴(节日我在岗警景相融)
- 战 疫 时刻 致敬每一位石化大学的 守护者(战疫时刻)
- 老弄堂里的市井味道(老弄堂里的市井味道)
- 小米音乐可绑定QQ音乐, QQ音乐 真的会消失在小米的设备上吗(小米音乐可绑定QQ音乐)
- 小米Watch S1评测 或许能成为小米冲击高端可穿戴设备的里程碑(小米WatchS1评测或许能成为小米冲击高端可穿戴设备的里程碑)