Table of Contents
Preface
Index query
Why not use a binary tree
Why not use Hash table
B-tree
Operation process
Insertion
Home Database Mysql Tutorial Finally understand that MySQL index needs to use B+tree, and it's so fast

Finally understand that MySQL index needs to use B+tree, and it's so fast

Nov 18, 2020 pm 05:36 PM
mysql index

mysql tutorial column introduces the B tree of understanding the index.

Finally understand that MySQL index needs to use B+tree, and it's so fast

Free recommendation: mysql tutorial(Video)

Preface

When you encounter a slow SQL and need to optimize it, what optimization method can you think of immediately?

Most people’s first reaction may be Add index. In most cases, index can add a query to a SQL statement. The efficiency is improved by several orders of magnitude.

Essence of index: A data structure used to quickly find records.

Commonly used indexesData structure:

  1. Binary tree
  2. Red-black tree
  3. Hash table
  4. B-tree (B tree is not called B minus tree)
  5. B tree

Data structure GraphicalWebsite: https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

Index query

Everyone knowsselect * from t where col = 88 If such a SQL statement is searched without using the index, the normal search is full table scan: search row by row starting from the first row of the table , comparing the value of the col field in each row with 88, this is obviously very inefficient.

Finally understand that MySQL index needs to use B+tree, and it's so fast

And if you use an index, the query process is completely different (assuming that a balanced binary tree data structure is used to store our index columns )

The storage structure of the binary tree at this time (Key - Value): Key is the data of the index field, and Value is the disk file address of the row where the index is located.

When 88 is finally found, you can take out the disk file address corresponding to its Value, and then go directly to the disk to find this line of data. The speed at this time It will be much faster than a full table scan.

Finally understand that MySQL index needs to use B+tree, and it's so fast

Butactually MySQL The bottom layer does not use binary tree to store index data, it uses B tree (B tree) .

Why not use a binary tree

Assuming that an ordinary binary tree is used to record the id index column, we must maintain the binary tree index field while inserting a row of records.

Finally understand that MySQL index needs to use B+tree, and it's so fast

At this time, when I want to find the data with id = 7, the search process is as follows:

Finally understand that MySQL index needs to use B+tree, and it's so fast

At this time, we searched for the row id = 7 7 times, which is not much different from our full table scan. Obviously, the binary tree is actually a data structure that is not suitable as an index for this increasing data column.

Why not use Hash table

Hash table: a data structure for fast search, the time complexity of search is O(1)

Hash function: convert a Any type of key can be converted into an int type subscript

Assuming that the Hash table is used to record the id index column, we insert a row of records at the same time Maintain Hash table index fields.

Finally understand that MySQL index needs to use B+tree, and it's so fast

At this time, the tree node with id = 7 was searched only 1 times, which is very efficient.

Finally understand that MySQL index needs to use B+tree, and it's so fast

But the index of MySQL stilldoes not use the Hash table that can be accurately positioned. Because it does not apply to range queries .

Why not use red-black tree

The red-black tree is a specialized AVL tree (balanced binary tree), which is maintained through specific operations during insertion and deletion operations. Balance of binary search trees;

If a binary search tree is a red-black tree, then any of its subtrees must be a red-black tree.

Assuming that the red-black tree record id index column is used at this time, we must maintain the red-black tree index field while inserting a row of records.

Finally understand that MySQL index needs to use B+tree, and it's so fast

During the insertion process, you will find that it is different from ordinary binary trees in that when the height difference between the left and right subtrees of a tree is > 1, it will spin Operation to keep the tree balanced.

At this time, the tree node with id = 7 was searched only 3 times, which is faster than the so-called ordinary binary tree.

Finally understand that MySQL index needs to use B+tree, and it's so fast

But MySQL’s index stilldoes not use which is excellent in precise positioning and range queryred-black tree.

Because when MySQL the amount of data is large, the size of the index will also be very large, and the memory may not be able to store it, so related reading and writing need to be done from the disk. If the tree level is too large If it is high, the number of disk reads and writes (I/O interactions) will be greater, and the performance will be worse.

B-tree

The only shortcoming of the red-black tree is that the height of the tree is uncontrollable, so now our entry point is the tree the height of.

Currently, a node is only allocated to store 1 element. If we want to control the height, we can allocate a larger space to a node and let it store multiple elements horizontally , at this time the height is controllable. Through such a transformation process, it became B-tree.

B-tree is an absolutely balanced multi-way tree. There are two concepts in its structure: Degree: the number of child nodes (subtrees) that a node has. (Some places use

degree
to explain

B-tree, explain it here) Order: the maximum number of child nodes of a node . (Usually represented by m

)

Keyword: data index.

An m-order

B-tree
is a balanced m-way search tree. It may be an empty tree, or it may meet the following characteristics:

Except for the root node and leaf nodes, each other node has at least
  1. ##m2 \lceil \dfrac{m}{2}\rceil##⌈

    ##⌈##m2\lceil \dfrac{m}{2}\rceilThe number of keywords j contained in each non-root node satisfies:

  2. m2 \lceil \dfrac{m}{2}\rceil#⌈

    All leaf nodes are located on the same layer.
  3. The meaning of the name (off topic, relax)
  4. The following is excerpted from Wikipedia

Rudolf Bayer ( Rudolf Bayer and Ed M. McCreight invented

B-tree

in 1972 while working at Boeing Research Labs, but they did not explain that B stood for What meaning (if any).

Douglas Comer explains: Neither author ever explained the original meaning of B-tree

. We might feel that balanced, broad or bushy might be appropriate. Others suggested the letter B stood for Boeing. Due to his sponsorship, however, it seems more appropriate to think of

B-tree as a Bayer tree.

Donald Knuth speculated on the

B-tree in his paper titled "CS144C classroom lecture about disk storage and B-trees" published in May 1980 Name interpretation, suggesting that B may mean Boeing or Bayer's name. Search

The search for B-tree is actually very similar to a binary tree:

A binary tree has a keyword and two branches on each node , Each node on

B-tree

has k keywords and (k 1) branches.

The search in the binary tree only considers whether to go left or right, while B-tree

needs to be determined by multiple branches. The search for

B-tree

is divided into two steps:

  1. First find the node. Since B-tree is usually stored on the disk, this step requires disk IO operation;
  2. Find the key word, when a node is found, the node is read into the memory and then the keyword is found through sequential or binary search. If the keyword is not found, you need to judge the size to find a suitable branch to continue searching.

Operation process

Now you need to find the element: 88

First time: Disk IO

The second time: Disk IO

The third time: Disk IO

Then there is a memory comparison, which is compared with 70 and 88 respectively, and finally 88 found.

From the search process, we found that B-tree The number of comparisons and the number of disk IOs are actually not much different from those of binary trees. It seems that there is no difference. What advantages.

But if you take a closer look, you will find that the comparison is completed in memory, does not involve disk IO, and the time consumption is negligible.

In addition, B-tree can store many keywords (the number is determined by the order) in one node, and the same number of keywordsThe nodes generated in B-tree are far less than the nodes in the binary tree, and the difference in the number of nodes is equivalent to the number of disk IOs. After reaching a certain number, the performance difference becomes apparent.

Insertion

When B-tree wants to insert a keyword, it directly finds the leaf node and performs the operation.

  1. Find the leaf node to be inserted according to the keyword to be inserted;
  2. Because the maximum number (order) of child nodes of a node is m , so it is necessary to determine whether the number of keywords in the current node is less than (m - 1).
    • Yes: Insert directly
    • No: Node split occurs. The node is divided into left and right parts based on the middle keyword of the node, and the middle keyword is placed Just go to the parent node.

Operation process

For example, we now need to insert elements in the B-tree with Max Degree (order) of 3: 72

  1. Find the leaf node to be inserted

  2. Node split: It should be with [70,88] On the same disk block, but when a node has 3 keywords, it may have 4 child nodes, which exceeds the maximum degree 3 of the limit we defined, so split## must be performed at this time #: Divide the node into two using the middle keyword as the boundary, generate a new node, and move the middle keyword up to the parent node.

Tip: When there are two middle keywords, the left keyword is usually used Move up the split.

Delete

The deletion operation will be more troublesome than search and insertion, because the keyword to be deleted may or may not be on the leaf node, and deletion may also cause

B-tree is unbalanced, and operations such as merging and rotation are required to maintain the balance of the entire tree.

Take any tree (level 5) as an example

The above is the detailed content of Finally understand that MySQL index needs to use B+tree, and it's so fast. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

See all articles