InnoDB ICP 代码路径

简介:

本文简单记录下和Index Condition Pushdown相关的代码路径

涉及的代码只包含InnoDB层

当MySQL使用索引进行数据检索时,不可用于在Innodb进行索引检索的WHERE条件,也可以下推到Innodb层,以减少回表查询的数据量

#对于innodb表,ICP只应用于二级索引

#在MySQL5.6里还不支持对分区表ICP(5.7支持)

MySQL版本:5.7.5

1.创建测试表

create table t1 (a int auto_increment primary key, b int, c int, d int, key(b,c));

生成数据:

insert into t1(b,c,d) select rand()*100, rand()*1000, rand()*10000;

insert into t1(b,c,d) select rand()*100, rand()*1000, rand()*10000 from t1;

insert into t1(b,c,d) select rand()*100, rand()*1000, rand()*10000 from t1;

……

root@sb1 01:57:06>explain select * from t1 where b = 90 and c > 500\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
partitions: NULL
type: range
possible_keys: b
key: b
key_len: 10
ref: NULL
rows: 168
filtered: 100.00
Extra: Using index condition
1 row in set, 1 warning (0.00 sec)

Tips: 从MySQL5.7.3开始,explain的输出增加了两列:partitions用户分区表的查询优化的分区选择,filtered表示根据当前查询计划的过滤性百分比,计算方式参考函数Explain_join::explain_rows_and_filtered()

1.optimizer确认是否可以ICP

堆栈:

handle_select –> mysql_select –> mysql_prepare_and_optimize_select

–> JOIN::optimize –> make_join_readinfo

–> QEP_TAB::push_index_cond

–> ha_innobase::idx_cond_push // 初始化icp的条件pushed_idx_cond 和使用到的索引号pushed_idx_cond_keyno,设置标记in_range_check_pushed_down为true

根据函数QEP_TAB::push_index_cond的注释,只有满足如下条件时,才会使用ICP:

/*
We will only attempt to push down an index condition when the
following criteria are true:
0. The table has a select condition
1. The storage engine supports ICP.
2. The system variable for enabling ICP is ON.
3. The query is not a multi-table update or delete statement. The reason
for this requirement is that the same handler will be used
both for doing the select/join and the update. The pushed index
condition might then also be applied by the storage engine
when doing the update part and result in either not finding
the record to update or updating the wrong record.
4. The JOIN_TAB is not part of a subquery that has guarded conditions
that can be turned on or off during execution of a ‘Full scan on NULL
key’.
@see Item_in_optimizer::val_int()
@see subselect_single_select_engine::exec()
@see TABLE_REF::cond_guards
@see setup_join_buffering
5. The join type is not CONST or SYSTEM. The reason for excluding
these join types, is that these are optimized to only read the
record once from the storage engine and later re-use it. In a
join where a pushed index condition evaluates fields from
tables earlier in the join sequence, the pushed condition would
only be evaluated the first time the record value was needed.
6. The index is not a clustered index. The performance improvement
of pushing an index condition on a clustered key is much lower
than on a non-clustered key. This restriction should be
re-evaluated when WL#6061 is implemented.
*/

ha_innobase::build_template

// 初始化prebuilt的idx_cond, 如果使用ICP,指向ha_innobase对象,否则为NULL,后续根据该列是否为NULL来判定

// 初始化prebuilt->idx_cond_n_cols,即使用到的索引列条件列数

2. 检查ICP条件

当从二级索引检索到数据时,需要根据icp条件进行记录过滤

堆栈:

ha_innobase::index_read

|–>row_search_mvcc

|–>row_search_idx_cond_check //当读取到一条合法的二级索引记录后,需要进行下推条件检查

// 将读取到的二级索引记录上,作为condition的列值转换为MySQL Format

// innobase_index_cond:判断是否满足索引条件

// 当满足索引条件时,如果无需回聚集索引查询,则将剩余的索引记录直接转换成MySQL Format

|–> row_sel_get_clust_rec_for_mysql //当找到满足条件的二级索引记录时,再根据其查询对应的聚集索引记录


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
存储 网络协议 关系型数据库
MySQL8.4创建keyring给InnoDB表进行静态数据加密
MySQL8.4创建keyring给InnoDB表进行静态数据加密
697 1
|
存储 关系型数据库 MySQL
介绍MySQL的InnoDB引擎特性
总结而言 , Inno DB 引搞 是 MySQL 中 高 性 能 , 高 可靠 的 存 储选项 , 宽泛 应用于要求强 复杂交易处理场景 。
569 15
|
存储 缓存 关系型数据库
【MySQL进阶篇】存储引擎(MySQL体系结构、InnoDB、MyISAM、Memory区别及特点、存储引擎的选择方案)
MySQL的存储引擎是其核心组件之一,负责数据的存储、索引和检索。不同的存储引擎具有不同的功能和特性,可以根据业务需求 选择合适的引擎。本文详细介绍了MySQL体系结构、InnoDB、MyISAM、Memory区别及特点、存储引擎的选择方案。
2782 57
【MySQL进阶篇】存储引擎(MySQL体系结构、InnoDB、MyISAM、Memory区别及特点、存储引擎的选择方案)
|
存储 Oracle 关系型数据库
【赵渝强老师】MySQL InnoDB的数据文件与重做日志文件
本文介绍了MySQL InnoDB存储引擎中的数据文件和重做日志文件。数据文件包括`.ibd`和`ibdata`文件,用于存放InnoDB数据和索引。重做日志文件(redo log)确保数据的可靠性和事务的持久性,其大小和路径可由相关参数配置。文章还提供了视频讲解和示例代码。
646 11
【赵渝强老师】MySQL InnoDB的数据文件与重做日志文件
|
SQL 缓存 关系型数据库
使用温InnoDB缓冲池启动MySQL测试
使用温InnoDB缓冲池启动MySQL测试
403 0
|
存储 关系型数据库 MySQL
MySQL存储引擎详述:InnoDB为何胜出?
MySQL 是最流行的开源关系型数据库之一,其存储引擎设计是其高效灵活的关键。InnoDB 作为默认存储引擎,支持事务、行级锁和外键约束,适用于高并发读写和数据完整性要求高的场景;而 MyISAM 不支持事务,适合读密集且对事务要求不高的应用。根据不同需求选择合适的存储引擎至关重要,官方推荐大多数场景使用 InnoDB。
1208 7
|
存储 关系型数据库 MySQL
Mysql索引:深入理解InnoDb聚集索引与MyisAm非聚集索引
通过本文的介绍,希望您能深入理解InnoDB聚集索引与MyISAM非聚集索引的概念、结构和应用场景,从而在实际工作中灵活运用这些知识,优化数据库性能。
909 7
|
存储 关系型数据库 MySQL
MySQL引擎InnoDB和MyISAM的区别?
InnoDB是MySQL默认的事务型存储引擎,支持事务、行级锁、MVCC、在线热备份等特性,主索引为聚簇索引,适用于高并发、高可靠性的场景。MyISAM设计简单,支持压缩表、空间索引,但不支持事务和行级锁,适合读多写少、不要求事务的场景。
585 9