MySQL索引条件下推的简单测试

自MySQL 5.6开始,在索引方面有了一些改进,比如索引条件下推(Index condition pushdown,ICP),严格来说属于优化器层面的改进。

如果简单来理解,就是优化器会尽可能的把index condition的处理从Server层下推到存储引擎层。举一个例子,有一个表中含有组合索引idx_cols包含(c1,c2,…,cn)n个列,如果在c1上存在范围扫描的where条件,那么剩余的c2,…,cn这n-1个上索引都无法用来提取和过滤数据,而ICP就是把这个事情优化一下。

我们在MySQL 5.6的环境中来简单测试一下。

我们创建表emp,含有一个主键,一个组合索引来说明一下。

 
 
 
 
  1. create table emp( 
  2. empno smallint(5) unsigned not null auto_increment, 
  3. ename varchar(30) not null, 
  4. deptno smallint(5) unsigned not null, 
  5. job varchar(30) not null, 
  6. primary key(empno), 
  7. key idx_emp_info(deptno,ename) 
  8. )engine=InnoDB charset=utf8; 

当然我也随机插入了几条数据,意思一下。

 
 
 
 
  1. insert into emp values(1,'zhangsan',1,'CEO'),(2,'lisi',2,'CFO'),(3,'wangwu',3,'CTO'),(4,'jeanron100',3,'Enginer'); 

ICP的控制在数据库参数中有一个优化器参数optimizer_switch来统一管理,我想这也是MySQL优化器离我们最贴近的时候了。可以使用如下的方式来查看。

 
 
 
 
  1. show variables like 'optimizer_switch'; 

当然在5.6以前的版本中,你是看不到index condition pushdown这样的字样的。在5.6版本中查看到的结果如下:

# mysqladmin var|grep optimizer_switch optimizer_switch | index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,subquery_materialization_cost_based=on,use_index_extensions=on下面我们就用两个语句来对比说明一下,就通过执行计划来对比。

 
 
 
 
  1. set optimizer_switch = "index_condition_pushdown=off" 
  2.  
  3. > explain select  *  from emp  where deptno between 1 and 100 and ename ='jeanron100'; 
  4. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
  5. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra      | 
  6. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
  7. |  1 | SIMPLE      | emp  | ALL  | idx_emp_info  | NULL | NULL    | NULL |    4 | Using where | 
  8. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 

而如果开启,看看ICP是否启用。

 
 
 
 
  1. set optimizer_switch = "index_condition_pushdown=on";> explain select  *  from emp  where deptno between 10 and 3000 and ename ='jeanron100'; 
  2. +----+-------------+-------+-------+---------------+--------------+---------+------+------+-----------------------+ 
  3. | id | select_type | table | type  | possible_keys | key          | key_len | ref  | rows | Extra                | 
  4. +----+-------------+-------+-------+---------------+--------------+---------+------+------+-----------------------+ 
  5. |  1 | SIMPLE      | emp  | range | idx_emp_info  | idx_emp_info | 94      | NULL |    1 | Using index condition | 
  6. +----+-------------+-------+-------+---------------+--------------+---------+------+------+-----------------------+ 

1 row in set (0.00 sec)如果你观察仔细,会发现两次的语句还是不同的,那就是范围扫描的范围不同,如果还是用原来的语句,结果还是有一定的限制的。

 
 
 
 
  1. > explain select  *  from emp  where deptno between 1 and 300 and ename ='jeanron100'; 
  2. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
  3. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra      | 
  4. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
  5. |  1 | SIMPLE      | emp  | ALL  | idx_emp_info  | NULL | NULL    | NULL |    4 | Using where | 
  6. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 

1 row in set (0.00 sec)这个地方就值得好好推敲了。

新闻标题:MySQL索引条件下推的简单测试
当前链接:http://www.hantingmc.com/qtweb/news37/475187.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联