Data Structure and algorithm principle behind MySQL indexes (I), mysql Indexes
This document usesMySQL
The database is the object of study. We will discuss some topics related to database indexes. In particular,MySQL
Supports many storage engines, and different storage engines support different indexes. ThereforeMySQL
The Database supports multiple index types, suchBTree
Index, hash index, full-text index, and so on. To avoid confusion, this article only focuses onBTree
Index, because this is commonly usedMySQL
This article does not discuss hash indexes and full-text indexes.
The main content of this article is divided into three parts.
The first part focuses on the data structure and algorithm theory.MySQL
Mathematical basis of database indexes.
The second part discusses topics such as clustered index, non-clustered index, and covered index based on the index architecture of MyISAM and InnoDB data storage engines in the MySQL database.
The third part discusses the high-performance index Usage Policy in MySQL based on the above theoretical basis.
The essence of basic data structure and algorithm Indexes
MySQL
The official index definition is:Index (Index
) Is helpMySQL
Efficient Data Structure acquisition. Extract the sentence trunk to get the essence of the index: Index is the data structure.
We know that database query is one of the most important functions of the database. We all want to query data as quickly as possible, so the designers of the database system willQuery Algorithm Optimization. The most basic query algorithm is sequential query (linear search
), This complexity isO(n)
The algorithm is obviously bad when the data size is large. Fortunately, the development of computer science provides many better search algorithms, such as binary search (binary search
), Binary tree search (binary tree search
. If you analyze it a little, you will find that,Each search algorithm can only be applied to a specific data structure.For example, binary search requires that the retrieved data be ordered, while binary search can only be applied to binary search trees,The organization structure of the data itself cannot fully satisfy various data structures(For example, theoretically it is impossible to organize both columns in order at the same time). Therefore,In addition to data, the database system also maintains data structures that meet specific search algorithms.,These data structures reference (point to) data in some wayIn this way, you canImplement advanced search algorithms on these data structures.This data structure is an index..
Let's look at an example:
Figure 1 shows a possible indexing method. On the left is a data table with a total of seven records in two columns, and on the left is the physical address of the data records (note that logically adjacent records are not physically adjacent on the disk ). To speed upCol2
To maintain a binary search tree shown on the right. Each node containsIndex key valueAnd a physical address pointing to the corresponding data recordPointerIn this way, you can use the binary query inO(log2n)
Obtain the corresponding data within the complexity.
Although this is a genuine index, the actual database system hardly uses a binary search tree or its evolutionary Variety(red-black tree)
The reason is described below.
B-Tree and B + Tree
Currently, most database systems and file systems useB-Tree
Or its variantsB+Tree
As an index structure, the next section in this article will discuss why Based on the memory principle and computer access principle.B-Tree
AndB+Tree
This section describes indexes from the perspective of data structures.
B-Tree
To describeB-Tree
First, define a data record as a binary group.[key, data]
,key
Is the key value of the record. For different data records,key
They are different;data
Division of data recordskey
External data. SoB-Tree
Is a data structure that meets the following conditions:
Figure 2 isd=2
OfB-Tree
:
BecauseB-Tree
InB-Tree
Presskey
The data retrieval algorithm is intuitive: first, perform a binary search from the root node. If yes, the system returnsdata
Otherwise, Recursively search the node pointed by the pointer of the corresponding interval until the node ornull
Pointer. The former query is successful, and the latter query fails.B-Tree
The pseudocode of the above search algorithm is as follows:
BTree_Search(node, key){ if(node == null) return null; foreach(node.key) { if(node.key[i] == key) return node.data[i]; if(node.key[i] > key) return BTree_Search(point[i]->node); } return BTree_Search(point[i+1]->node);}data = BTree_Search(root, my_key);
AboutB-Tree
There are a series of interesting properties, such as a degreed
OfB-Tree
, Set its indexN
Itemskey
, Then its height ish
The upper limit islogd((N+1)/2)
, Retrievekey
The progressive complexity of finding the number of nodes isO(logdN)
. From this point, we can see that,B-Tree
Is a very efficient index data structure.
In addition, insertion and deletion of new data records will damageB-Tree
Therefore, when inserting or deleting a tree, you need to split, merge, and transfer the tree to maintainB-Tree
Nature, this article does not intend to fully discussB-Tree
This is because there are a lot of detailsB-Tree
If you are interested in the mathematical nature and the insertion and deletion algorithm, you can find the relevant materials in the reference column at the end of this Article for reading.
B + Tree
B-Tree
There are many variants, the most common of which isB+Tree
For exampleMySQL
It is widely used.B+Tree
Implement its index structure.
AndB-Tree
Compared,B+Tree
There are the following differences:
Figure 3 is a simpleB+Tree
.
Because not all nodes have the same domainB+Tree
The size of the inner and middle nodes is generally different. This correspondsB-Tree
Different thoughB-Tree
Thekey
The number of pointers may be different, but the fields and upper limit of each node are the same.B-Tree
The same size of space is usually applied for each node.
Generally,B+Tree
RatioB-Tree
It is more suitable for implementing the external storage index structure. The specific reason is related to the principle of external storage and the principle of computer access, which will be discussed below.
B + Tree with sequential access pointers
Generally used in a database or File SystemB+Tree
The structure is in the classicB+Tree
And added sequential access pointers.
4B+Tree
Add a pointer to the adjacent leaf node for each leaf node.With sequential access pointerB+Tree
. The purpose of this optimization isImprove the Performance of range accessIn Example 4, if you want to querykey
For slave18
To49
All data records,After 18 is found, you only need to traverse the nodes and pointers to access all the data nodes at a time. This greatly mentions the interval query efficiency..
This section describesB-Tree
AndB+Tree
A brief introduction is provided. The next section introduces why the currentB+Tree
It is the preferred data structure for database systems to implement indexes.
Why use B-Tree (B + Tree)
As mentioned above, data structures such as red and black trees can also be used for indexing. However, file systems and database systems are widely used.B-/+Tree
As an index structure, this section will be discussed based on knowledge about computer composition principles.B-/+Tree
As the theoretical basis of indexes.
In general, the index itself is also very large, it is impossible to store all in the memory, so the index is often stored on the disk in the form of index files. In this way, a disk is generated during the index search process.I/O
Consumption, relative to memory access,I/O
The access consumption is several orders of magnitude higher, soThe most important indicator for evaluating a data structure as an index is the diskI/O
Progressive complexity of the number of operations. In other words,The structure of indexes should be organized to minimize the number of disks in the search process.I/O
Number of accesses. Next we will first introduce the memory and disk access principles, and then combine these principles for analysis.B-/+Tree
Efficiency of indexing.
Principle of primary storage access
Currently, the main memory used by computers is basically random read/write memory (RAM
), ModernRAM
The structure and access principle of the object are complicated. The difference in this article is as follows: abstract a very simple access model to describe it.RAM
.
From the abstract point of view, the primary storage is a matrix composed of a series of storage units, each of which stores a fixed size of data. Each storage unit has a unique address, and the addressing rules of modern primary storage are complicated. Here, it is simplified into a two-dimensional address: a row address and a column address can be used to uniquely locate a storage unit. Figure 5 shows a 4x4 primary storage model.
The access process of the primary storage is as follows:
When the system needs to read the primary storage, the address signal is put into the address bus and uploaded to the primary storage. After reading the address signal, the primary storage parses the signal and locates the specified storage unit, the stored unit data is then placed on the Data Bus for reading by other components.
The process of writing the primary storage is similar. The system will write the Unit Address and data respectively on the address bus and data bus. The primary storage will read the content of the two bus and write the data accordingly.
It can be seen that the time of primary storage access is only linearly related to the number of accesses, because there is no mechanical operation, the "distance" of the data accessed twice will not affect the time, for example, firstA0
Retrieve againA1
And first takeA0
Retrieve againD3
The time consumption is the same.
Disk access principle
As mentioned above, indexes are generally stored on disks as files, and index retrieval requires disks.I/O
Operation. Unlike primary storage, DisksI/O
There is a mechanical movement cost, so the diskI/O
The time consumption is huge.
Figure 6 shows the overall structure of the disk.
A disk is composed of circular disks of the same size and coaxial. the disk can be rotated (each disk must be rotated synchronously ). There is a head bracket on one side of the disk, and the head bracket is fixed with a set of heads, each of which is responsible for accessing the content of a disk. The head cannot be rotated, but it can be moved along the radius of the disk (in fact, it is a diagonal movement). Each head must be coaxial at the same time, that is, from top to bottom, all heads are overlapped at any time (however, multiple heads are already available independently, but not limited to this ).
Figure 7 shows the disk structure.
The disc is divided into a series of concentric rings with the center of the disc. Each concentric ring is calledTrackAll the tracks with the same radius constitute oneCylindrical. The track is divided into small segments along the radius line. Each segment is calledSlice,Each sector is the minimum storage unit of the disk.. For simplicity, we assume that the disk has only one disk and one head.
When data needs to be read from the disk, the system will pass the data Logical Address to the disk. the disk control circuit translates the logical address into a physical address according to the addressing logic, determine the track and sector of the data to be read. To read data from this sector, you need to place the head above this sector. To achieve this, the head needs to be moved and aligned to the corresponding track. This process is called track finding and the time it takes is calledSeek timeAnd then the disk rotates the target sector to the bottom of the head. the time consumed in this process is calledRotation time.
Local principle and disk pre-read
Because of the characteristics of the storage medium, the access to the disk itself is much slower than the primary storage, coupled with the cost of mechanical movement, the access speed of the disk is often one of the primary storage, so in order to improve efficiency, minimize disk spaceI/O
. To achieve this goal, the disk is usually not read strictly on demand, but preread every time. Even if only one byte is required, the disk starts from this location, read data of a certain length in sequence into the memory. This theory is based on the well-known local principle in Computer Science:
When a data is used, the data nearby it is usually used immediately.
The data required during the program running is usually concentrated.
Because sequential disk reading is highly efficient (with little rotation time required without seeking time), preread can improve local programs.I/O
Efficiency.
The pre-read length is generally page (page
. Pages are logical blocks for computer memory management. Hardware and operating systems often divide primary and disk storage areas into contiguous blocks of the same size, each block is called a page (in many operating systems, the page size is usually4k
), The primary storage and disk exchange data in pages. When the data to be read by the program is not in the primary storage, a page missing exception is triggered, and the system sends a disk reading signal to the disk, the disk finds the starting position of the data and reads one or more pages consecutively into the memory. If an exception is returned, the program continues to run.
B-/+ Tree index Performance Analysis
It can be analyzed at last.B-/+Tree
The index performance is improved.
As mentioned above, disks are generally used.I/O
Number of times to evaluate the index structure. Starting fromB-Tree
Analysis, accordingB-Tree
You can see that you need to accessh
Nodes. The database system designer cleverly utilizes the disk pre-read principle,Set the size of a node to equal to one page.,In this way, each node only needs oneI/O
It can be fully loaded.. In order to achieve this goalB-Tree
You also need to use the following skills:
Each time you create a node, you can directly apply for a page space. This ensures that a node is physically stored on a page. In addition, the computer storage allocation is page-aligned.node
Only onceI/O
.
B-Tree
A maximumh-1
TimesI/O
(Root Node resident memory), progressive complexity isO(h)=O(logdN)
. In practiced
It is a very large number, usually greater100
, Soh
Very small (usually no more than 3 ).
To sum up, useB-Tree
As an index structure, the efficiency is very high.
The structure of the red and black trees,h
It is much deeper. Because logically close nodes (Parent and Child) may be far physically unable to use localityI/O
The progressive complexity is alsoO(h)
, Obvious efficiency ratioB-Tree
A lot worse.
As mentioned above,B+Tree
More suitable for external storage index, cause and internal node outboundd
. From the analysis above, we can see that,d
The larger the index, the better the performance, and the maximum degree of output depends on the node.key
Anddata
Size:
dmax = floor(pagesize / (keysize + datasize + pointsize)) (pagesize – dmax >= pointsize)
Or
dmax = floor(pagesize / (keysize + datasize + pointsize)) – 1 (pagesize – dmax < pointsize)
floor
Returns the rounded down value.BecauseB+Tree
Internal node removeddata
Therefore, you can have a higher degree of output and better performance..
This chapter discusses the index-related data structures and algorithms theoretically. The next chapter will discussB+Tree
How to ImplementMySQL
AndMyISAM
AndInnDB
The storage engine introduces two different index implementation modes: Non-clustered index and clustered index.
MySQL index implementation
InMySQL
The index is a concept at the storage engine level. Different storage engines have different implementation methods for the index. This article mainly discussesMyISAM
AndInnoDB
The index implementation method of the two storage engines.
MyISAM index implementation
MyISAM
Engine usageB+Tree
As the index structure, the leaf nodedata
The domain storesData Record address. YesMyISAM
Schematic diagram of the index:
The table has three columns.Col1
Is the primary key, then figure 8 isMyISAM
Primary Index of a table (Primary key
. We can see thatMyISAM
The index file only stores the address of the data record. InMyISAM
Medium, primary index and secondary index (Secondary key
) There is no difference in structure, but it is only required by the primary index.key
The secondary index is unique.key
It can be repeated. If weCol2
If a secondary index is created, the index structure is shown in:
It is alsoB+Tree
,data
The address of the domain to save the data record. Therefore,MyISAM
The index search algorithm inB+Tree
If the specifiedKey
If yes, retrievedata
The value of the domain, and thendata
The value of the domain is the address, and the corresponding data record is read..
MyISAM
The index method is also called "non-clustering ".InnoDB
Clustered index differentiation.
InnoDB Index implementation
AlthoughInnoDB
Also useB+Tree
As the index structure, but the specific implementation method isMyISAM
Different.
The first major difference is thatInnoDB
The data file itself is the index file. From the above,MyISAM
Index files and data files are separated,The index file only stores the address of the data record. WhileInInnoDB
The table data file itself isB+Tree
An index structure of the organization, the leaf node of the treedata
Domain stores complete data records. This indexkey
Is the primary key of the data table, soInnoDB
The table data file itself is the primary index.
Figure 10 isInnoDB
The primary index (also a data file) shows that the leaf node contains a complete data record. This index is calledClustered Index. BecauseInnoDB
The data file itself needs to be clustered by the primary key, soInnoDB
The table must have a primary key (MyISAM
No). If it is not explicitly specifiedMySQL
The system automatically selects a column that uniquely identifies a data record as the primary key. If this column does not existMySQL
AutomaticallyInnoDB
The table generates an implicit field as the primary key,This field is 6 bytes in length and its type is long integer..
Second andMyISAM
The difference between indexes is thatInnoDB
Secondary Indexdata
The value of the primary key of the corresponding record in the domain storage instead of the address.. In other words,InnoDB
All secondary indexesdata
Domain. For example, in Figure 11Col3
A secondary index on:
It is a string of English characters.ASCII
Code as a comparison criterion. Clustered index makes the search by primary key very efficient,Secondary index search requires two indexes: First retrieve the secondary index to obtain the primary key, and then use the primary key to retrieve the record in the primary index.
Understanding the index implementation methods of different storage engines is very helpful for correct use and optimization of indexes, for example, knowingInnoDB
After the implementation of the index, it is easy to understand whyToo long fields are not recommended as primary keys.Because all secondary indexes reference the primary index,If the primary index is too long, the secondary index will become too large.. For example, a non-monotonous field is used as the primary key inInnoDB
Is not a good idea, becauseInnoDB
The data file itself isB+Tree
A non-monotonous primary key causes the data file to be maintained when a new record is inserted.B+Tree
And frequent split adjustment, which is very inefficient, and using the auto-incrementing field as the primary key is a good choice.
The next article will discuss these index-Related Optimization Strategies in detail.