首页 数据库 mysql教程 MySQL 优化之 ICP (index condition pushdown:索引条件下推)_MySQL

MySQL 优化之 ICP (index condition pushdown:索引条件下推)_MySQL

May 18, 2018 am 11:33 AM

ICP技术是在MySQL5.6中引入的一种索引优化技术。它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数。在索引组织表中,使用二级索引进行回表的代价相比堆表中是要高一些的。

Index Condition Pushdown optimization is used for the range, ref, eq_ref, and ref_or_null access methods when there is a need to access full table rows. 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. The goal of ICP is to reduce the number of full-record reads and thereby reduce IO operations. For InnoDB clustered indexes(值主键索引), the complete record is already read into the InnoDB buffer. Using ICP in this case does not reduce IO.

要想深入理解 ICP 技术,必须先理解数据库是如何处理 where 中的条件的。

对 where 中过滤条件的处理,根据索引使用情况分成了三种:index key, index filter, table filter

1. index key

用于确定SQL查询在索引中的连续范围(起始范围+结束范围)的查询条件,被称之为Index Key。由于一个范围,至少包含一个起始与一个终止,因此Index Key也被拆分为Index First Key和Index Last Key,分别用于定位索引查找的起始,以及索引查询的终止条件。

2. index filter

在使用 index key 确定了起始范围和介绍范围之后,在此范围之内,还有一些记录不符合where 条件,如果这些条件可以使用索引进行过滤,那么就是 index filter。

3. table filter

where 中的条件不能使用索引进行处理的,只能访问table,进行条件过滤了。

如何确定 index key, index filter, table filter,可以参考何博士的文章。

在 MySQL5.6 之前,并不区分Index Filter与Table Filter,统统将Index First Key与Index Last Key范围内的索引记录,回表读取完整记录,然后返回给MySQL Server层进行过滤。而在MySQL 5.6之后,Index Filter与Table Filter分离,Index Filter下降到InnoDB的索引层面进行过滤,减少了回表与返回MySQL Server层的记录交互开销,提高了SQL的执行效率。

所以所谓的 ICP 技术,其实就是 index filter 技术而已。只不过因为MySQL的架构原因,分成了server层和引擎层,才有所谓的“下推”的说法。所以ICP其实就是实现了index filter技术,将原来的在server层进行的table filter中可以进行index filter的部分,在引擎层面使用index filter进行处理,不再需要回表进行table filter。

4. ICP 技术启用前后比较

To see how this optimization works, consider first how an index scan proceeds when Index Condition Pushdown is not used:

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.

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:

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.

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.

5. ICP 例子

官方文档给出了一个例子:

Suppose that we have a table containing 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:

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 the index_condition_pushdown flag. See Section 8.9.2, “Controlling Switchable Optimizations”.

上面例子中的 lastername like '%etrunia%' 和 address like '%Main Street%' 本来是无法使用复合索引 index(zipcode, lastername, firstname) 进行过滤的,但是因为有了ICP技术,所以他们可以在 index filter 阶段使用索引进行过滤,而不需要回表进行 table filter.

例子2:

role_goods 表上有组合索引 index(roleId,status,number),下面的select语句,因为 “索引最左前缀原则”,只能使用到 组合索引的 roleId 部分,但是因为 ICP 技术的存在,现在 number 条件过滤也可以在 index filter 阶段完成了,无需像以前一样需要进行 table filer 了:

mysql> explain select * from role_goods where roleId=100000001 and number=1;

+----+-------------+------------+------+---------------+----------+---------+-------+------+-----------------------+

| id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                 |

+----+-------------+------------+------+---------------+----------+---------+-------+------+-----------------------+

|  1 | SIMPLE      | role_goods | ref  | roleId_2      | roleId_2 | 9       | const |   14 | Using index condition |

+----+-------------+------------+------+---------------+----------+---------+-------+------+-----------------------+

1 row in set (0.01 sec)

可以看到 key_len = 9, 因为 roleId 是big int 类型,所以 key_len = 8 + 1 = 9; 所以在 index key 阶段中,并没有使用到 组合索引 index(roleId,status,number) 中的 number 字段(因为中间有一个status字段没有出现在where 条件中),但是 “Using index condition” 却说明使用到了ICP技术,显然是 number =1 条件过滤使用到了ICP技术。

以上就是MySQL 优化之 ICP (index condition pushdown:索引条件下推)_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何修复 MySQL 8.4 上的 mysql_native_password 未加载错误 如何修复 MySQL 8.4 上的 mysql_native_password 未加载错误 Dec 09, 2024 am 11:42 AM

MySQL 8.4(截至 2024 年的最新 LTS 版本)中引入的主要变化之一是默认情况下不再启用“MySQL 本机密码”插件。此外,MySQL 9.0完全删除了这个插件。 此更改会影响 PHP 和其他应用程序

Mpeppe(MPEPE)币:加密货币市场的新竞争者,吸引来自 Render(RNDR)和互联网计算机(ICP)的投资者 Mpeppe(MPEPE)币:加密货币市场的新竞争者,吸引来自 Render(RNDR)和互联网计算机(ICP)的投资者 Sep 03, 2024 pm 02:03 PM

加密货币的世界总是在不断变化,新的代币吸引了寻找下一个重大机会的经验丰富的投资者的注意力。

随着 BlockDAG (BDAG) 代币受到关注,互联网计算机 (ICP) 投资者寻求其他选择 随着 BlockDAG (BDAG) 代币受到关注,互联网计算机 (ICP) 投资者寻求其他选择 Sep 04, 2024 am 03:33 AM

尽管技术实力雄厚,但互联网计算机(ICP)由于进展缓慢,正在考验一些投资者的耐心。 Uniswap (UNI) 的日子也不好过。

使用 Redis Exporter 服务监控 Redis Droplet 使用 Redis Exporter 服务监控 Redis Droplet Jan 06, 2025 am 10:19 AM

有效监控 Redis 数据库对于保持最佳性能、识别潜在瓶颈和确保整体系统可靠性至关重要。 Redis Exporter Service 是一个强大的实用程序,旨在使用 Prometheus 监控 Redis 数据库。 本教程将指导您完成 Redis Exporter Service 的完整设置和配置,确保您无缝建立监控解决方案。通过学习本教程,您将实现完全可操作的监控设置,以有效监控 Redis 数据库的性能指标。

Toncoin (TON)、互联网计算机 (ICP) 和 BlockDAG:一周命运喜忧参半 Toncoin (TON)、互联网计算机 (ICP) 和 BlockDAG:一周命运喜忧参半 Sep 13, 2024 am 06:27 AM

在加密货币喜忧参半的一周中,Toncoin (TON)、Internet Computer (ICP) 和 BlockDAG 的各种发展掀起了波澜。

Mpeppe(MPEPE):区块链领域的后起之秀吸引了互联网计算机(ICP)鲸鱼的注意 Mpeppe(MPEPE):区块链领域的后起之秀吸引了互联网计算机(ICP)鲸鱼的注意 Aug 29, 2024 am 03:18 AM

加密货币市场对竞争并不陌生,Solana (SOL) 和 Internet Computer (ICP) 等主要参与者不断争夺主导地位。

互联网计算机 (ICP) 价格即将爆发——这就是 ICP 可能成为下一个重大事件的原因 互联网计算机 (ICP) 价格即将爆发——这就是 ICP 可能成为下一个重大事件的原因 Oct 24, 2024 am 04:12 AM

加密货币分析师 Seth (@seth_fin) 与他的 X(以前的 Twitter)关注者分享说,ICP 是当今为数不多的绿色山寨币之一,暗示了其大幅上涨的潜力。

PHP 与 MySQL:终极分步指南 PHP 与 MySQL:终极分步指南 Nov 26, 2024 pm 12:09 PM

PHP 是一种语言,可让您在开发网页时灵活地连接和使用不同的数据库。有不同的数据库,既有商业的,也有免费使用的。其中MySQL是最常用的数据库

See all articles