What's going on with Mysql's index condition pushdown? What is the use? Theory

Mysql index condition pushdown optimization

Preface

This article is translated from the English version of the official document. The official website address is attached at the end of the article.

text

Index Condition Pushdown (ICP) is an optimization for the situation where MySQL uses an index to retrieve rows from a table. Without ICP, the storage engine traverses the index to locate rows in the base table and returns them to the MySQL server, which evaluates the row's WHEREconditions. When ICP is enabled, if some WHEREconditions can be evaluated using only columns in the index, the MySQL server will push these WHEREconditions down to the storage engine. The storage engine then evaluates the pushed index condition by using the index entries, and only reads rows from the table if this condition is met. ICP can reduce the number of times the storage engine must access the base table and the number of times the MySQL server must access the storage engine.

The applicability of index condition pushdown optimization is limited by the following conditions:

  • rangeICP is used for the , ref, eq_refand access methods when an entire table row needs to be accessed .ref_or_null
  • ICP can be used for InnoDBand MyISAMtables, including partitioned tables InnoDBand MyISAMtables.
  • For InnoDBtables, ICP is only used for secondary indexes. The goal of ICP is to reduce the number of full-row reads, thereby reducing I/O operations. For InnoDBclustered indexes, the complete record has been read into InnoDBthe buffer. Using ICP in this case will not reduce I/O.
  • Secondary indexes created on virtual generated columns do not support ICP. InnoDBSupports secondary indexes on virtually generated columns.
  • Conditions that refer to subqueries cannot be pushed down.
  • Conditions referencing storage functions cannot be pushed down. The storage engine cannot call stored functions.
  • Trigger conditions cannot be pushed down. (For information about trigger conditions, see "Optimizing subqueries using the EXISTS strategy .")

To understand how this optimization works, first consider how an index scan works when index conditional pushdown is not used:

  1. Get the next row, first by reading the index tuple, then using the index tuple to locate and read the full table row.
  2. Test some conditions on this table WHERE. Accept or reject the row based on the test results.

Using index conditional pushdown, the scan would go like this:

  1. Get the index tuple of the next row (but not the entire table row).
  2. Partial conditions are tested on this table WHEREand can only be checked using indexed columns. If the condition is not met, continue with the index tuple of the next row.
  3. If the condition is met, the index tuple is used to locate and read the full table row.
  4. Test the rest of the conditions on this table WHERE. Accept or reject the row based on the test results.

When using an index condition to push down, the output in EXPLAINthe Extracolumn is shown as Using index condition. It is not shown Using indexas this does not apply when the full table row must be read.

Suppose a table contains information about people and their addresses, and the table has INDEX (zipcode, lastname, firstname)an index defined as . If we know a person's zipcodevalue but are not sure of the last name, we can search like this:

SELECT * FROM people
  WHERE zipcode='95054'
  AND lastname LIKE '%etrunia%'
  AND address LIKE '%Main Street%';

MySQL can use an index to scan for zipcode='95054'people who have it. The second part ( lastname LIKE '%etrunia%') cannot be used to limit the number of rows that must be scanned, so without the index condition to push down, this query must retrieve all zipcode='95054'people who have it.

Using index conditional pushdown, MySQL checks a partial row before reading the entire table row lastname LIKE '%etrunia%'. This avoids reading the complete row corresponding to the index tuple that matches zipcodethe condition but does not match the condition.lastname

By default, index condition pushdown optimization is enabled. 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';

Reference:
MySQL Reference Manual - Index Condition Pushdown Optimization

Guess you like

Origin blog.csdn.net/booynal/article/details/125667378