explain select * from staff where first_name='Mike'; -- yes
explain select * from staff where last_name='Hillyer'; -- no
explain select * from staff where first_name='Mike' and last_name='Hillyer'; -- yes
explain select * from staff where last_name='Hillyer' and first_name='Mike'; -- yes(内部优化)
explain select * from staff where first_name='Mike' or last_name='Hillyer'; -- no
explain select * from staff where first_name='Mike' and(last_name='Hillyer' or last_name=''); -- yes
复制代码
索引(条件)下推:即ICP,全称是Index Condition Pushdown Optimization,官网的解释在这里。我们一般叫索引下推,其实正式应该称为:索引条件下推。
怎么理解?下推什么呢? 顾名思义,Condition Pushdown,把查询条件往下推。官网的这句:
With ICP enabled, ... , the MySQL server pushes this part of the WHERE condition down to the storage engine.
翻译即是:ICP启用后,把where条件的部分从server层下推到storage engine层。
需要先了解MySQL的大概架构:
就是,原来where条件筛选在Server层这里,现在下推到存储引擎层去。
举例:
下表中,id是主键,name,age是联合索引。
查找姓张且年龄是10岁的记录:select * from tuser where name like '张%' and age=10;
没有使用ICP:二级索引找到主键1和4,分别回表去查找对应的完整记录,Server层再根据where条件的age=10进行筛选。这个过程要回表两次。
使用ICP: 二级索引找到主键1和4,存储引擎层(Server的下层)根据联合索引where条件age=10进行筛选。根据筛选结果再回表查到完整记录。这个过程回表1次。
上面的ICP举例和图片出自这里。
使用执行计划分析时,使用索引下推在Extra栏位会出现:Using index condition信息,具体参见。
MySQL默认启用索引(条件)下推。系统设置变量为:index_condition_pushdown