0%

MySQL的聚集索引

概要

MySQL 的聚集索引,有的地方又叫聚簇索引,其实英文单词是 cluster。

博客

原帖收藏于IT老兵驿站

正文

什么是聚集索引?
参考这里

A clustered index, on the other hand, is actually the table. It is an index that enforces the ordering on the rows of the table physically.

聚集索引就是说索引和数据是在一起的。一般的索引是单独的一个数据结构,而数据是一个数据结构;而聚集索引是说这两块内容是在一起的,并且是有顺序的。

参考官网:

14.6.2.1 Clustered and Secondary Indexes
Every InnoDB table has a special index called the clustered index where the data for the rows is stored. Typically, the clustered index is synonymous with the primary key. To get the best performance from queries, inserts, and other database operations, you must understand how InnoDB uses the clustered index to optimize the most common lookup and DML operations for each table.
每一个 InnoDB 表都会有一个特别的索引,被称作聚簇索引,存储了行的数据。聚簇索引是主键的同义词。
When you define a PRIMARY KEY on your table, InnoDB uses it as the clustered index. Define a primary key for each table that you create. If there is no logical unique and non-null column or set of columns, add a new auto-increment column, whose values are filled in automatically.
If you do not define a PRIMARY KEY for your table, MySQL locates the first UNIQUE index where all the key columns are NOT NULL and InnoDB uses it as the clustered index.
MySQL 会自己选择一个唯一索引来作为主键,如果你没有定义主键的话。
If the table has no PRIMARY KEY or suitable UNIQUE index, InnoDB internally generates a hidden clustered index named GEN_CLUST_INDEX on a synthetic column containing row ID values. The rows are ordered by the ID that InnoDB assigns to the rows in such a table. The row ID is a 6-byte field that increases monotonically as new rows are inserted. Thus, the rows ordered by the row ID are physically in insertion order.

上文的摘抄讲了 MySQL 是如何使用聚集索引的。

光看上文,还是有些没有搞明白,参考《高可用 MySQL》,找到这张图:
在这里插入图片描述
从这张图就可以看得比较明白了,叶子节点包含所有数据,而非叶子节点里面存在的是索引,这样其实数据和索引在一个结构里面,这就是聚簇索引。

参考

http://www.mysqltutorial.org/mysql-index/mysql-clustered-index/
https://dev.mysql.com/doc/refman/5.7/en/innodb-index-types.html