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)
這個地方就值得好好推敲了。