Still I do not know MySQL index? This is time to get to know the complete B- and B + Tree Tree

Foreword

Read a lot of blog on the index, say much the same. But never let me understand some of the concepts of the index, such as the B-Tree index, Hash index, the only index .... Maybe there are a lot of people like me, I began to study B-Tree, B + Tree structures such as no clear concept, leading to answer the question during the interview!

What indexes are?

MySQL index is to help efficiently get the data structure of the data.

Index can you do?

Improve the efficiency of data queries.

Index: sorted quickly find data structures! Index might affect the sort of behind where to find, and order by the latter.

First, the index of classification

1. divided up from the storage structure: BTree index (or B + Tree index the BTree), Hash index, full-index full-text index, R-Tree index.

2. From the application level to points: General index, the only index, a composite index.

The logical-physical order of the key data (index) sequence relationship: clustered index, non-clustered index.

1) described in the form is saved when the index store,

2) is an index using the classification process, the two are divided on different levels. But usually we talk about the type of index generally refers to application-level division.

Like the phone classification, as Android phone, IOS phone and Huawei cell phone, Apple phone, OPPO phone.

  • Common Index: i.e. only contains a single column index, a table can have multiple separate index

  • The only index: value of the index columns must be unique, but allow free value

  • Composite Index: i.e. an index comprising a plurality of columns

  • Clustered index (clustered index): not a single index type, but a data storage. Depending on implementation details, InnoDB clustered index is actually stored in the same B-Tree index in a structure (technically a B + Tree) and data lines.

  • Non-clustered index: not a clustered index is non-clustered index (serious face).

Second, the realization of the underlying index

mysql innodb default storage engine only explicit support B-Tree (B + Tree is technically) index for frequently accessed tables, innodb transparently establish adaptive hash index, i.e. hash index established on the basis of the B-tree index, can significantly improve search efficiency, the client is transparent, uncontrollable, implicit.

Aside storage engines, only discuss the implementation of (abstract)

Hash index

Based on a hash table implementations, only exact matches the query index to be effective for all columns for each row of data, the storage engine are calculated for all the indexed columns a hash code (hash code), and all the hash index Hash code storage in the index, while preserving each data row pointer in the index table.

v2-7bec3683f56acb9b780b1fa6f7265bb6_hd.png

B-Tree can speed up data access speed, because the engine no longer need to store a full table scan to obtain data, the data distributed among the various nodes.

v2-274de1de0624e25931fcaa212876d292_hd.png

B-Tree is an improved version of the index database also stores the index structure employed. Data on the leaf nodes, and increases the sequential access pointer, each leaf node points to the address of the adjacent leaf node. Only two nodes when compared to find B-Tree, the range searches, can be traversed. B-Tree and the need to obtain all of the nodes, compared to B + Tree higher efficiency.

v2-3c6d81011ba7629234be88196502c358_hd.png

Example: Say you have a student table, id primary key

v2-1c1b57f765695925a0fb3d956ee90278_hd.png

Implemented in MyISAM engine (secondary index is achieved by a)

v2-3417d2b80be23998b98354816af3203f_hd.jpg

Implemented in InnoDB

v2-66a7f360f8fcb5b47d224814e043e823_hd.png


v2-06d7b7779ce689e333730670271d5ba6_hd.png


ISSUES

Q: Why is the index structure using the default B-Tree, rather than hash, binary tree, red-black tree?

hash: Although you can quickly locate, but there is no order, IO high complexity.

Binary tree: highly non-uniform tree, not self-balancing, search efficiency with data (height of the tree) related to IO and high costs.

Red-black tree: tree height increases as the amount of data increases, the high cost of IO.

Q: Why is the official recommended to use self-growth as the primary key index.

B + Tree binding characteristics, auto-increment primary key is continuous, during insertion to minimize page splitting, to be split even pages, and only a small portion split. And can reduce the movement of data, is inserted into each insertion end. In short, it is to reduce the frequency division and movement.

Interpolating successive data:


v2-97e1fc32d8b1c5ec14e827eb619795b6_hd.gif

Inserting the non-continuous data



v2-404a9ec45e97550a9b1b8c5e9c6b766a_hd.gif

Finally, we welcome the exchange, like the point of a praise yo remember the article, thanks for the support!



Guess you like

Origin blog.51cto.com/14442094/2433452