前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL性能优化特性 Index Condition Pushdown

MySQL性能优化特性 Index Condition Pushdown

作者头像
用户1278550
发布2018-08-09 10:58:41
6200
发布2018-08-09 10:58:41
举报
文章被收录于专栏:idbaidba

一 概念介绍 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式。 a 当关闭ICP时,index 仅仅是data access 的一种访问方式,存储引擎通过索引回表获取的数据会传递到MySQL Server 层进行where条件过滤。 b 当打开ICP时,如果部分where条件能使用索引中的字段,MySQL Server 会把这部分下推到引擎层,可以利用index过滤的where条件在存储引擎层进行数据过滤,而非将所有通过index access的结果传递到MySQL server层进行where过滤. 优化效果:ICP能减少引擎层访问基表的次数和MySQL Server 访问存储引擎的次数,减少io次数,提高查询语句性能。 二 原理

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. Index Condition Pushdown is used 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 est the remaining part of the WHERE condition that applies to this table. Accept or reject the row based on the test result.

三 实践案例

a 环境准备 数据库版本 5.6.16 + 关闭缓存

set query_cache_size=0; set query_cache_type=OFF;

测试数据下载地址 b 当开启ICP时

此时情况下根据MySQL的最左前缀原则, first_name 可以使用索引,last_name采用了like 模糊查询,不能使用索引。 c 关闭ICP

当开启ICP时 查询在sending data环节时间消耗是 0.000189s

当关闭ICP时 查询在sending data环节时间消耗是 0.000735s

从上面的profile 可以看出ICP 开启时整个sql 执行时间是未开启的2/3,sending data 环节的时间消耗前者仅是后者的1/4

ICP 开启时的执行计划 含有 Using index condition 标示 ,表示优化器使用了ICP对数据访问进行优化。

ICP 关闭时的执行计划显示use where.

案例分析 以上面的查询为例关闭ICP 时,存储引擎通前缀index first_name 访问表中225条first_name 为Anneke的数据,并在MySQL server层根据last_name like '%sig' 进行过滤 开启ICP 时,last_name 的like '%sig'条件可以通过索引字段last_name 进行过滤,在存储引擎内部通过与where条件的对比,直接过滤掉不符合条件的数据。该过程不回表,只访问符合条件的1条记录并返回给MySQL Server ,有效的减少了io访问和各层之间的交互。 ICP 关闭时 ,仅仅使用索引作为访问数据的方式。

ICP 开启时 ,MySQL将在存储引擎层 利用索引过滤数据,减少不必要的回表,注意 虚线的using where 表示如果where条件中含有没有被索引的字段,则还是要经过MySQL Server 层过滤。

四 ICP的使用限制
  1. 当sql需要全表访问时,ICP的优化策略可用于range, ref, eq_ref, ref_or_null 类型 的访问数据方法 。
  2. 支持InnoDB和MyISAM表。
  3. ICP只能用于二级索引,不能用于主索引。
  4. 并非全部where条件都可以用ICP筛选。 如果where条件的字段不在索引列中,还是要读取整表的记录到server端做where过滤。
  5. ICP的加速效果取决于在存储引擎内通过ICP筛选掉的数据的比例。
  6. 5.6 版本的不支持分表的ICP 功能,5.7 版本的开始支持。
  7. 当sql 使用覆盖索引时,不支持ICP优化方法。

五 参考文章 [1] http://dev.mysql.com/doc/refman/5.7/en/index-condition-pushdown-optimization.html

[2] https://mariadb.com/kb/en/mariadb/mariadb-documentation/optimization-and-tuning/query-optimizations/index-condition-pushdown/

[3] http://olavsandstaa.blogspot.com/2011/04/mysql-56-index-condition-pushdown.html

[4] http://www.mysqlperformanceblog.com/2013/03/14/mysql-5-6-10-optimizer-limitations-index-condition-pushdown/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 四 ICP的使用限制
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档