MySQL-5.7-8.2.1.5 Index Condition Pushdown Optimization

Index Condition Pushdown (ICP) is an optimization for the case where MySQL retrieves rows from a table using an index.

当MySQL使用索引从表中检索行时,索引条件下推(ICP)是一种优化。

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服务器,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:

下推优化的适用性取决于以下条件:

  • ICP is used for the rangerefeq_ref, and ref_or_null access methods when there is a need to access full table rows.

  • 当需要访问完整的表行时,ICP用于range、ref、eq_ref和ref_or_null访问方法。

  • ICP can be used for InnoDB and MyISAM tables, including partitioned InnoDB and MyISAM tables.

  • ICP可以用于InnoDB和MyISAM表,包括分区的InnoDB和MyISAM表

  • For InnoDB tables, ICP is used only for secondary indexes. The goal of ICP is to reduce the number of full-row reads and thereby reduce I/O operations. For InnoDB clustered indexes, the complete record is already read into the InnoDB buffer. Using ICP in this case does not reduce I/O.

  • 对于InnoDB表,ICP只用于二级索引。ICP的目标是减少整行读取的数量,从而减少I/O操作。对于InnoDB集群索引,完整的记录已经被读入InnoDB缓冲区。在这种情况下使用ICP不会减少I/O。

  • ICP is not supported with secondary indexes created on virtual generated columns. InnoDB supports secondary indexes on virtual generated columns.

  • 在虚拟生成的列上创建二级索引时,不支持ICP。InnoDB支持在虚拟生成的列上建立二级索引。

  • Conditions that refer to subqueries cannot be pushed down.

  • 引用子查询的条件不能下推。

  • Conditions that refer to stored functions cannot be pushed down. Storage engines cannot invoke stored functions.

  • 引用存储函数的条件不能下推。存储引擎无法调用存储函数。

  • Triggered conditions cannot be pushed down. (For information about triggered conditions, see Section 8.2.2.3, “Optimizing Subqueries with the EXISTS Strategy”.)

  • 无法下推触发的条件。

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. Accept or reject the row based on the test result.测试适用于该表的WHERE条件部分。根据测试结果接受或拒绝该行。

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. If the condition is not satisfied, proceed to the index tuple for the next row.测试应用于该表的WHERE条件的一部分,并且只能使用索引列进行检查。如果条件不满足,则继续执行下一行的索引元组。

  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. Accept or reject the row based on the test result.测试应用于此表的WHERE条件的其余部分。根据测试结果接受或拒绝该行。

 EXPLAIN output shows Using index condition in the Extra column when Index Condition Pushdown is used. It does not show Using index because that does not apply when full table rows must be read.

当使用索引条件下推时,EXPLAIN输出显示在额外列中使用索引条件。它没有显示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). If we know a person's zipcode value but are not sure about the last name, we can search like this:

假设一个表包含关于人员及其地址的信息,并且该表有一个定义为index的索引(邮政编码、姓、名)。如果我们知道一个人的邮政编码值,但不确定他的姓,我们可以这样搜索:

MySQL can use the index to scan through people with 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'.

MySQL可以使用索引来扫描邮政编码='95054'的人。第二部分(姓氏LIKE '%etrunia%')不能用于限制必须扫描的行数,因此如果没有索引条件下推,这个查询必须检索所有拥有zipcode='95054'的人的完整表行。

With Index Condition Pushdown, MySQL checks the lastname LIKE '%etrunia%' part before reading the full table row. This avoids reading full rows corresponding to index tuples that match the zipcode condition but not the lastname condition.

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系统变量:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值