Why do MySQL indexes need to be pushed down?

 

Article directory

1. Table return operation

2. Primary key index

3. Non-primary key index

4. Low version operation

5. High version operation

 6. Summary

1. Table return operation

For databases, as long as indexes are involved, table return operations cannot be bypassed. Of course, this is also the premise and basis of what we are talking about today. When it comes to returning tables, we need to start with indexes. Here we use Innodb storage engine as the object of explanation.

2. Primary key index

        The underlying data storage of primary key index is implemented through B+ tree . Nodes other than leaf nodes store primary key values, and leaf nodes store an entire row of data. The primary key index is actually a clustered index, which is the process of sorting data according to the primary key value and storing it in a B+ tree.

The general structure is shown in the figure below

3. Non-primary key index

In addition to the primary key index, other indexes are called non-primary key indexes (non-clustered indexes). Different from the primary key index, the leaf nodes of the non-primary key index store the primary key value pointing to the corresponding row, rather than the complete row data. Therefore, when using the non-primary key index, you need to first find the primary key value of the corresponding row, and then Find the complete row data through the primary key value .

So let's go back to the original question, what is a table return operation?

        When we search for a row of data on a non-primary key index, the search method at this time is to first search the non-primary key index tree , get the corresponding primary key value , and then search for the corresponding row of data on the primary key index tree . This operation is called a table return operation .

Okay, here you should understand what the table return operation is. To put it simply, it is to get the corresponding primary key value on the non-primary key index tree, and then return to the primary key index to find the corresponding row data.

The prerequisite for this is that the field being searched does not exist in the non-primary key index tree.

4. Low version operation

After talking about the table return operation, let us continue to return to the topic of this article- index pushdown .

In fact, there was no index pushdown function before Mysql version 5.6. This optimization item was added only after version 5.6 . Therefore, before introducing index pushdown, let’s review how it would be handled without this function.

Let's explain with a real example.

Here there is a user table user, which records the user's name, gender, height, age and other information. In the table, id is an auto-incrementing primary key, and (name,sex) is a joint index. Here, 1 represents male and 2 represents female. Now we need to find information about all men with the surname Wang.

SQL is very simple to implement:

But what is its implementation principle?

According to the leftmost prefix principle of the joint index , when we find the first value that meets the condition on the non-primary key index tree, we go back to the primary key index tree to find the corresponding row data through the primary key value recorded in the leaf node, and then compare whether The gender you are currently looking for.

The entire principle can be represented by the diagram below.

You see, in the lower version, every piece of data needs to be returned to the table, which increases the number of tree searches. If you encounter a large amount of data to be found, performance will inevitably be lacking.

5. High version operation

Having finished talking about low-version operations, let’s return to the topic of this article—index pushdown.

If you know the pain points, how to solve them. It's very simple, only if you meet the conditions, you can return the form . Combined with our example, when the gender sex = 1 is satisfied, then go back to the table to look up. In this way, it may have been necessary to perform table lookups 4 times, but now it may only be necessary 2 times.

So in essence, index pushdown means that only the fields that meet the conditions are returned to the table, and the fields included in the index are first judged, and those that do not meet the conditions are skipped . Reduce unnecessary table return operations.

 6. Summary

table return operation

  • When the field you are looking for is not on the non-primary key index tree, you need to obtain the corresponding row data on the primary key index through the primary key value of the leaf node. This process is called a table return operation.

index pushdown

  • Index pushdown mainly reduces unnecessary table return operations. For the found data, first filter out the data that does not meet the conditions, and then search the rest in the primary key index tree.

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_71921932/article/details/131125998