Innodb和MyIsam在B+樹中的區別是什麼?

可以先了解下B+樹

說起索引就引出了聚集索引非聚集索引,它們的區別是什麼呢?

葉節點存放一整行記錄的索引聚集索引,反之,叫非聚集索引

Innodb

  • 按照主鍵B+樹的排列方式存放,子節點存放的就是數據。(如果沒有主鍵,以第一列爲聚集索引)
  • 只有一個聚集索引。
  • 普通索引指向聚集索引。
  • 葉子節點存放的是:除該索引外的一整行數據

有一個人員表:主鍵爲id,列爲 name(索引),class,age。那麼他們的存儲方式爲:
數據爲:
在這裏插入圖片描述
聚集索引爲

在這裏插入圖片描述
而也可能會有很多普通索引,普通索引的子節點指向聚集索引。例如當普通索引通過xiaohuang查到id爲1後,再到聚集索引中繼續查詢到子節點,也就是說通過普通索引查詢會調用兩次索引。

在這裏插入圖片描述

MyIsam

  • 普通索引和非聚集索引沒什麼區別。
  • 葉子節點存放的是:磁盤地址

在這裏插入圖片描述

下面是聚集索引規則,感興趣的同學,可以看下

InnoDB選取聚集索引規則:

英文文檔:

    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.

    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.

    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.

    How the Clustered Index Speeds Up Queries
    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.

    How Secondary Indexes Relate to the Clustered Index
    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.

    If the primary key is long, the secondary indexes use more space, so it is advantageous to have a short primary key.

    For guidelines to take advantage of InnoDB clustered and secondary indexes, see Section 8.3, “Optimization and Indexes”.

    InnoDB中有一個6字節的隱藏字段ROWID。 如果有主鍵(或者未建立主鍵但是有非空唯一索引,該列即爲主鍵),則rowid的值與主鍵的值相等,並以主鍵構造聚集索引。 否則使用rowid(將其設置爲自增)作爲主鍵,並在rowid上建立聚集索引 GEN_CLUST_INDEX 。

翻譯如下:

每個InnoDB表都有一個特殊的索引,稱爲聚集索引,用於存儲行數據。通常,聚集索引是主鍵的同義詞。要從查詢、插入和其他數據庫操作中獲得最佳性能,您必須瞭解InnoDB如何使用聚集索引來優化每個表最常見的查找和DML操作。
當你在表上定義一個主鍵時,InnoDB使用它作爲聚集索引。爲創建的每個表定義一個主鍵。如果沒有邏輯惟一且非空的列或列集,則添加一個新的自動遞增的列,其值將自動填充。
如果你沒有爲你的表定義一個主鍵,MySQL找到第一個唯一的索引,其中所有的鍵列不是空的,InnoDB使用它作爲聚集索引。
如果表沒有主鍵或合適的唯一索引,InnoDB內部會在包含行ID值的合成列上生成一個名爲GEN_CLUST_INDEX的隱藏聚集索引。這些行是按照InnoDB給表中的行分配的ID排序的。行ID是一個6字節的字段,在插入新行時單調增加。因此,按行ID排序的行在物理上是按插入順序排列的。
聚集索引如何加速查詢
通過聚集索引訪問一行非常快,因爲索引搜索直接指向包含所有行數據的頁面。如果表很大,與使用與索引記錄不同的頁存儲行數據的存儲組織相比,聚集索引體系結構通常會節省一次磁盤I/O操作。
二級索引如何與聚集索引關聯
聚集索引以外的所有索引都稱爲二級索引。在InnoDB中,輔助索引中的每條記錄都包含該行的主鍵列,以及爲輔助索引指定的列。InnoDB使用這個主鍵值在聚集索引中搜索行。
如果主鍵很長,輔助索引會使用更多的空間,所以使用短的主鍵比較有利。
關於如何利用InnoDB聚集和二級索引的指導,請參見8.3節“優化和索引”。
InnoDB中有一個字6節的隱藏字段ROWID。如果有主鍵(或者未建立主鍵但是有非空唯一索引,該列即爲主鍵),則ROWID的值與主鍵的值相等,並以主鍵構造聚集索引。否則使用ROWID(將其設置爲自增)作爲主鍵,並在ROWID上建立聚集索引GEN_CLUST_INDEX。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章