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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章