search
HomeDatabaseMysql TutorialWhat is the difference between B-tree index and B+-tree index in MySQL?

If you use a tree as the index data structure, each time you search for data, one node of the tree will be read from the disk, which is a page. However, each node of the binary tree only stores one piece of data and cannot fill a page. storage space, wouldn’t the extra storage space be wasted? In order to solve this shortcoming of the binary balanced search tree, we should look for a data structure that can store more data in a single node, that is, a multi-way search tree.

1. Multi-path search tree

1. Complete binary tree height: O(log2N), where 2 is the logarithm, the number of nodes at each level of the tree;

2. The height of the complete M-way search tree: O(logmN), where M is the logarithm, the number of nodes at each level of the tree;

M-way search tree is suitable for Scenarios where the amount of stored data is too large to be loaded into memory at once. Store more data in one layer by increasing the number of nodes per layer and storing more data in each node, thereby reducing the height of the tree and reducing the number of disk accesses during data lookup.

Therefore, if the number of keywords each node contains and the number of nodes per level increase, the height of the tree will decrease. Although the data determination of each node will be more time-consuming, the focus of B-tree is to solve the disk performance bottleneck, so the cost of searching data on a single node can be ignored.

2. B-tree - multi-path balanced search tree

B tree is an M-way search tree. B-tree is mainly used to solve the problem of unbalanced M-way search tree and the height change of the tree. High, which is the same as the performance problem caused by the degeneration of binary trees into linked lists. B-tree ensures the balance of the M-way search tree by controlling and adjusting the nodes of each layer, such as node separation, node merging, splitting parent nodes upward when one layer is full to add new layers, and other operations.

In the B-tree, each node is a disk block (page), and the order or path number M specifies the maximum number of child nodes in the node. Each non-leaf node stores keywords and pointers to subtrees. The specific number is: M-order B-tree, each non-leaf node stores M-1 keywords and M pointers to subtrees. The picture shows a schematic diagram of a 5-order B-tree structure:

What is the difference between B-tree index and B+-tree index in MySQL?

3. B-tree index

First create a user table:

create table user(
	id int,
	name varchar,
	primary key(id)
) ROW_FORMAT=COMPACT;

If we use a B-tree to index user records in the table:

What is the difference between B-tree index and B+-tree index in MySQL?

Each node of the B-tree occupies one Disk blocks are also pages. As can be seen from the above figure, compared with the balanced binary tree, each node of the B-tree stores more primary keys and data, and each node has more child nodes. The number of nodes is generally called the order. The B-tree in the above figure is a third-order B-tree, and the height will also be reduced. If we want to find the user information of id=28, then the search process is as follows:

1. Find page 1 according to the root node and read it into the memory. [Disk I/O operation 1st time]

2. Compare the key value 28 in the interval (17,35) and find the pointer p2 of page 1;

3. Find the page based on the p2 pointer 3. Read into memory. [Disk I/O operation 2nd time]

4. Compare the key value 28 in the interval (26,35) and find the pointer p2 of page 3.

5. Find page 8 according to the p2 pointer and read it into the memory. [Disk I/O operation 3rd time]

6. Find the key value 28 in the key value list on page 8. The user information corresponding to the key value is (28,po);

B-Tree Compared with AVLTree, the number of nodes is reduced, so that the data fetched from the memory every time the disk I/O is used, thereby improving the query efficiency.

4. B-tree index

B Tree is an optimization based on B-Tree, making it more suitable for implementing external storage index structure. The properties of B-tree:

1. The subtree pointers of non-leaf nodes are the same as the number of keywords;

2. Add a link pointer to all leaf nodes;

3. All keywords are in leaf nodes appears, and the keywords in the linked list happen to be in order;

4. Non-leaf nodes are equivalent to the indexes of leaf nodes, and leaf nodes are equivalent to the data layer that stores (keyword) data;

InnoDB storage engine uses B Tree to implement its index structure.

B-In the tree structure diagram, it can be seen that each node also contains data values ​​in addition to the key value of the data. The storage space of each page is limited. If the data data is large, the number of keys that can be stored in each node (i.e. one page) will be very small. When the amount of stored data is large, it will also lead to B- The depth of Tree is larger, which increases the number of disk I/Os during query, thereby affecting query efficiency. In B Tree, all data record nodes are stored on leaf nodes of the same layer in order of key value. Only key value information is stored on non-leaf nodes. This can greatly increase the number of key values ​​stored in each node. Reduce the height of B Tree.

B Tree has several differences compared to B-Tree:

1. Non-leaf nodes only store key value information and pointers to child node page numbers;

2. There is a link pointer between all leaf nodes;

3. Data records are stored in leaf nodes;

What is the difference between B-tree index and B+-tree index in MySQL?

Based on the above figure, let’s take a look at the difference between B-tree and B-tree:

(2) In B-tree, non-leaf Nodes only store key values, while B-tree nodes store both key values ​​and data.

The page size is fixed, and the default page size in InnoDB is 16KB. If data is not stored, more key values ​​will be stored, the order of the corresponding tree will be larger, and the tree will be shorter and fatter. As a result, the number of disk IO times we need to search for data will be reduced again. Data query efficiency will also be faster.

In addition, if one node of our B-tree can store 1000 key values, then the 3-layer B-tree can store 1000×1000×1000=1 billion pieces of data. Generally, the root node is resident in memory (there is no need to read the disk when retrieving the root node for the first time), so generally we only need 2 disk IOs to search for 1 billion data.

(2) All data in the B-tree index are stored in leaf nodes, and the data is arranged in order.

B The pages in the tree are connected through a two-way linked list, and the data in the leaf nodes are connected through a one-way linked list. In this way, all the data in the table can be found. B-trees make range queries, sort queries, group queries, and deduplication queries extremely simple. In B-tree, because the data is scattered in various nodes, it is not easy to achieve this.

The above is the detailed content of What is the difference between B-tree index and B+-tree index in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.