Table of contents
This chapter of index will be divided into the following parts to study:
- Index overview
- Index structure
- Index classification
- Index syntax
- SQL performance analysis
- Index usage
- Index design principles
Index overview
introduce
Index (index) is a data structure ( ordered ) that helps MySQL obtain data efficiently . In addition to data, the database system also maintains data structures that satisfy specific search algorithms. These data structures reference (point to) the data in some way, so that advanced search algorithms can be implemented on these data structures. This data structure is an index. .
When querying table data without an index (for example, querying age equals 45),
The entire table needs to be scanned to match one by one whether it meets the query conditions;
If the binary search tree structure is used to index, there is no need to perform a full table scan when querying, and the information with age equal to 45 in the table can be found quickly.
Advantages and Disadvantages
Advantage | Disadvantages |
---|---|
Improve data retrieval efficiency and reduce database IO costs | Index columns also need space |
Sort data through index columns to reduce the cost of data sorting and reduce CPU consumption. | Indexes greatly improve query efficiency, but at the same time, they also reduce the speed of updating tables. For example, when performing INSERT, UPDATE, and DELETE on the table, the efficiency decreases. |
If the index field data in the table is frequently modified, the index maintenance work will also be frequent, and even data inconsistency problems may occur.
Index structure (1)
introduce
When we learned about storage engines earlier, we learned that MySQL indexes are implemented at the storage engine layer. Different storage engines have different index structures, which mainly include the following (some data structure basics are better understood):
Index structure | describe |
---|---|
B+Tree index
|
The most common index type, most engines support B+ tree indexes
|
Hash
index
|
The underlying data structure is implemented using a hash table
.
Only queries that exactly match the index column are valid
.
Not
Support range query
|
R-tree (
space index
lead)
|
Spatial index is a special index type of the
MyISAM
engine, mainly used for geospatial data classes
type, usually used less
|
Full-text(
full text
index
)
|
It is a way to quickly match documents
by
establishing an inverted index .
Similar to
Lucene,Solr,ES
|
B+ trees are the most commonly used. We focus on B+ trees.
Next, let’s take a look at the support of different storage engines for index structures:
index | InnoDB | MyISAM | Memory |
---|---|---|---|
B+tree
index
|
support
|
support
|
support
|
Hash
index
|
not support
|
not support
|
support
|
R-tree
index
|
not support
|
support |
not support
|
Full-text
|
Supported after version 5.6
|
support | not support |
Binary tree
If the index structure of MySQL adopts the data structure of a binary tree, the ideal structure is as follows:
If the primary keys are inserted sequentially, a one-way linked list will be formed with the following structure:
Therefore, if you choose a binary tree as the index structure, there will be the following disadvantages:
- When inserting sequentially, a linked list will be formed, and query performance will be greatly reduced.
- In the case of large amounts of data, the hierarchy is deep and the retrieval speed is slow.
At this point, you may think that you can choose a red-black tree. The red-black tree is a self-balancing binary tree. Even if data is inserted sequentially, the final data structure will be a balanced binary tree. The structure is as follows:
However, even so, since the red-black tree is also a binary tree, there will be a shortcoming:
- In the case of large amounts of data, the hierarchy is deep and the retrieval speed is slow.
Therefore, in the index structure of MySQL, binary tree or red-black tree is not selected, but B+Tree is selected.
Before learning B+Tree, let’s introduce a B-Tree.
B-Tree
B-Tree, B-tree is a multi-fork roadmap search tree. Compared with binary trees, each node of B-tree can have multiple branches, that is, multi-fork.
Taking a b-tree with a maximum degree (max-degree) of 5 (order 5) as an example, each node of this B-tree can store up to 4 keys and 5 pointers.
Note: The degree of a tree refers to the number of child nodes of a node.
We can briefly demonstrate this through a website that visualizes data structure.
https://www.cs.usfca.edu/~galles/visualization/BTree.html
Insert a set of data: 100 65 169 368 900 556 780 35 215 1200 234 888 158 90 1000 88 120
268 250. Then observe the changes in the nodes during the insertion of some data.
Features:
- A 5-order B-tree stores up to 4 keys per node, corresponding to 5 pointers.
- Once the number of keys stored in a node reaches 5, it will fission, and the intermediate elements will split upward.
- In a B-tree, both non-leaf nodes and leaf nodes store data.
END
Learn from: Dark Horse Programmer - MySQL Database Course