Typically, an index is a separate data structure such as B-Tree that stores the key values used for faster lookups.
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.
Once a clustered index is created, all rows in the table will be stored according to the key columns used to create the clustered index.
Because a clustered index store the rows in sorted order, each table have only one clustered index.
1.聚簇索引是物理索引,数据在表里是按顺序存储的,物理上是连续的,一般选主键id作为聚簇索引,且一张表里只能有一个聚簇索引。
2.只有InnoDB支持聚簇索引。
3.非聚簇索引是逻辑索引,将数据的某个字段抽取出来组成独立的B-Tree,原数据的存储顺序可能不是物理连续的。
4.对于非聚簇索引,索引字段越紧凑,每页可以存储的索引越多,单次IO可以读到的信息越多,查询效率越高。
If you do not have a primary key for a table, MySQL will search for the first UNIQUE
index where all the key columns are NOT NULL
and use this UNIQUE
index as the clustered index.
In case the InnoDB table has no primary key or suitable UNIQUE
index, MySQL internally generates a hidden clustered index named GEN_CLUST_INDEX
on a synthetic column which contains the row ID values.
5.一般地,InnoDB把 primary key作为聚簇索引,没有指定 primary key,则MySQL将唯一索引作为聚簇索引,如果也没有唯一索引,MySQL将创建一个隐藏的索引GEN_CLUST_INDEX。
6.聚集索引就是以主键创建的索引,非聚集索引就是以非主键创建的索引,聚集索引的叶子节点存储的是表中的数据,非聚集索引的叶子节点存储的是主键和索引列,拿到叶子节点上的存储的主键再去查询想要查询的数据,这个过程叫做回表。