在非聚集索引中包含聚集索引键

mdo*_*yle 6 sql-server-2008 sql-server

我们正在运行 SQL Server 2008。在查看我在 sys.dm_db_missing_index_* 视图上运行的查询结果时,我看到了一个关于包含列的奇怪建议。我有以下两个表:

create table foo (
    id int not null identity(1,1) primary key
    /* many more fields, not relevant to question */
)

create table bar (
    id int not null identity(1,1) primary key
    foo_id int not null,
    param_name nvarchar(50),
    param_value nvarchar(255)
)
Run Code Online (Sandbox Code Playgroud)

表 foo 与 bar 有 1:M 的关系;bar 用于存储与 foo 中的某些记录相关的杂项。

回到 sys.dm_db_missing_index_* 查询。一天中,优化器会在equality_columns 中查找foo_id 和included_columns 中的id 数次在bar 上查找索引。我的问题是,在非聚集索引中包含表的聚集索引键有什么意义?由于非聚簇索引的叶级无论如何都包含聚簇索引的键值,那么在非聚簇索引中包含聚簇索引键不是多余的吗?

编辑: bar 上唯一现有的索引是以 PK 作为键的聚集索引。

提前致谢。

JNK*_*JNK 9

是的,这是多余的。

另一方面,它不会伤害任何东西(因为优化器会忽略它并且不会两次添加数据)。

这是不以表面价值看待建议指数的众多原因之一。