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 做为存储引擎的表都有一个特殊的索引称为汇集索引,它保存着每一行的数据,一般,汇集索引就是主键索引。为了获得更高效的查询、插入以及其余的数据库操做的性能,你必须理解 InnoDB 引擎是如何使用汇集索引来优化常见的查找和 DML 操做。ide
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.
若是你的表定义了一个主键,InnoDB 就使用它做为汇集索引。所以,尽量的为你的表定义一个主键,若是实在没有一个数据列是惟一且非空的能够做为主键列,建议添加一个自动递增列做为主键列。性能
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.
若是你的表没有定义主键,InnoDB 会选择第一个惟一非空索引来做为汇集索引。优化
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.
若是你的表既没有主键,又没有合适的惟一索引,InnoDB 内部会生成一个隐式汇集索引 —— GEN_CLUST_INDEX,该索引创建在由 rowid 组成的合成列上。数据行根据 InnoDB 分配的 rowid 排序,rowid 是一个 6 字节的字段,随着数据插入而单调递增。也就是说,数据行根据 rowid 排序其实是根据插入顺序排序。ui
Accessing a row through the clustered index is fast because the index search leads directly to the page with all the row data. If a table is large, the clustered index architecture often saves a disk I/O operation when compared to storage organizations that store row data using a different page from the index record.
经过汇集索引来访问一行数据是很是快的,这是由于全部的行数据和索引在同一页上。若是表特别大,相较于行数据和索引在不一样页上存储结构(好比 myisam 引擎),这将大大节省磁盘 I/O 资源。this
All indexes other than the clustered index are known as secondary indexes. In InnoDB, each record in a secondary index contains the primary key columns for the row, as well as the columns specified for the secondary index. InnoDB uses this primary key value to search for the row in the clustered index.
除了汇集索引外的其余索引类型都属于二级索引。在 InnoDB 中,二级索引中的每一个记录都包含该行的主键列,以及二级索引指定的列;汇集索引中,InnoDB 经过主键值来查询数据行。spa
If the primary key is long, the secondary indexes use more space, so it is advantageous to have a short primary key.
若是主键过长,二级索引就须要更大的空间,所以,使用短的主键列是颇有利的。orm
对于二级索引,叶子节点并不包含行记录的所有数据。叶子节点除了包含键值之外,每一个叶子节点中的索引行中还包含了一个书签 —— 相应行数据的汇集索引键。排序
若是在一棵高度为 3 的二级索引树中查找数据,那须要对这颗二级索引树遍历3次找到指定汇集索引键。若是汇集索引树的高度一样为 3 ,那么还须要对汇集索引树进行 3 次查找,最终找到一个完整的行数据所在的页,所以一共须要 6 次逻辑 IO 访问以获得最终的一个数据页。索引