MySQL Index Condition Pushdown

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一、Index Condition Pushdown简介

ICP(index condition pushdown)是mysql利用索引(二级索引)元组和筛字段在索引中的where条件从表中提取数据记录的一种优化操作。ICP的思想是:存储引擎在访问索引的时候检查筛选字段在索引中的where条件(pushed index condition,推送的索引条件),如果索引元组中的数据不满足推送的索引条件,那么就过滤掉该条数据记录。ICP(优化器)尽可能的把index condition的处理从server层下推到storage engine层。storage engine使用索引过过滤不相关的数据,仅返回符合index condition条件的数据给server层。也是说数据过滤尽可能在storage engine层进行,而不是返回所有数据给server层,然后后再根据where条件进行过滤。

二、ICP开启和关闭时数据访问和提取过程对比

优化器没有使用ICP时,数据访问和提取的过程如下:

180800_VWg5_3023401.png

1):MySQL Server发出读取数据的命令,这是在执行器中执行如下代码段,通过函数指针和handle接口调用存储引擎的索引读或全表表读。此处进行的是索引读。

if (in_first_read){in_first_read= false;error= (*qep_tab->read_first_record)(qep_tab); //设定合适的读取函数,如设定索引读函数/全表扫描函数}elseerror= info->read_record(info);

2、3):进入存储引擎,读取索引树,在索引树上查找,把满足条件的(经过查找,红色的满足)从表记录中读出(步骤④,通常有IO),从存储引擎返回⑤标识的结果。此处,不仅要在索引行进行索引读取(通常是内存中,速度快。步骤③),还要进行进行步骤④,通常有IO。

6):从存储引擎返回查找到的多条元组给MySQL Server,MySQL Server在⑦得到较多的元组。

7、8):⑦到⑧依据WHERE子句条件进行过滤,得到满足条件的元组。注意在MySQL Server层得到较多元组,然后才过滤,最终得到的是少量的、符合条件的元组。

 

优化器使用ICP时,server层将会把能够通过使用索引进行评估的where条件下推到storage engine层。

183253_pOK6_3023401.png

数据访问和提取过程如下:

1)    storage engine从索引中读取下一条索引元组。

2)    storage engine使用索引元组评估下推的索引条件。如果没有满足where条件,storage engine将会处理下一条索引元组(回到上一步)。只有当索引元组满足下推的索引条件的时候,才会继续去基表中读取数据。

3)    如果满足下推的索引条件,storage engine通过索引元组定位基表的行和读取整行数据并返回给server层。

4)    server层评估没有被下推到storage engine层的where条件,如果该行数据满足where条件则使用,否则丢弃。

三、ICP测试

3.1  对比执行计划的差别

联合索引的第一个条件可以使用索引,第二个不能使用索引

root@localhost:mysql.sock  15:33:47 [test]>explain select  *   from person  where postadlcode between 300000 and 400000 and age > 40;+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                 |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+|  1 | SIMPLE      | person | NULL       | range | idx_p_a       | idx_p_a | 7       | NULL |    1 |    33.33 | Using index condition |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+1 row in set, 1 warning (0.11 sec)

关闭ICP后

root@localhost:mysql.sock  15:35:42 [test]>set optimizer_switch = "index_condition_pushdown=off";Query OK, 0 rows affected (0.00 sec)root@localhost:mysql.sock  15:39:48 [test]>explain select  *   from person  where postadlcode between 300000 and 400000 and age > 40;+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+|  1 | SIMPLE      | person | NULL       | range | idx_p_a       | idx_p_a | 7       | NULL |    1 |    33.33 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.00 sec)

where条件包含索引字段但用不到索引

root@localhost:mysql.sock  15:39:49 [test]>explain select  *   from person  where age > 40;+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+|  1 | SIMPLE      | person | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    7 |    33.33 | Using where |+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.00 sec)root@localhost:mysql.sock  15:41:03 [test]>set optimizer_switch = "index_condition_pushdown=on";Query OK, 0 rows affected (0.00 sec)root@localhost:mysql.sock  15:41:09 [test]>explain select  *   from person  where age > 40;+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+|  1 | SIMPLE      | person | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    7 |    33.33 | Using where |+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.01 sec)

结论:

需要index condition pushdown 的query通常索引的字段出现where子句里面都是范围查询。比如:

select * from tb where tb.key_part1 < x and tb.key_part2 = y       
select * from tb where tb.key_part1 = x andtb.key_part2 like '%yyyy%'
select * from tb where tb.key_part1 > x and tb.key_part1 < y and tb.key_part1 > xx and tb.key_part2 < yy

但是需要注意的是:
1. 如果索引的第一个字段的查询就是没有边界的比如 key_part1 like '%xxx%',那么不要说ICP,就连索引都会没法利用。
2.
如果select的字段全部在索引里面,那么就是直接的index scan了,没有必要什么ICP

为了方便大家交流,本人开通了微信公众号,和QQ群1(291519319)和QQ群2(659336691)。喜欢技术的一起来交流吧

转载于:https://my.oschina.net/u/3023401/blog/1535362

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540637.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ADBB的完整形式是什么?

ADBB&#xff1a;所有完成的再见 (ADBB: All Done Bye Bye) ADBB is an abbreviation to All Done Bye Bye. ADBB是All Done Bye Bye的缩写。 Whenever a person wants to convey his message to another person, they use some sort of short-form in the text messages. ADB…

c 环境

系统ubuntu sudo apt-get install vim g openssh-server libgl1-mesa-dev检查下安装的版本gcc -v g -v make -v gdb -v 转载于:https://blog.51cto.com/skinglzw/1964449

java.util (Collection接口和Map接口)

1&#xff1a;Collection和Map接口的几个主要继承和实现类 1.1 Collection接口 Collection是最基本的集合接口&#xff0c;一个Collection代表一组Object&#xff0c;即Collection的元素&#xff08;Elements&#xff09;。一些Collection允许相同的元素而另一些不行。一些能排…

scala 拆分字符串翻转_Scala程序分割字符串

scala 拆分字符串翻转A string is a collection that stores multiple characters, it is an immutable sequence which cannot be changed. 字符串是存储多个字符的集合&#xff0c;它是不可更改的不可更改的序列。 分割字符串 (Splitting a string) In Scala, using the spl…

[转载] python 简单示例说明os.walk和os.path.walk的不同

参考链接&#xff1a; 示例说明Python2.x和Python3.x之间的重要区别 import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,filespath) if __name__"__main__": print "os.walk" index 1 for root,subd…

c#中索引器是什么_C#中的索引器

c#中索引器是什么An Indexer is a special feature of C# to use an object as an array. If you define an indexer in a class then it will behave like a virtual array. Indexer is also known as smart array in C#. It is not a compulsory or essential part of OOPS. …

asp.net MVC5为WebAPI添加命名空间的支持

前言 默认情况下&#xff0c;微软提供的MVC框架模板中&#xff0c;WebAPI路由是不支持Namespace参数的。这导致一些比较大型的项目&#xff0c;无法把WebApi分离到单独的类库中。 本文将提供解决该问题的方案。 微软官方曾经给出过一个关于WebAPI支持Namespace的扩展&#xff0…

[转载] Python3.X 线程中信号量的使用方法示例

参考链接&#xff1a; 示例说明Python2.x和Python3.x之间的重要区别 信号量semaphore 是一个变量&#xff0c;控制着对公共资源或者临界区的访问。信号量维护着一个计数器&#xff0c;指定可同时访问资源或者进入临界区的线程数。下面这篇文章主要给大家介绍了关于Python3.X 线…

从流程的自动化中获得最大价值的10种方式

流程自动化很好&#xff0c;如果它可以节省时间并减少错误。但是如果它不能在业务流程中“很好地契合”&#xff0c;那么会难以得到普及。问问有谁没有对语音助手感到伤脑筋。 所幸的是&#xff0c;某些最佳实践让你可以从流程自动化中获得最大价值&#xff0c;以下就是其中的1…

java中null是常量吗_C_NULL Julia中的常量

java中null是常量吗Julia| C_NULL常数 (Julia | C_NULL Constant) C_NULL is a constant of Ptr{Nothing} type in Julia programming language, it represents the null pointer value, which is used for C Null Pointer while calling external code. C_NULL是Julia编程语言…

[转载] Python京东抢购

参考链接&#xff1a; 从Python获取输入 Python京东抢购 分析其中提交信息接口的参数&#xff0c;可以成功抢购商品&#xff0c;并且可以提交订单。。。。2018年7月17日 提交信息的获取 直接提交信息对post提交分析其中的参数。 经过分析参数大多数在&#xff1a;https…

6.04 从字符串中删除不需要的字符

需求&#xff1a;删除所有的0和元音字母。 select ename,replace(replace(replace(replace(replace(ename,A,),E,),I,),O,),U,) as stripped1,sal,replace(sal,0,) stripped2from emp;转载于:https://www.cnblogs.com/liang545621/p/7518766.html

Scala分号

Scala分号 (Scala semicolons) A semicolon or semi-colon (;) is a punctuation mark in programming, it is used to separate multiple lines of code. It is common in major programming languages like C, C, Java, Pascal. In modern programming languages like Python…

[转载] python通过adb获取android手机耗电量

参考链接&#xff1a; 从Python中控制台获取输入 把开发者模式打开&#xff0c;激活 adb 调试&#xff0c;然后可以使用以下python代码获取安卓手机的耗电量 # -*- coding: utf-8 -*- import re import os def getSelectDevice(): pip os.popen(adb devices) result pip.…

ES6之主要知识点(二) 变量的解构赋值。默认值

引自http://es6.ruanyifeng.com/#docs/destructuring 数组解构赋值默认值对象解构赋值用途1.数组的解构赋值 let [a, b, c] [1, 2, 3]; let [foo, [[bar], baz]] [1, [[2], 3]]; foo // 1 bar // 2 baz // 3let [ , , third] ["foo", "bar", "baz&…

python无符号转有符号_Python | 散布符号

python无符号转有符号There are multiple types of Scatter Symbols available in the matplotlib package and can be accessed through the command marker. In this article, we will show some examples of different marker types and also present a list containing all…

[转载] 基于LSTM的股票预测模型_python实现_超详细

参考链接&#xff1a; 从Python获取输入 文章目录 一、背景二、主要技术介绍1、RNN模型2、LSTM模型3、控制门工作原理四、代码实现五、案例分析六、参数设置七、结论完整程序下载 一、背景 近年来&#xff0c;股票预测还处于一个很热门的阶段&#xff0c;因为股票市场的波动…

shell -eom_EOM的完整形式是什么?

shell -eomEOM&#xff1a;消息结尾 (EOM: End Of Message) EOM is an abbreviation of "End Of Message". EOM是“消息结尾”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is also written as Eom or eom. It is written at …

在eclipse中启动Tomcat访问localhost:8080失败项目添加进Tomcat在webapp中找不到

软件环境&#xff1a;Eclipse oxygen&#xff0c; Tomcat8.5 #在eclipse中启动Tomcat访问localhost:8080失败 在eclipse中配置tomcat后&#xff0c;打开tomcat后访问localhost:8080后无法出现登陆成功的界面,即无法出现下面的界面 在eclipse中的servers状态栏中双击tomcat&…

[转载] 【基础教程】Python input()函数:获取用户输入的字符串

参考链接&#xff1a; 从Python中控制台获取输入 input() 是 Python 的内置函数&#xff0c;用于从控制台读取用户输入的内容。input() 函数总是以字符串的形式来处理用户输入的内容&#xff0c;所以用户输入的内容可以包含任何字符。 input() 函数的用法为&#xff1a; str…