0
点赞
收藏
分享

微信扫一扫

Index Condition Pushdown Optimization

343d85639154 2022-02-18 阅读 60


​​https://dev.mysql.com/doc/refman/5.7/en/index-condition-pushdown-optimization.html​​

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. 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. 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. 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使用索引从表中检索行的情况的优化。

如果不使用ICP,则存储引擎将遍历索引以在基表中定位行,并将其返回给MySQL服务器,后者将评估这些行的WHERE条件。

启用ICP后,如果仅可以使用索引中的列来评估WHERE条件的一部分,则MySQL服务器会将WHERE条件的这一部分下推到存储引擎。

然后,存储引擎通过使用索引条目来评估推送的索引条件,并且只有在满足此条件的情况下,才从表中读取行。 ICP可以减少存储引擎必须访问基表的次数以及MySQL服务器必须访问存储引擎的次数。


Applicability of the Index Condition Pushdown optimization is subject to these conditions:

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



  • ICP is used for the range, ref, eq_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”.)


触发条件不能下推。 (有关触发条件的信息,请参见第8.2.2.3节“使用EXISTS策略优化子查询”。)



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:


使用“索引条件下推”,扫描将改为这样进行:


Get the next row's index tuple (but not the full table row).

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.

If the condition is satisfied, use the index tuple to locate and read the full table row.

Test the remaining part of the WHERE condition that applies to this table. Accept or reject the row based on the test result.



  • 获取下一行的索引元组(而不是整个表行)。
  • 测试适用于此表的WHERE条件部分,仅可使用索引列进行检查。 如果不满足条件,请转到下一行的索引元组。
  • 如果满足条件,请使用索引元组来定位和读取整个表行。
  • 测试适用于此表的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输出在“Extra”列中显示“Using index condition”。 它不显示“Using index”,因为当必须读取完整表行时,该方法不适用。

Index Condition Pushdown Optimization_mysql


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(zipcode, lastname, firstname)。 如果我们知道一个人的邮政编码值,但是不确定姓氏,可以这样搜索:

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'. 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%')不能用于限制必须扫描的行数,因此,在没有“索引条件下推”的情况下,此查询必须为所有邮政编码为“ 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.


通过索引条件下推,MySQL在读取完整的表行之前检查姓氏,如“%etrunia%”部分。这样可以避免读取与匹配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”​​.

SELECT @@optimizer_switch














举报

相关推荐

0 条评论