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

时间:2022-05-05
本文章向大家介绍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,含有一个主键,一个组合索引来说明一下。

create table emp( empno smallint(5) unsigned not null auto_increment, ename varchar(30) not null, deptno smallint(5) unsigned not null, job varchar(30) not null, primary key(empno), key idx_emp_info(deptno,ename) )engine=InnoDB charset=utf8;

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

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

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

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

下面我们就用两个语句来对比说明一下,就通过执行计划来对比。

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

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

set optimizer_switch = "index_condition_pushdown=on";> explain select  *   from emp  where deptno between 10 and 3000 and ename ='jeanron100';
+----+-------------+-------+-------+---------------+--------------+---------+------+------+-----------------------+
| id | select_type | table | type  | possible_keys | key          | key_len | ref  | rows | Extra                 |
+----+-------------+-------+-------+---------------+--------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | emp   | range | idx_emp_info  | idx_emp_info | 94      | NULL |    1 | Using index condition |
+----+-------------+-------+-------+---------------+--------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

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

> explain select  *   from emp  where deptno between 1 and 300 and ename ='jeanron100';
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | emp   | ALL  | idx_emp_info  | NULL | NULL    | NULL |    4 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

这个地方就值得好好推敲了。