在Sql Server数据库中创建了一个形如:
create index 索引名称 on 索引所在的表(索引的列,可为多列) include (被索引附属的列)
创建好索引后 无法在表—键/索引 中可以看到创建了索引,但找不到Sql语句中关于 include (被索引附属的列) 的信息。
请问如何找到创建的覆盖索引中 include部分的信息?
可以使用下面的SQL语句(答案来自 List all indexes with included columns(nonkeys) ):
SELECT IndexName = i.Name, ColName = c.Name FROM sys.indexes i INNER JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id INNER JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id WHERE ic.is_included_column = 1
查看包含列,使用以下脚本,或直接查看索引的定义
select i.Name as index_name ,c.name as index_key_name ,ic.is_included_column ,ic.index_column_id ,ic.key_ordinal from sys.indexes i inner join sys.index_columns ic on i.object_id = ic.object_id and i.index_id = ic.index_id inner join sys.columns c on ic.object_id = c.object_id and ic.column_id = c.column_id where i.object_id=object_id('Base Table','U') and i.name='index name'
Exec sp_helpindex 表名 查询一张表所有的索引