Index pushdown and index merging that must be known in Mysql

index pushdown

Assuming that a joint index of name and age is established for the table now, for the convenience of understanding, I will take the previous figure again

Next, execute the following sql

select * from `user` where name > '王五' and age > 22;

Before MySQL5.6 (excluding 5.6), the general execution steps of the entire SQL are as follows:

  • First, according to the binary search, locate name > '王五'the first piece of data, that is, Zhao Liu with id=4
  • After that, it will return to the table according to id=4, search the data in other fields with id=4 in the clustered index, and then judge whether the age in the data is greater than 22, if it is, it means that it is the data we need to find, otherwise it is not
  • Then follow the linked list, continue to traverse, and then return to the table once when a record is found, and then judge the age, and so on until the end

Therefore, as shown in the figure, the entire search process will go through 5 back-to-table operations, two Zhao Liu, two Liu Qi, and one Wang Jiu, and finally the data that meets the conditions is the data of Zhao Liu with id=6, and the rest ages do not match and. Although there is no problem with this implementation, I don’t know if you have discovered that it is not necessary to return the table so many times, because it can be seen from the index diagram above that the matching data name > '王五' and age > 22is the data of Zhao Liu with id=6. So after MySQL5.6, the above age > 22judgment logic is optimized. It is still the same as before, locate and find Zhao Liu with id=4, and then do not go back to the table to judge the age, because the index column has the value of age, then directly judge whether it is greater than 22 according to the age in the index, if it is greater, then Return to the table to query the remaining field data (because it is select *), and then traverse the linked list sequentially until the end. So after this optimization, the number of times to return to the table becomes 1, which greatly reduces the number of times to return to the table compared to the previous 5 times. And this optimization is called index pushdown , which is to reduce the number of times to return to the table.

index merge

Index merge (index merge) is an index optimization mechanism introduced from MySQL5.1. In previous MySQL versions, multiple query conditions in one sql can only use one index. In the case of multiple indexes are scanned, and then the scan results are merged. The results will be merged into the following three situations:

  • Take the intersection (intersect)
  • Take union
  • Union after sorting (sort-union)

In order not to delay the demonstration, delete all previous indexes, and then create a secondary index idx_name and idx_age for name and age respectively

Take the intersection (intersect)

When the following sql is executed, the intersection will appear

select * from `user` where name = '赵六' and age= 22;

View execution plan

type is index_merge, and both possible_key and key are sum idx_name, idx_ageindicating that the index merge is used, and Extra has it Using intersect(idx_age,idx_name), and intersect means intersection. The whole process is roughly like this, take out the corresponding primary key id according to idx_nameand respectively, and then take the intersection of the primary key id, then the id of this part of the intersection must meet the query conditions of the query at the same time (think carefully), and then return to the table according to the id of the intersection . However, if you want to use the joint index that takes the intersection, you need to satisfy that the primary key ids found by the respective indexes are sorted, which is for the convenience of quickly taking the intersection. For example, the following sql cannot use the joint indexidx_agename = '赵六' and age= 22

select * from `user` where name = '赵六' and age > 22;

You can only use the name index, because age > 22the IDs found are out of order. When talking about indexes, I mentioned the sorting rules of index columns. It can be seen from this that the conditions for using a joint index are relatively harsh.

Take union

andTaking the union is to replace the previous example withor

select * from `user` where name = '赵六' or age = 22;

The previous execution is the same. According to the conditions, go to the respective indexes to search, and then take the union of the queried id to deduplicate, and then return to the table. Similarly, taking the union also requires that the primary key ids found by the respective indexes are sorted. If the query condition is changed age > 22to

select * from `user` where name = '赵六' or age > 22;

Union after sorting (sort-union)

Although the union requires that the primary key ids found by the respective indexes be sorted, but if there is an unsorted situation, mysql will automatically optimize this situation, sorting the primary key id first, and then fetching and merging Set, this situation is called sort-union. For example, the above-mentioned sql that cannot directly take the union is in line with the situation of taking the union after sorting (sort-union)

select * from `user` where name = '赵六' or age > 22;

The original text comes from Sanyou's java diary 

Guess you like

Origin blog.csdn.net/qq_28165595/article/details/131030863