自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL索引條件下推的簡單測試

數(shù)據(jù)庫 MySQL
自MySQL 5.6開始,在索引方面有了一些改進,比如索引條件下推(Index condition pushdown,ICP),嚴格來說屬于優(yōu)化器層面的改進。

自MySQL 5.6開始,在索引方面有了一些改進,比如索引條件下推(Index condition pushdown,ICP),嚴格來說屬于優(yōu)化器層面的改進。

[[196455]]

如果簡單來理解,就是優(yōu)化器會盡可能的把index condition的處理從Server層下推到存儲引擎層。舉一個例子,有一個表中含有組合索引idx_cols包含(c1,c2,…,cn)n個列,如果在c1上存在范圍掃描的where條件,那么剩余的c2,…,cn這n-1個上索引都無法用來提取和過濾數(shù)據(jù),而ICP就是把這個事情優(yōu)化一下。

我們在MySQL 5.6的環(huán)境中來簡單測試一下。

我們創(chuàng)建表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; 

當然我也隨機插入了幾條數(shù)據(jù),意思一下。

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

ICP的控制在數(shù)據(jù)庫參數(shù)中有一個優(yōu)化器參數(shù)optimizer_switch來統(tǒng)一管理,我想這也是MySQL優(yōu)化器離我們最貼近的時候了??梢允褂萌缦碌姆绞絹聿榭础?/p>

  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下面我們就用兩個語句來對比說明一下,就通過執(zhí)行計劃來對比。

  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)如果你觀察仔細,會發(fā)現(xiàn)兩次的語句還是不同的,那就是范圍掃描的范圍不同,如果還是用原來的語句,結果還是有一定的限制的。

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

責任編輯:武曉燕 來源: Linux社區(qū)
相關推薦

2017-07-17 09:29:41

MySQL索引測試

2021-02-03 08:52:52

Mysql索引數(shù)據(jù)庫

2024-12-24 14:11:57

2024-03-25 13:02:00

MySQL索引主鍵

2010-05-31 13:38:17

2011-07-27 17:22:10

mysql極限測試索引

2010-01-13 11:27:06

C++安全性

2015-06-25 10:06:05

軟件定義數(shù)據(jù)中心

2020-09-23 13:22:46

Raccoon攻擊

2020-11-20 06:13:04

Like %

2025-04-08 07:30:40

數(shù)據(jù)庫對象索引

2023-05-09 11:28:03

雷達技術

2021-08-30 07:49:33

索引ICP Mysql

2025-04-28 07:10:46

聚簇非聚簇索引

2024-05-24 09:28:22

2021-12-09 07:22:52

索引下推前綴

2018-11-05 09:00:13

蘇寧易購數(shù)據(jù)分析

2015-06-24 10:04:50

軟件定義數(shù)據(jù)中心基礎設施

2019-08-08 14:40:18

開發(fā)者技能工具

2015-08-10 15:51:03

數(shù)據(jù)中心
點贊
收藏

51CTO技術棧公眾號