mysql-B+tree index InnoDB

Basic introduction to B+ tree (balance balance-height difference is less than 2)

B+ trees are an extension of B trees that allow efficient insertion, deletion, and search operations.

In B-trees, both keys and records can be stored in internal nodes and leaf nodes. However, in a B+ tree, records (data) can only be stored on leaf nodes, and internal nodes can only store key values.

The leaf nodes of the B+ tree are linked together in a singly linked list to make search queries more efficient.

B+ trees are used to store large amounts of data that cannot be stored in main memory. Due to the fact that the size of the main memory is always limited, the internal nodes of the B+ tree (keys to access records) are stored in the main memory, while the leaf nodes are stored in the auxiliary memory.

The internal nodes of the B+ tree are usually called index nodes

Those with child nodes are internal nodes, and those without child nodes are leaf nodes

Introduction to the relationship between disk storage and innodb

When the system reads data from the disk to the memory, the basic unit is the disk block (block) . The data located in the same disk block will be read out at one time, rather than what is needed.

The InnoDB storage engine has the concept of a page (Page), which is the smallest unit of its disk management . The default size of each page in the InnoDB storage engine is 16KB, and the page size can be set to 4K, 8K, or 16K through the parameter innodb_page_size

However, the storage space of one disk block in the system is often not so large, so each time InnoDB applies for disk space, it will use several disk blocks with consecutive addresses to achieve a page size of 16KB. InnoDB uses pages as the basic unit when reading disk data into memory. When querying data, if each piece of data in a page can help locate the location of the data record, this will reduce the number of disk I/Os. Improve query efficiency

B+tree detailed introduction

  1. Non-leaf nodes only store key-value information;

  1. There is a chain pointer between all leaf nodes, which can improve the efficiency of range queries;

  1. Data records are stored in leaf nodes;

  1. All leaf nodes (that is, data nodes) are a single linked list structure. Therefore, two search operations can be performed on B+Tree: one is range search and page search for the primary key, and the other is random search starting from the root node.

B tree and B + tree comparison

INNODB

InnoDB使用了聚簇索引(Clustered),即所有二级索引聚集在主键索引上,对InnoDB存储引擎表的任何访问,最终一定要搜索主键索引树

在InnoDB中,二级索引上没有实际的数据,存储的是主键索引的值。这样的话,如果是基于二级索引的查询,会先在二级索引上搜索得到主键索引的值,然后再去主键索引树上搜索,得到最终的行数据。

这就意味着,至少有一次索引查找,可能会有两次索引查找,其中一定有一次主键索引查找

在InnoDB中,主键要设计的尽量主键越小二级索引也会越小

如果主键用更大的数据类型,由于二级索引上有主键索引的值,那么不只是主键索引树变的更大更高,其他的二级索引树也会更大更高

二级索引:所有不是主键索引的索引

树高度决定因素

阶树:描述一颗 B 树时需要指定它的阶数,阶数表示了一个节点最多有多少个孩子节点,一般用字母 m 表示阶数,阶树也决定了树的高度

根节点的关键字数量范围:1 <= k <= m-1,非根节点的关键字数量范围:m//2 <= k <= m-1,超出范围就会分裂新的内部节点。

树高度越小,查询io操作就会越少,读写也就越快

Guess you like

Origin blog.csdn.net/xiaofeixia666888/article/details/129753460