Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why MongoDB index chooses B-tree and Mysql index chooses B + tree

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Why MongoDB index chooses B-tree and Mysql index chooses B + tree". In daily operation, I believe many people have doubts about why MongoDB index chooses B-tree and Mysql index chooses B + tree. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer "Why MongoDB index chooses B-tree and Mysql index chooses Btree". Next, please follow the editor to study!

I. the difference between B-tree and B + tree

Obviously, if we want to find out why, we need to know the difference between B-trees and B + trees. In order not to make a long speech. We give their forms directly and summarize their characteristics.

1. B-tree

B-tree is a self-balanced search tree with a simple form:

This is a B-tree. The core features of our problem are as follows:

(1) Multipath, non-binary tree

(2) each node saves both index and data.

(3) search is equivalent to binary search.

Here we assume that we have already understood the structure related to the B-tree.

2. B + tree

B + tree is a variety of B-tree.

The core features are as follows:

(1) Multi-channel non-binary

(2) only leaf nodes save data.

(3) search is equivalent to binary search.

(4) the pointing pointer of the adjacent contact is added.

From the above, we can see that there are two core differences, one is the location of the data, and the other is the direction of the adjacent nodes. It is these two that make the difference between MongoDB and Mysql. Why?

3. The difference between B-tree and B + tree.

The main results are as follows: (1) the time complexity of B+ tree query is fixed, and the best query complexity of logn,B- tree is O (1).

(2) the pointers of the adjacent nodes of the B + tree can greatly increase the accessibility of the interval, and can be used in the range query, etc., while the key and data of each node of the B-tree can not be found in the interval.

(3) B + tree is more suitable for external storage, that is, disk storage. Since there is no data domain in the inner node, each node can index a larger and more accurate range.

(4) it is very important to note that this difference is based on (1) (2) (3). Each node of the B-tree saves both data and index, so the number of disk IO is very small, the B+ tree is saved only by leaf nodes, disk IO is more, but interval access is better.

With the difference between them, it would be much better for us to explain this reason now.

II. Explanation of the reasons

To explain why, we must also understand the basic concepts of MongoDB and Mysql.

1 、 MongoDB

MongoDB is a document-based database, a nosql that saves data in a Json-like format. For example, before our table may have a user table, order table, shopping basket table, etc., but also to establish a foreign key relationship between them. But Json-like is different.

We can see that this form is simpler and easier to understand. So why does MongoDB use B-trees?

MongoDB uses a B-tree, and all nodes have a data field, which can be accessed as long as the specified index is found. There is no doubt that a single query is faster than Mysql on average.

2 、 Mysql

Mysql as a relational database, the relevance of data is very strong, interval access is a common situation. Because the data of B+ tree are all stored in leaf nodes and strung together by pointers, it is easy to traverse intervals or even all of them.

The core of these two differences is easy to understand if you can understand the difference between B-trees and B + trees.

At this point, the study on why the MongoDB index chooses the B-tree and the Mysql index chooses the B + tree is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report