[MySQL] MySQL indexing question of interpretation of the underlying implementation

1, why not Hash table as an index?

Hash table range query is difficult, such as select * from sanguo where id> 10;

2, why not balanced binary tree as the index?

Although balanced binary support range queries, but that this data structure to find the range to be looking back, that is, back to the parent node, while the efficiency of the leaf node pointer B + tree is even higher.

3, Why B-tree structure of a plurality of node storage elements?

Because the database is stored in the index file, reads the contents of the file but also for disk I / O operations, common node of the tree is only one element, the number of times a lot of disk I / O, and a plurality of tree node B structure of the data reduces the number of disk I / O, improve search efficiency.

4, the principle of locality data disk access

Some data is removed, then around the data will be used.

5, the operating system for storing data units

The operating system is accessed according to data in page units, a default is 4KB.

6, a node how many elements which should exist? Or that a node should be?

Because the operating system is accessed by a page unit, so in order to avoid waste of data, the size of a node to be an integer multiple of the page. A MySQL database size is 16KB, so a node should be four.

7, MySQL B + tree Why not non-leaf nodes store the data?

MySQL size of a Node B + tree just to the size of a database, if the data is stored, then the index number storage will be reduced, thus contributing to the whole teeth B + number increased, thereby increasing the disk I / O times reduce the search efficiency.

8, the difference between the primary key index MyISAM and InnoDB are B + tree?

MyISAM B + tree index stored in the leaf node is the address of the data, and the leaf nodes InnoDB engine in the B + tree is stored directly in the data, thus reducing a disk I / O operations.

9, secondary index and secondary indexes MyISAM and InnoDB difference between the B + tree structure?

Same secondary indexes MyISAN storage structure with the primary key index, and leaf nodes InnoDB secondary index B + tree is not all data is stored, but stored primary key for each row of data, if not create a primary key table, InnoDB on their own create a hidden primary key storage defaults.

10, the default value of a MySQL Why is 16KB?

Suppose a row of data is 1KB size, the B + a leaf node of the tree can be stored 16 lines (16KB / 1KB) data, the non-leaf node only stores the index values ​​and pointers, the primary key index default bigInt type (8B) in the InnoDB, a pointer size 6B, so a non-leaf node may store 1170 pairs index + pointers, B + tree height is 2, the number of the leaf node is 117,016 data, when the height is 3, may store 11701170 * 3 data. A 16KB of data is sufficient.

11, joint index storage

A plurality of joint index index is spliced ​​together to construct the B + tree.

12, the most left-prefix principles

If the field in the table for a, b, c build joint index, and the order of a, b, c.

select * from table where a = 1 ;
select * from table where a = 1 and b = 1;
select * from table where a = 1 and b = 1 and c = 1;
select * from table where a = 1 and c = 1;

With the above fields query sql statement will use the index, but the last sql statement is slightly different, although according to a, c field joint inquiry, but it only uses a index, the index will use the results equivalent to the first sentence case, that is c = 1 and did not use the joint index.

And b, bc, c field alone will not use the joint index.

13, can be used to determine how the index?

If you use the Node B + tree can help narrow your search, you can use the index, if the query can not use the Node B + trees to narrow your search, it is less than the index. Therefore, the joint index query leftmost prefix to follow the principle, if not the most left-prefix principles, then less than joint index.

Guess you like

Origin blog.csdn.net/renjingjingya0429/article/details/89364986