一个属于你的次元网络基地
 
昨日:篇  今日:篇   总帖:篇   会员:
小狼人Lv92   
MySQL索引条件下推的简单测试     技术教程
自MySQL 5.6开始,在索引方面有了一些改进,比如索引条件下推(Index condition pushdown,ICP),严格来说属于优化器层面的改进。Image


如果简单来理解,就是优化器会尽可能的把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)这个地方就值得好好推敲了。

 6  已被阅读了2977次  楼主 2017-07-20 14:01:45
回复列表

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

加入官群 QQ咨询 友链展示 申请友链
粤ICP备18094291号
您的IP:3.14.142.115,2024-05-05 22:28:39,Processed in 0.08797 second(s).
免责声明: 本网不承担任何由内容提供商提供的信息所引起的争议和法律责任。
Powered by HadSky 8.0.0
已有0次打赏
(6) 分享
分享

请保存二维码或复制链接进行分享

取消