当前位置:网站首页>Dig deep into MySQL - resolve the difference between clustered and non clustered indexes

Dig deep into MySQL - resolve the difference between clustered and non clustered indexes

2022-06-24 23:09:00 Little Camellia girl

All are B+ The data structure of the tree , The leaf node of cluster index is the data node . The leaf node of the non clustered index is still the index node , There is only one pointer to the corresponding data block .

Cluster index : Put data storage and index together , The leaf node of the index structure holds the row data

Nonclustered index : Separate data from index , The leaf node of the index structure points to the corresponding position of the data

Cluster index is an index whose sequential structure is consistent with the physical structure of data storage , And the clustered index of a table can only have a unique one ; A non clustered index corresponding to a table can have multiple entries , Non clustered indexes with different requirements can be established according to the constraints of different columns ;

advantage
1、 The data can be directly obtained through the cluster index , Compared with non clustered indexes, back to table is required ( Except overlay index ) Be efficient
2、 Cluster index is very efficient for range query , Because its data is arranged by size
3、 Cluster index is suitable for sorting , Nonclustered indexes are not suitable for .

InnoDB There must be a primary key in , The primary key must be a clustered index , Do not set it manually , Will use a unique Index as primary key index , No, unique Indexes , A hidden row inside the database will be used id As a primary key index . An index created on top of a clustered index is called a secondary index , Secondary index access data always needs secondary search , Non clustered indexes are secondary indexes , Like composite index , Prefix index 、 unique index . The secondary index leaf node no longer stores the physical location of the row , It's the primary key value .

InnoDB The leaf node of the secondary index stores KEY Field plus primary key value . therefore , Through the secondary index query, the primary key value is found first , then InnoDB Then find the corresponding data block through the primary key index according to the primary key value . and MyISAM The secondary index leaf node of is a combination of column value and row number , The leaf node holds the physical address of the data . So it can be seen that MYISAM There is no difference between primary key index and secondary index , The primary key index is just one called PRIMARY The only 、 Non empty index , And MYISAM No primary key can be set in the engine .

原网站

版权声明
本文为[Little Camellia girl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211145087893.html