您好,欢迎访问一九零五行业门户网

MySQL (ICP) 索引条件下推对比Oracle进行说明

第一次看到这个名词,与oracle fpd - filter push-down想到了一块,但是后来才发现他们根本同一个东西,简单的收icp就是当索引包
第一次看到这个名词,与oracle fpd - filter push-down想到了一块,但是后来才发现他们根本同一个东西,简单的收icp就是当索引包含所有的访问字段的时候,可以在根据前导列过滤掉条件的时候,同时过滤掉另外的条件,比如说
create table testicp(a int,b int,c name);
alter table testticp add key(a,b);
select * from testicp where a=1 and b 的时候,如果未使用icp就是通过a=1的条件返回结果集然后通过回表操作后然后过滤掉b
所以及早的过滤掉不需要的数据是非常必要的。在oracle中这也许不是问题,但是mysql知道5.6才引入了icp。
我们先来看看oracle的执行计划
使用脚本:
create table testicp(a int,b int,c varchar2(20));
 declare 
    i number(10);
 begin
  for i in 1..1000
  loop
  insert into testicp
    values(i,i,'gaopeng');
  end loop;
 end;
 select * from testicp where a=1 and b
--------------------------------------------------------------------------------
 plan hash value: 446810821
 --------------------------------------------------------------------------------
 | id  | operation                  | name          | rows  | bytes | cost (%cpu
 --------------------------------------------------------------------------------
 |  0 | select statement            |              |    1 |    38 |    3  (0
 |  1 |  table access by index rowid| testicp      |    1 |    38 |    3  (0
 |*  2 |  index range scan          | testicp_index |    1 |      |    2  (0
 --------------------------------------------------------------------------------
 predicate information (identified by operation id):
 ---------------------------------------------------
    2 - access(a=1 and b
非常加单我们只需要看到access(a=1 and b=1)就知道是通过a=1 and b=1来访问索引的
 如果是filter b=1我们可以理解为访问索引后过滤的。
sql> explain plan for select * from testicp where a=1 and c='gtest';
 explained
sql> select * from table(dbms_xplan.display);
 plan_table_output
 --------------------------------------------------------------------------------
 plan hash value: 446810821
 --------------------------------------------------------------------------------
 | id  | operation                  | name          | rows  | bytes | cost (%cpu
 --------------------------------------------------------------------------------
 |  0 | select statement            |              |    1 |    38 |    3  (0
 |*  1 |  table access by index rowid| testicp      |    1 |    38 |    3  (0
 |*  2 |  index range scan          | testicp_index |    1 |      |    2  (0
 --------------------------------------------------------------------------------
 predicate information (identified by operation id):
 ---------------------------------------------------
    1 - filter(c='gtest')
    2 - access(a=1)
 note
 -----
    - dynamic sampling used for this statement (level=2)
 19 rows selected
如果我们改变为and c='gtest'
可以看到 filter(c='gtest'),这就是所谓的过滤。是索引回表后过滤的。
但这一切在oracle认为理所当然的东西到了mysql到了5.6才实现。我们通过mysql来做一下脚本使用:
create table testicp(a int,b int,c varchar(20));
 delimiter //
 create procedure myproc3()
 begin
 declare num int;
 set num=1;
 while num   insert into testicp  values(num,num,'gaopeng');
  set num=num+1;
 end while;
  end//
  call myproc3() //
  delimiter ;
  alter table testicp add key(a,b);
explain select * from testicp where a=1 and b  mysql> explain select * from testicp where a=1 and b +----+-------------+---------+-------+---------------+------+---------+------+------+-----------------------+
 | id | select_type | table  | type  | possible_keys | key  | key_len | ref  | rows | extra                |
 +----+-------------+---------+-------+---------------+------+---------+------+------+-----------------------+
 |  1 | simple      | testicp | range | a            | a    | 10      | null |    1 | using index condition |
 +----+-------------+---------+-------+---------------+------+---------+------+------+-----------------------+
这里使用关键字using index condition加以说明,他受参数
optimizer_switch='index_condition_pushdown=on'
影响,如果我们设置optimizer_switch='index_condition_pushdown=off'再来看一下
其它类似信息

推荐信息