B + tree index underlayer constituted MySQL

(Read should have a basic understanding of the structure and principles of InnoDB index page, image source "MySQL is how it works")

InnoDB primary key B + tree index structure

  

 Simple explanation  

  (1) can be seen, InnoDB B + tree index node is InnoDB data page, by the nodes in the File Header Previous, Next so connected into a doubly linked list;

  (2) B + tree only leaf nodes only store data, the recording head RECORD_TYPE field of non-leaf nodes are set to 1, record_type field of the record leaf node is 0 (except system inserted maximum recorded minimum recording);

  (3) only two fields of the non-leaf node is valid: the minimum primary key id Page number + page number recorded (note in FIG red portion). Thus, according to the investigation's target record id you can find it belongs to take a page, and then find the most within the page based on the primary key to check the record-half

      Similar slot number, slot number after the recording by the group of digits is recorded, and can traverse directly.

  (4) secondary index, or called secondary index is a similar structure, just to find according to the master key changed according to the index field + primary key to find the node and has (plus primary key is not only to visit the primary key index, but also to ensure the non-leaf nodes in the uniqueness of records, because the index field may be repeated).

      Secondary index leaf node all data is not recorded, and only the primary key index field, to determine the need to take the primary key to find the target returning the primary key index.

  (5) as c1, c2 establish joint index, which is the secondary index. Priority sorted by the left of the field when indexing, ie c1. When the same field c1, c2 then indexed according to these same fields in the node in the series of successive c1. This principle is the principle of the left prefix.

 

Different points B and B + tree tree and thinking

  (1) We know that usually a few KB of data to move from disk to memory handling, avoid frequent disk IO. So InnoDB select a page as a node, a node into the run-time memory, and then binary search in memory, to find the corresponding record traversal. Each node in the tree for each B

     Record is complete with a row of data, which led to the number of records that can be stored in a node becomes smaller, it means that a node open fork becomes a lot less, it means that the entire index tree bigger height, which is obviously not good. Based on this, B + tree so that all the data saved to

                Leaf node, the other nodes do not store data, each non-leaf node bifurcation greatly increased the cost is the time to find all must come to a leaf node. B + tree has four layers can be present lot record, 5-6 layer is the limit.

 

  Therefore, the data (2) B + tree to the leaf node are stored, as well as bi-directional pointer to the nodes consisting of doubly linked list, it is to find a suitable range.

 

Guess you like

Origin www.cnblogs.com/shen-qian/p/12661835.html