Index Condition Pushdown Optimization

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,存储引擎层将会穿过索引从基表里面定位行,并把结果返回给server层,MySQL server层将会根据where条件判断哪些结果合格并返回给客户端). With ICP enabled, and if parts of the WHERE condition can be evaluated by using only fields from the index, the MySQL server pushes this part of the WHERE condition down to the storage engine(如果用了ICP,如果能根据索引里的字段获取符合where条件,server层就会把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可以减少存储引擎层必须访问基表的次数和server层必须访问存储引擎层的次数).

Index Condition Pushdown optimization 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方法访问全表数据优化的). This strategy can be used for InnoDB and MyISAM tables. (Note that index condition pushdown is not supported with partitioned tables in MySQL 5.6; this issue is resolved in MySQL 5.7.) For InnoDB tables, however, ICP is used only for secondary indexes(然而对于innodb表,ICP只适用于二级索引). The goal of ICP is to reduce the number of full-record reads and thereby reduce IO operations(ICP的目标是减少全表扫描的次数和减少IO操作). For InnoDB clustered indexes, the complete record is already read into the InnoDB buffer. Using ICP in this case does not reduce IO(对于InnoDB的聚簇索引,所有的记录都已经在buffer pool里面了,所以使用ICP在这种情况下并没有减少IO).

To see how this optimization works, consider first 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.

 

When Index Condition Pushdown is used, 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.

  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.

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

Suppose that we have a table containing information about people and their addresses and that the table has an index defined asINDEX (zipcode, lastname, firstname). If we know a person‘s zipcode value but are not sure about 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 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 the people who have zipcode=‘95054‘.

With Index Condition Pushdown, MySQL will check the lastname LIKE ‘%etrunia%‘ part before reading the full table row. This avoids reading full rows corresponding to all index tuples that do not match the lastname condition.

Index Condition Pushdown is enabled by default; it can be controlled with the optimizer_switch system variable by setting theindex_condition_pushdown flag. See Section 8.9.2, “Controlling Switchable Optimizations”.

以上是关于Index Condition Pushdown Optimization的主要内容,如果未能解决你的问题,请参考以下文章

MySQL Index Condition Pushdown

MySQL 索引条件下推 Index Condition Pushdown

MySQL性能优化之 Index Condition Pushdown

MySQL索引与Index Condition Pushdown

MySQL 5.6 Index Condition Pushdown

MySQL索引与Index Condition Pushdown