共計(jì) 3174 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
本篇文章給大家分享的是有關(guān)如何進(jìn)行 MySQL 索引條件下推的簡(jiǎn)單測(cè)試,丸趣 TV 小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著丸趣 TV 小編一起來(lái)看看吧。
自 MySQL 5.6 開始,在索引方面有了一些改進(jìn),比如索引條件下推(Index condition pushdown,ICP), 嚴(yán)格來(lái)說(shuō)屬于優(yōu)化器層面的改進(jìn)。
如果簡(jiǎn)單來(lái)理解,就是優(yōu)化器會(huì)盡可能的把 index
condition 的處理從 Server 層下推到存儲(chǔ)引擎層。舉一個(gè)例子,有一個(gè)表中含有組合索引 idx_cols 包含(c1,c2,…,cn)n 個(gè)列,如果在 c1 上存在范圍掃描的 where 條件,那么剩余的 c2,…,cn 這 n - 1 個(gè)上索引都無(wú)法用來(lái)提取和過(guò)濾數(shù)據(jù), 而 ICP 就是把這個(gè)事情優(yōu)化一下。
我們?cè)?MySQL 5.6 的環(huán)境中來(lái)簡(jiǎn)單測(cè)試一下。
我們創(chuàng)建表 emp,含有一個(gè)主鍵,一個(gè)組合索引來(lái)說(shuō)明一下。
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;
當(dāng)然我也隨機(jī)插入了幾條數(shù)據(jù),意思一下。
insert into emp values(1, zhangsan ,1, CEO),(2, lisi ,2, CFO),(3, wangwu ,3, CTO),(4, jeanron100 ,3, Enginer
ICP 的控制在數(shù)據(jù)庫(kù)參數(shù)中有一個(gè)優(yōu)化器參數(shù) optimizer_switch 來(lái)統(tǒng)一管理,我想這也是 MySQL 優(yōu)化器離我們最貼近的時(shí)候了。可以使用如下的方式來(lái)查看。
show variables like optimizer_switch
當(dāng)然在 5.6 以前的版本中,你是看不到 index condition pushdown 這樣的字樣的。在 5.6 版本中查看到的結(jié)果如下:
# 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 下面我們就用兩個(gè)語(yǔ)句來(lái)對(duì)比說(shuō)明一下,就通過(guò)執(zhí)行計(jì)劃來(lái)對(duì)比。
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) 如果你觀察仔細(xì),會(huì)發(fā)現(xiàn)兩次的語(yǔ)句還是不同的,那就是范圍掃描的范圍不同,如果還是用原來(lái)的語(yǔ)句,結(jié)果還是有一定的限制的。
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) 這個(gè)地方就值得好好推敲了。
以上就是如何進(jìn)行 MySQL 索引條件下推的簡(jiǎn)單測(cè)試,丸趣 TV 小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注丸趣 TV 行業(yè)資訊頻道。