【问题标题】:include column in sql server index在 sql server 索引中包含列
【发布时间】:2015-02-04 12:17:48
【问题描述】:

所有非聚集索引(NCI)也会存储聚集索引(CI)的键列

在创建 NCI 时,如果我们有意包含键列会发生什么情况,是否会再次占用空间?

意思是存储key column,会不会占用两次空间?

提前致谢

【问题讨论】:

    标签: sql-server indexing include


    【解决方案1】:

    不,空间不会被占用两次。

    create table test (id int not null primary key, c1 int, c2 int)
    
    create index ix1_test on test (c1)
    create index ix2_test on test (c1) include (id)
    create index ix3_test on test (c1) include (id, c2)
    

    sp_SQLskills_SQL2012_helpindex 显示此信息:

    index_name  index_description     index_keys    included_columns    columns_in_tree columns_in_leaf
    [PK__test]  clustered, unique, PK [id]        NULL                  [id]            All columns "included"
    [ix1_test]  nonclustered          [c1]        NULL                  [c1], [id]      [c1], [id]
    [ix2_test]  nonclustered          [c1]        [id]                  [c1], [id]      [c1], [id]
    [ix3_test]  nonclustered          [c1]        [id], [c2]            [c1], [id]      [c1], [id], [c2]
    

    【讨论】:

    • 我理解包含的列不占用空间。所以在上面的例子中c2不占用任何空间。谢谢。
    • 索引是一个b树结构。 c2 是包含列,因此它只出现在叶级别的树中。所以 c2 确实占用空间,但仅在叶级。
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2010-12-14
    相关资源
    最近更新 更多