MySQL index condition pushdown optimization

Everyone thinks the writing is okay, you can like, bookmark, and pay attention to it!
You can also visit my personal blog , it is estimated that it will be updated in recent years! Be friends with me! https://motongxue.cn


Index condition pushdown optimization

overview

Index Condition Pushdown (ICP for short) is an optimization for the situation where MySQL uses an index to retrieve rows from a table. And it was launched on the version of Mysql5.6. MySQL provides a way to use an index to obtain tuples from a specific table, that is, a single table uses an index to scan to obtain data, so it is not suitable for multi-table queries.

The difference between whether to enable ICP

In the absence of ICP, the storage engine traverses the index to locate the rows in the base table and returns them to the MySQL server , which evaluates the row's WHERE condition.

When ICP is enabled, if part of the WHERE condition can be calculated using only the columns in the index, the MySQL server will push this part of the WHERE condition down to the storage engine . The storage engine then evaluates the pushed index condition by using the index entry, and only if that condition is met, the row is read from the table. **You can understand its meaning in the following example.

The meaning of ICP

  1. The optimization of index pushdown on non-primary key indexes can effectively reduce the number of table returns and greatly improve query efficiency.
  2. ICP can reduce the number of times the storage engine must access the base table
  3. The number of times the MySQL server must access the storage engine.

The applicability of indicator conditional pushdown optimization depends on the following conditions:

  1. ICP is used for the range, ref, eq_ref, and ref_or_null access methods when access to entire table rows is required.

  2. ICP can be used with InnoDB and MyISAM tables, including partitioned InnoDB and MyISAM tables.

  3. For InnoDB tables, ICP is only used for secondary indexes. The goal of ICP is to reduce the number of whole row reads, thereby reducing I/O operations. For InnoDB clustered indexes, complete records are already read into InnoDB buffers. Using ICP in this case does not reduce I/O. (Note: Ⅰ means to reduce the number of read complete records (a complete tuple) ; Ⅱ means that it is invalid for InnoDB clustered indexes, and only valid for non-clustered indexes such as SECOND INDEX.)

  4. Secondary indexes created on virtually generated columns do not support ICP. InnoDB supports secondary indexes on virtual generated columns.

  5. Conditions referencing subqueries cannot be pushed down.

  6. Conditions referencing stored functions cannot be pushed down. Storage engines cannot call stored functions.

  7. Trigger conditions cannot be pushed down.

ICP optimization principle

To understand how this optimization works, first consider how an index scan works without index condition pushdown:

  1. To get the next row, first read the index tuple, then use the index tuple to locate and read the entire table row.
  2. The test applies to the Conditions section of this table WHERE. Accept or reject rows based on test results.

Push down process

Using the "index condition" pushdown, the scanning process is as follows:

  1. Get the index tuple for the next row (instead of the entire table row).
  2. Tests the part of the condition that applies to this table and can only be checked using indexed columns WHERE. If the condition is not met, go to the next row's index tuple.
  3. If the condition is met, the entire table row is located and read using the index tuple.
  4. Test the rest of the conditions that apply to this table WHERE. Accept or reject rows based on test results.

EXPLAINThe output shows the use of the index condition in the extra column when pushing down using the index condition. It doesn't show using an index because that doesn't apply when full table rows have to be read .

the case

Suppose a table contains information about people and their addresses, and the table has an indexindex defined as (Mobile Last, Name, Package). If we know a person's phone number but not their name, we can search like this:

SELECT * FROM order
  WHERE 手机尾号='9856'
  AND 姓名 LIKE '%莫同学%'
  AND 包裹号 LIKE '%211%';

MySQL can use the index to scan people whose phone numbers end in '9856'. The second part (name LIKE '%Classmate Mo%') cannot be used to limit the number of rows that must be scanned, so if there is no index pushdown, this query must retrieve the full table rows of all people whose phone number = '9850'.

When using pushdown index conditions, MySQL checks the name LIKE '%Mo classmate%' part before reading the entire table row. This avoids reading the complete row corresponding to an index tuple that matches the mobile phone suffix condition but not the name condition.

On and off of ICP

Index criteria are enabled with "pushdown" by default. index_condition_pushdownSystem variables can be controlled by setting flags optimizer_switch:

SET optimizer_switch = 'index_condition_pushdown=off';
SET optimizer_switch = 'index_condition_pushdown=on';

Updated on June 5, 2021

Everyone thinks the writing is okay, you can like, bookmark, and pay attention to it!
You can also visit my personal blog , it is estimated that it will be updated in recent years! Be friends with me! https://motongxue.cn


Guess you like

Origin blog.csdn.net/CrazyMooo/article/details/117932324
Recommended