MySQL 索引条件下推 Index Condition Pushdown

MySQL 索引条件下推 Index Condition Pushdown 出现在MySQL5.6及之后的版本中,能大幅提升查询效率,原因如下:

内容摘录自《深入理解MariaDB和MySQL》

下面使实验,使用官方提供的employees 测试数据库演示。

> use employees ;

> show create table employees \G

***************************[ 1. row ]***************************

Table        | employees

Create Table | CREATE TABLE `employees` (

`emp_no` int(11) NOT NULL,

`birth_date` date NOT NULL,

`first_name` varchar(14) NOT NULL,

`last_name` varchar(16) NOT NULL,

`gender` enum('M','F') NOT NULL,

`hire_date` date NOT NULL,

PRIMARY KEY (`emp_no`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

> alter table employees add index idx_lastname_firstname(last_name,first_name);

关闭ICP:

> set optimizer_switch='index_condition_pushdown=off';

> explain extended select * from employees where last_name='Action' and first_name LIKE '%sal' ;

***************************[ 1. row ]***************************

id            | 1

select_type   | SIMPLE

table         | employees

type          | ref

possible_keys | idx_lastname_firstname

key           | idx_lastname_firstname

key_len       | 18

ref           | const

rows          | 1

filtered      | 100.0

Extra         | Using where

查询条件中的first_name 这个前面%匹配导致无法用到整个idx_lastname_firstname 索引的,只能根据last_name 字段过滤部分数据,然后在里面找出符合first_name列 %sal的行记录。

但是,如果开启ICP,则执行计划如下:

> set optimizer_switch='index_condition_pushdown=on';

> explain extended select * from employees where last_name='Action' and first_name LIKE '%sal' \G

***************************[ 1. row ]***************************

id            | 1

select_type   | SIMPLE

table         | employees

type          | ref

possible_keys | idx_lastname_firstname

key           | idx_lastname_firstname

key_len       | 18

ref           | const

rows          | 1

filtered      | 100.0

Extra         | Using index condition

原理:

索引比较是在InnoDB存储引擎层进行的。而数据表的记录比较first_name条件是在MySQL引擎层进行的。开启ICP之后,包含在索引中的数据列条件(即上述SQL中的first_name LIKE %sal') 都会一起被传递给InnoDB存储引擎,这样最大限度的过滤掉无关的行。

执行计划如下图:

原文地址:http://blog.51cto.com/lee90/2060449

时间: 2024-08-18 04:57:16

MySQL 索引条件下推 Index Condition Pushdown的相关文章

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

ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行回表的代价相比堆表中是要高一些的.相关文档地址:http://dev.mysql.com/doc/refman/5.6/en/index-condition-pushdown-optimization.html Index Condition Pushdown optimization is use

浅析MySQL中的Index Condition Pushdown (ICP 索引条件下推)和Multi-Range Read(MRR 索引多范围查找)查询优化

本文出处:http://www.cnblogs.com/wy123/p/7374078.html(保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错误进行修正或补充,无他) ICP优化原理 Index Condition Pushdown (ICP),也称为索引条件下推,体现在执行计划的上是会出现Using index condition(Extra列,当然Extra列的信息太多了,只能做简单分析)ICP原理通俗讲就是,查询过程中,直接在查询引擎

MySQL5.6.7-rc index condition pushdown 索引条件下推代码解读

MySQL5.6.7-rc index condition pushdown  索引条件下推代码解读 http://jishu.zol.com.cn/4505.html MySQL5.6.7-rc index condition pushdown代码解读 A-A+ 2013-08-07 11:10| 分享到: 对index condition pushdown很感兴趣,并且跟踪代码让自己受益良多,因此就来跟一下相关代码. 看的是mysql5.6.7-rc官方社区版. 先说说我对研究MySQL源码

MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)-Using index condition

http://blog.itpub.net/22664653/viewspace-1210844/ -- 这篇博客写的更细,以后看 ICP(index condition pushdown)是mysql利用索引(二级索引)元组和筛字段在索引中的where条件从表中提取数据记录的一种优化操作.ICP的思想是:存储引擎在访问索引的时候检查筛选字段在索引中的where条件(pushed index condition,推送的索引条件),如果索引元组中的数据不满足推送的索引条件,那么就过滤掉该条数据记录

MySQL索引与Index Condition Pushdown

大约在两年前,我写了一篇关于MySQL索引的文章.最近有同学在文章的评论中对文章的内容提出质疑,质疑主要集中在联合索引的使用方式上.在那篇文章中,我说明联合索引是将各个索引字段做字符串连接后作为key,使用时将整体做前缀匹配. 而这名同学在这个页面找到了如下一句话:index condition pushdown is usually useful with multi-column indexes: the first component(s) is what index access is

MySQL Index Condition Pushdown(ICP)优化

Index Condition Pushdown(ICP)索引条件下推优化适用于mysql在table中通过index检索数据行,没有ICP,存储引擎层遍历索引来定位基表(base table)上的数据行并且把它们返回给server层,由server层来计算过滤where语句.使用ICP,并且where语句的部分筛选条件可以通过index来检测,则mysql server层会讲部分where 条件下推给存储引擎层.存储引擎通过使用index条目来评估下推的index condition,并且仅仅

MySQL Index Condition Pushdown

Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式.[Index Condition Pushdown] 当关闭ICP时,index 仅仅是data access 的一种访问方式,存储引擎通过索引回表获取的数据会传递到MySQL Server 层进行where条件过滤.       当打开ICP时,如果部分where条件能使用索引中的字段,MySQL Server 会把这部分下推到引擎层,可以利用in

MySQL 5.6新特性 -- Index Condition Pushdown

Index Condition Pushdown(ICP)是针对mysql使用索引从表中检索行数据时的一种优化方法. 在没有ICP特性之前,存储引擎根据索引去基表查找并将数据返回给mysql server,mysql server再根据where条件进行数据过滤. 有了ICP之后,在取出索引的同时,判断是否可以根据索引中的列进行where条件过滤,也就是将where的部分过滤操作放在了存储引擎层.这样就会减少上层sql层对记录的获取. ICP优化支持range.ref.eq_ref.ref_or

【MySQL】性能优化之 Index Condition Pushdown

一 概念介绍    Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式.a 当关闭ICP时,index 仅仅是data access 的一种访问方式,存储引擎通过索引回表获取的数据会传递到MySQL Server 层进行where条件过滤.b 当打开ICP时,如果部分where条件能使用索引中的字段,MySQL Server 会把这部分下推到引擎层,可以利用index过滤的where条件在存储引擎层进