MySQL InnoDb中的 Clustered Index 和 Secondary Index

MySQL InnoDb中的 Clustered Index 和 Secondary Index

clustered index

The InnoDB term for a primary key index. InnoDB table storage is organized based on the values of the primary key columns, to speed up queries and sorts involving the primary key columns. For best performance, choose the primary key columns carefully based on the most performance-critical queries. Because modifying the columns of the clustered index is an expensive operation, choose primary columns that are rarely or never updated.

In the Oracle Database product, this type of table is known as an index-organized table.


clustered index其实就是promary key的另外一种说法。

MySQL InnoDb中的索引分为Clustered Index (聚簇索引)和 Secondary Index(二级索引)


Clustered Index:
每一个InnoDB表都有一个特殊的索引,叫做clustered index,通常来讲,clustered index和primary key是同一个意思,InnoDB选择clustered index原则如下:

-如果表上定义了primary key,则使用primary key作为clustered index
-如果没有定义primary key,选择第一个非空的UNIQUE索引作为clustered index。所以,如果表只有一个非空的UNIQUE索引,那么InnoDB就把它当作主健了。
-如果即没有primary key,也没有合适的UNIQUE索引,InnoDB内部产生一个隐藏列,这个列包含了每一行的row ID, row ID随着新行的插入而单调增加。然后在这个隐藏列上建立索引作为clustered index。

Secondary Index:
除了Clustered Index之外的索引都是Secondary Index,每一个Secondary Index的记录中除了索引列的值之外,还包含主健值。通过二级索引查询首先查到是主键值,然后InnoDB再根据查到的主键值通过主键/聚簇索引找到相应的数据块。

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

猜你喜欢

转载自blog.csdn.net/jolly10/article/details/80854892