Summary of interview routine questions 6

1. Why does the database use B+ tree instead of B tree

The difference between B tree and B + tree

There are two main differences between B-tree and B+ tree:

  1. In a B-tree, you can store keys and values ​​in internal nodes and leaf nodes, but in a B+ tree, all internal nodes are keys and have no values. Leaf nodes store both keys and values
  2. The leaf nodes of the B+ tree are connected by a chain, while the leaf nodes of the B tree are independent.

Benefits of using B+ trees


1. Since the internal nodes of the B+ tree only store keys and do not store values, more keys can be obtained in the memory page in one read, which is conducive to narrowing down the search range faster.
2. The leaf nodes of the B+ tree are connected by a chain. Therefore, when a full data traversal is required, the B+ tree only needs to use O(logN) time to find the smallest node, and then perform O(N) order through the chain It can be traversed. The B-tree needs to traverse each layer of the tree, which will require more memory replacement times, so it will take more time

3. B-tree can only perform random retrieval, while B+ tree can perform random retrieval and sequential retrieval. (Because the leaf nodes of the B+ tree are connected by a chain)

4. The B+ tree query is more stable. The closer the data of the B tree is to the root node, the faster the query is. The data of the B+ tree is always in the leaf node.

5. B+ tree can conveniently perform range query, only need to traverse the leaf nodes, but B tree can not

6. B+ tree can improve the speed of adding and deleting data (because the data of B+ tree is stored in an ordered linked list structure)

The advantage of using B-trees is that
B-trees can store both keys and values ​​in internal nodes. Therefore, placing frequently accessed data close to the root node will greatly improve the query efficiency of hot data. This feature makes B-trees more efficient in scenarios where specific data is repeatedly queried.

Why does the database use a B+ tree instead of a B tree
? Because it is the benefit of the B+ tree mentioned above. The data reading of the database requires expensive disk IO operations. Therefore, the focus of the database is to narrow the range faster and reduce the number of reads. The B+ tree is better than the B tree at these points. This is why the database chooses B+ tree as the underlying implementation.
 

Guess you like

Origin blog.csdn.net/songkai558919/article/details/122304566