繁体   English   中英

当表只有二级索引时,索引叶子节点存储什么?

[英]What is stored in index leaf nodes when a table has only secondary index?

如果一个表有聚簇索引和二级索引,二级索引中的叶子节点包含聚簇索引的属性值。 但是如果一个表只有非聚集索引呢? 非聚集索引如何在没有聚集索引的情况下检索数据?

create table table_without_primary_key(
    name varchar(30) not null ,
    date datetime not null
);

insert into table_without_primary_key
values ('jack',now());
insert into table_without_primary_key
values ('alice',now());
insert into table_without_primary_key
values ('ribbon',now());

create index time_index
on table_without_primary_key (date);
show index from table_without_primary_key;

结果:

+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table                     | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| table_without_primary_key |          1 | time_index |            1 | date        | A         |           2 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.02 sec)

另外,我也听人说,如果一个表没有主键或唯一索引,它会使用row id来创建聚簇索引。

但我怀疑这是不是真的,因为我在上面描述的表中没有发现行 id 上的自动聚集索引。

所有 InnoDB 表都存储为聚集索引。

https://dev.mysql.com/doc/refman/8.0/en/innodb-index-types.html说:

如果表没有 PRIMARY KEY 或合适的 UNIQUE 索引,InnoDB 会在包含行 ID 值的合成列上生成一个名为 GEN_CLUST_INDEX 的隐藏聚集索引。 ...

行 ID 是一个 6 字节的字段,随着插入新行而单调增加。

除了比尔所说的,并在 8.0.31-0ubuntu0.22.04.1 上进行了测试:

SELECT * FROM mysql.innodb_index_stats WHERE table_name = 'table_without_primary_key' ;
+---------------+---------------------------+-----------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name                | index_name      | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+---------------------------+-----------------+---------------------+--------------+------------+-------------+-----------------------------------+
| try           | table_without_primary_key | GEN_CLUST_INDEX | 2022-12-01 11:39:23 | n_diff_pfx01 |          2 |           1 | DB_ROW_ID                         |
| try           | table_without_primary_key | GEN_CLUST_INDEX | 2022-12-01 11:39:23 | n_leaf_pages |          1 |        NULL | Number of leaf pages in the index |
| try           | table_without_primary_key | GEN_CLUST_INDEX | 2022-12-01 11:39:23 | size         |          1 |        NULL | Number of pages in the index      |
| try           | table_without_primary_key | time_index      | 2022-12-01 11:39:23 | n_diff_pfx01 |          1 |           1 | date                              |
| try           | table_without_primary_key | time_index      | 2022-12-01 11:39:23 | n_diff_pfx02 |          3 |           1 | date,DB_ROW_ID                    |
| try           | table_without_primary_key | time_index      | 2022-12-01 11:39:23 | n_leaf_pages |          1 |        NULL | Number of leaf pages in the index |
| try           | table_without_primary_key | time_index      | 2022-12-01 11:39:23 | size         |          1 |        NULL | Number of pages in the index      |
+---------------+---------------------------+-----------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.01 sec)

请注意,它确实有一个名为GEN_CLUST_INDEXPRIMARY KEY

二级索引中的“行”将具有

  1. 二级索引的列,按指定顺序排列。
  2. PRIMARY KEY中的任何其他列。 如前所述,总会有一个 PK,但您可能会将其视为UNIQUE或隐藏的 6 字节东西。

还会有 B+Tree 开销的东西,比如到其他节点的链接,无论是在同一层还是其他层。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM