MySQL Advanced Road (17) - Research on Compound Index and Leftmost Matching Principle

Research on Compound Index and Leftmost Matching Principle

I. Overview

​ The previous article introduced how the B+ tree is constructed based on the index and how to use the index to query data. I don’t know if you have found that those examples are all of a column as an index, which is easier to understand. Today, we What will be said is what an index composed of multiple columns looks like, and the rules for its use.

2. What does a joint index look like?

If there is only one column, then matching is very simple, we only need to continuously compare the value of one column, but if there are multiple columns, it may need to be compared multiple times.

When using multiple columns to build a B+ tree, it is built according to a certain logical order. This logic is like the compareTomethod of objects in Java. When comparing whether two objects are equal, they will compare whether some of their properties are equal. Attribute A must be compared first. If they are equal, attribute B will be compared, and so on. If they are all equal, it will be the same. It should be noted here that in order , you cannot compare B first and then compare A. For a joint index, you can treat it as an object with multiple field values ​​as properties, and then call its compareTomethods to compare when you do a lookup.

​ The most important thing to pay attention to when using a composite index to search is the order of comparison!

​ Let's take a look at what a composite index looks like and how to find data

Please add image description

​ Suppose there is such a table currently, with fields such as id, class, name, subject, grade, etc., now use the three fields of "class, name, subject"

A composite index is formed, which is probably like the picture above. The yellow part is the composite index, you can look at it as a whole. Now we want to find the English grades of Zhang San in class 1. We first go to the data page 14 according to the class. After reaching the data page, we can perform a binary search according to the page directory, or we can traverse along the singly linked list. In short, after comparing When I find that the classes are the same, I will compare the names, and then I will find that there are two pieces of data. At this time, I will compare the subjects and finally find them ID:14. Then I will use the id to cluster the index to query the table, and then I will get the desired data. .

3. How to create and use compound indexes

The following is an example of an SQL statement to create a joint index

CREATE TABLE test (
    id         INT NOT NULL,
    col1  CHAR(30) NOT NULL,
    col2  CHAR(30) NOT NULL,
    col3  CHAR(30) NOT NULL,
    col4  CHAR(30) NOT NULL,
    PRIMARY KEY (id),
    INDEX name (col1,col2,col3)
);

​ As can be seen from the SQL statement, now we have established a continuous index based on col1, col2, and col3. Remember this order, which is very important when comparing!

Here is the SQL statement that can use this index:

SELECT * FROM test WHERE col1 = 'xxxx';

SELECT * FROM test WHERE col1 = 'xxxx1' and col2 = 'xxxx2'

SELECT * FROM test WHERE col1 = 'xxxx1' and col2 = 'xxxx2' and col3 = 'xxxx3';

SELECT * FROM test WHERE col1 = 'xxxx1' and col2 = 'xxxx2' and (col3 = 'xxxx3' or col3 = 'xxxx4');

SELECT * FROM test WHERE col1 = 'xxxx1' and col2 = 'xxxx2' and (col3 > 'xxxx3' or col3 < 'xxxx4');

The following SQL statement does not use the above index

SELECT * FROM test WHERE col1 = 'xxxx1' or col2 = 'xxxx2' or col3 = 'xxxx3'

Leftmost matching principle :

​ If you are careful, you should be able to see the difference between the above two types of SQL statements. The first type of SQL statement is used in the order in which the index is created in the Where condition, while the second type is not, because the or statement is used for MySQL has no order at all.

​ For compound indexes, MySQL's optimizer uses the leftmost matching principle to perform the search operation. In the above example, you can use it in the whre statement (col1), (col1, col2)and (col1, col2, col3)three ways to search, of course, the premise is that according to the index Order. For the case where there is only (col1)or (col1, col2)in the where condition, what the compound index can do is to help us narrow the search range as much as possible.

( Note: An index can contain up to 16 columns. )

4. What is a covering index?

Or continue to use the above example, assuming that there is the following SQL statement:

SELECT col1,col2,col3 FROM test WHERE col1 = 'xxxx1' and col2 = 'xxxx2' and col3 = 'xxxx3';

Now the data we want to query is exactly in the fields that make up the composite index. When we find the data page in the composite index, we can directly get the data we need in the data page without returning to the table . This is coverage All, so in many cases, it is not recommended to use select *the writing method. On the one hand, it will consume I/O, and on the other hand, the operation of covering index cannot be used, in other words, a large number of return table operations will be performed.

V. Summary

​ The above is the whole content of this article. It introduces the origin of the leftmost matching principle of the compound index. In fact, the so-called leftmost matching principle is the comparison logic when the compound index is searched.

​ The covering index is also introduced later, which also reminds us that when writing SQL statements, we can leave it to see if this method can be used, and occupy as little select *I/O as possible.

​ If there is something wrong with this article or something needs to be added, please point it out in the comments, everyone can make progress together!

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/120937092