8.2.1.6 Index Condition Pushdown Optimization指标条件下推优化

Index Condition Pushdown (ICP) is an optimization for the case where MySQL retrieves rows from a table using an index. 索引条件下推(ICP)是MySQL使用索引从表中检索行的一种优化。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 WHERE condition for the rows. 如果没有ICP,存储引擎将遍历索引以定位基表中的行,并将它们返回给MySQL服务器,该服务器将评估行的WHERE条件。With ICP enabled, and if parts of the WHERE condition can be evaluated by using only columns from the index, the MySQL server pushes this part of the WHERE condition down to the storage engine. 启用ICP后,如果WHERE条件的一部分可以仅使用索引中的列进行评估,则MySQL服务器会将WHERE条件的这一部分向下推送到存储引擎。The storage engine then evaluates the pushed index condition by using the index entry and only if this is satisfied is the row read from the table. 然后,存储引擎使用索引项评估推送索引条件,只有满足此条件时,才会从表中读取行。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.ICP可以减少存储引擎必须访问基表的次数和MySQL服务器必须访问存储引擎的次数。

Applicability of the Index Condition Pushdown optimization is subject to these conditions:指标条件下推优化的适用性取决于以下条件:

To understand how this optimization works, first consider how an index scan proceeds when Index Condition Pushdown is not used:要理解这种优化是如何工作的,首先考虑索引扫描不使用索引条件推送时如何进行:

  1. Get the next row, first by reading the index tuple, and then by using the index tuple to locate and read the full table row.获取下一行,首先读取索引元组,然后使用索引元组定位并读取整个表行。

  2. Test the part of the WHERE condition that applies to this table. 测试适用于此表的WHERE条件部分。Accept or reject the row based on the test result.根据测试结果接受或拒绝该行。

Using Index Condition Pushdown, the scan proceeds like this instead:使用“索引条件”下推,扫描按如下方式进行:

  1. Get the next row's index tuple (but not the full table row).获取下一行的索引元组(但不是完整的表行)。

  2. Test the part of the WHERE condition that applies to this table and can be checked using only index columns. 测试适用于此表且只能使用索引列进行检查的WHERE条件部分。If the condition is not satisfied, proceed to the index tuple for the next row.如果不满足条件,则转至下一行的索引元组。

  3. If the condition is satisfied, use the index tuple to locate and read the full table row.如果满足条件,则使用索引元组查找并读取完整的表行。

  4. Test the remaining part of the WHERE condition that applies to this table. 测试适用于此表的WHERE条件的其余部分。Accept or reject the row based on the test result.根据测试结果接受或拒绝该行。

EXPLAIN output shows Using index condition in the Extra column when Index Condition Pushdown is used. EXPLAIN输出显示在使用索引条件下推时在Extra列中Using index conditionIt does not show Using index because that does not apply when full table rows must be read.它不显示Using index,因为当必须读取完整的表行时,这不适用。

Suppose that a table contains information about people and their addresses and that the table has an index defined as INDEX (zipcode, lastname, firstname). 假设一个表包含有关人员及其地址的信息,并且该表有一个定义为INDEX (zipcode, lastname, firstname)的索引。If we know a person's zipcode value but are not sure about the last name, we can search like this:如果我们知道一个人的zipcode值,但不确定姓氏,我们可以这样搜索:

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

MySQL can use the index to scan through people with zipcode='95054'. MySQL可以使用索引扫描zipcode='95054'的用户。The second part (lastname LIKE '%etrunia%') cannot be used to limit the number of rows that must be scanned, so without Index Condition Pushdown, this query must retrieve full table rows for all people who have zipcode='95054'.第二部分(lastname LIKE '%etrunia%')不能用于限制必须扫描的行数,因此,如果没有索引条件下推,此查询必须为所有zipcode='95054'的用户检索完整的表行。

With Index Condition Pushdown, MySQL checks the lastname LIKE '%etrunia%' part before reading the full table row. 按下索引条件,MySQL会在读取完整表行之前检查lastname LIKE '%etrunia%'部分。This avoids reading full rows corresponding to index tuples that match the zipcode condition but not the lastname condition.这样可以避免读取与zipcode条件(而不是lastname条件)匹配的索引元组对应的整行。

Index Condition Pushdown is enabled by default. 默认情况下启用“索引条件”下推。It can be controlled with the optimizer_switch system variable by setting the index_condition_pushdown flag:通过设置index_condition_pushdown标志,可以使用optimizer_switch系统变量对其进行控制:

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

See Section 8.9.2, “Switchable Optimizations”.请参阅第8.9.2节,“可切换的优化”