阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

95次阅读
没有评论

共计 3129 个字符,预计需要花费 8 分钟才能阅读完成。

自 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) 这个地方就值得好好推敲了。

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-07/145408.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计3129字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中