What is index pushdown in Mysql, understand it in a minute

I think it's nonsense, let's go straight to the dry goods, after all, everyone's time is precious!

 

principle

The principle of MySQL index pushdown can be simply summarized as follows: when executing a query statement, the MySQL optimizer pushes down the part of the WHERE condition that can be pushed down to the storage engine layer to the storage engine for filtering, and returns only the data that meets the condition to the MySQL server, thereby reducing the workload of the MySQL server and improving query efficiency. Typically, only queries that use indexes can take advantage of index pushdown.

In MySQL, the storage engine layer is responsible for the actual data storage and retrieval, while the MySQL server is responsible for processing SQL statements, parsing query statements, optimizing query plans and other tasks. When the MySQL server receives a SELECT statement, it first parses the query statement, determines the query conditions and the data table to be queried, then generates a query plan, and finally sends the query plan to the storage engine layer for actual data retrieval.

In the process of generating the query plan, the MySQL optimizer will select the optimal index to query according to the query conditions and the structure of the data table. If the query conditions contain conditions that can be pushed down, the MySQL optimizer will push these conditions down to the storage engine layer for filtering, and only return the data that meets the conditions to the MySQL server.

For example, suppose we have a data table called employees, which contains information about employees, such as name, job number, date of entry, etc. If we want to query employee information whose entry date is after January 1, 2022, we can use the following SQL statement:

SELECT * FROM employees WHERE hire_date > '2022-01-01';

When executing this query statement, the MySQL optimizer will select an appropriate index to query the hire_date column, and push down the query condition hire_date > '2022-01-01' to the storage engine layer for filtering, and only the data that meets the condition Return to the MySQL server.

example

ICP is not used

Before MySQL 5.6, the storage engine used the joint index to find the primary key id (1, 4) with the name like 'Zhang%', and then scanned back to the table one by one, went to the clustered index to find the complete row record, and then the server layer processed the data according to the age =10 for screening.

Let's look at the schematic diagram:

 

It can be seen that we need to return to the table twice, which wastes another field age of our joint index.

Use ICP

After MySQL 5.6, the storage engine finds the data according to the (name, age) joint index. Since the joint index contains columns, the storage engine directly filters according to age=10 in the joint index. Scan back to the table one by one according to the filtered data.

Let's look at the schematic diagram:

 

It can be seen that the table is returned only once.

In order to better understand the principle of MySQL index pushdown, we can demonstrate its use through a simple example.

Suppose we have a data table called orders, which contains order information, such as order number, order date, order amount, etc. There is an index idx_order_date_amount in this table, which contains two columns of order_date and order_amount. We want to query the order information with the order date after January 1, 2022, and the order amount is greater than 100 yuan, we can use the following SQL statement:

SELECT * FROM orders WHERE order_date > '2022-01-01' AND order_amount > 100;

When executing this query statement, the MySQL optimizer will select the `idx_order_date_amount` index to query the `order_date` and `order_amount` columns, and push down the query condition `order_date > '2022-01-01'` to the storage engine layer for processing filter. The storage engine layer will first use the `order_date` column to filter, and only return data with an order date after January 1, 2022, and then use the `order_amount` column to filter, and only return data with an order amount greater than 100 yuan. In the end, only the data that meets the conditions will be returned to the MySQL server, improving query efficiency. In order to verify the effect of MySQL index pushdown, we can use the EXPLAIN statement to view the query plan. Enter the following statement on the MySQL command line:

EXPLAIN SELECT * FROM orders WHERE order_date > '2022-01-01' AND order_amount > 100; 

After execution, you can get the following query plan:

+----+-------------+-------+------------+------+--------------------------+--------------------------+---------+------+------+----------+-----------------------------+
| id    | select_type  | table  | partitions   | type   | possible_keys                | key                                  | key_len | ref      | rows | filtered    | Extra                       |
+----+-------------+-------+------------+------+--------------------------+--------------------------+---------+------+------+----------+-----------------------------+
| 1     | SIMPLE        | orders| NULL          | ref     | idx_order_date_amount | idx_order_date_amount | 7           | NULL | 1000 | 10.00      | Using where; Using index |
+----+-------------+-------+------------+------+--------------------------+--------------------------+---------+------+------+----------+-----------------------------+

Among them, the possible_keys column shows the index that may be used, the key column shows the index actually used, and the filtered column shows the ratio of the number of rows after filtering. It can be seen that the idx_order_date_amount index is used in the query plan, and the value of the filtered column is 10.00, which means that only 10% of the data needs to be filtered. This demonstrates the effect of MySQL index pushdown.

It should be noted that MySQL index pushdown does not apply to all situations. If the query conditions contain conditions that cannot be pushed down, the MySQL optimizer cannot push the query conditions down to the storage engine layer for filtering, and the index pushdown function cannot be used at this time. In addition, in some cases, index pushdown may lead to poor query performance, so it needs to be evaluated and optimized according to the specific situation.

Guess you like

Origin blog.csdn.net/Dark_orange/article/details/131569900
Recommended