MySQL InnoDb中的 Clustered Index 和 Secondary Index

本文详细介绍了MySQL InnoDB存储引擎中的两种主要索引类型:聚簇索引(Clustered Index)和二级索引(Secondary Index)。聚簇索引基于主键组织数据,对性能至关重要;二级索引则用于加速非主键列的查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值