这些信息在目录元数据视图中可用,类似于:
select ...
from sys.indexes i
join sys.index_columns ic on i.object_id = ic.object_id
and i.index_id = ic.index_id
join sys.columns c
on ic.object_id = c.object_id
and ic.index_column_id = c.column_id
where i.object_id = object_id('MyTable')
and (c.name = 'CustomerId' and ic.key_ordinal = 1 and ic.is_descending_key = 0)
or (ic.is_included_column = 1 and c.name in ('Id', 'ModificationDate', 'ProductId'));
这不会产生是/否的答案,而是向您显示可能已经涵盖您正在计划的索引的索引。您需要考虑键位置、键方向和包含的列,覆盖重叠((K1,K2)上的索引覆盖(K1),以及包括(C1,C2,C3)覆盖(C1,C3)的索引. 对于一个真正的生产系统,你需要考虑隐含的列(例如聚集索引键)、索引过滤器表达式和索引数据空间的差异(即partitioninig)