一篇文章带你了解MySQL索引下推

2022-10-22,,,,

前言

本文围绕这三个话题来学习索引下推

  • select 语句执行过程
  • 什么是索引下推?
  • 索引下推限制

select 语句执行过程

mysql 数据库由 server 层和 engine 层组成:

  • server 层:sql 分析器、sql 优化器、sql 执行器,用于负责 sql 语句的具体执行过程。
  • engine 层: 负责存储具体的数据,如最常使用的 innodb 存储引擎,还有用于在内存中存储临时结果集的 temptable 引擎。

  • 通过客户端/服务器通信协议与 mysql 建立连接。

  • 查询缓存:

    • 如果开启了 query cache 且在查询缓存过程中查询到完全相同的 sql 语句,则将查询结果直接返回给客户端;
    • 如果没有开启 query cache 或者没有查询到完全相同的 sql 语句则会由解析器进行语法语义解析,并生成解析树。
  • 分析器生成新的解析树。

  • 查询优化器生成执行计划。

  • 查询执行引擎执行 sql 语句,此时查询执行引擎会根据 sql 语句中表的存储引擎类型,以及对应的 api 接口与底层存储引擎缓存或者物理文件的交互情况,得到查询结果,由 mysql server 过滤后将查询结果缓存并返回给客户端。

    若开启了 query cache,这时也会将 sql 语句和结果完整地保存到 query cache 中,以后若有相同的 sql 语句执行则直接返回结果。

tipsmysql 8.0 已去掉 query cache(查询缓存模块)。

因为查询缓存的命中率会非常低。 查询缓存的失效非常频繁:只要有对一个表的更新,这个表上所有的查询缓存都会被清空。

什么是索引下推?

索引下推(index condition pushdown): 简称 icp,通过把索引过滤条件下推到存储引擎,来减少 mysql 存储引擎访问基表的次数 和 mysql 服务层访问存储引擎的次数。

索引下推 vs 覆盖索引: 其实都是 减少回表的次数,只不过方式不同

  • 覆盖索引: 当索引中包含所需要的字段(select xxx),则不再回表去查询字段。

  • 索引下推: 对索引中包含的字段先做判断,直接过滤掉不满足条件的记录,减少回表的行数。

要了解 icp 是如何工作的,先从一个查询 sql 开始:

举个栗子:查询名字 la 开头、年龄为 18 的记录

select * from user where name like 'la%' and age = 18;

有这些记录:

不开启 icp 时索引扫描是如何进行的:

  • 通过索引元组,定位读取对应数据行。(实际上:就是回表)
  • where 中字段做判断,过滤掉不满足条件的行。

使用 icp,索引扫描如下进行:

  • 获取索引元组。
  • where 中字段做判断,在索引列中进行过滤。
  • 对满足条件的索引,进行回表查询整行。
  • where 中字段做判断,过滤掉不满足条件的行。

动手实验:

实验:使用 mysql 版本 8.0.16

-- 表创建
create table if not exists `user` (
`id` varchar(64) not null comment '主键 id',
`name` varchar(50) not null comment '名字',
`age` tinyint not null comment '年龄',
`address` varchar(100) not null comment '地址',
primary key (id)
) engine=innodb default charset utf8mb4 collate=utf8mb4_unicode_ci comment '用户表';

-- 创建索引
create index idx_name_age on user (name, age);

-- 新增数据
insert into user (id, name, age, address) values (1, 'tt', 14, 'linhai');
insert into user (id, name, age, address) values (2, 'lala', 18, 'linhai');
insert into user (id, name, age, address) values (3, 'laxi', 30, 'linhai');
insert into user (id, name, age, address) values (4, 'lawa', 40, 'linhai');

-- 查询语句
select * from user where name like 'la%' and age = 18;

新增数据如下:

  • 关闭 icp,再调用 explain 查看语句:
-- 将 icp 关闭
set optimizer_switch = 'index_condition_pushdown=off';
-- 查看确认
show variables like 'optimizer_switch';

-- 用 explain 查看
explain select * from user where name like 'la%' and age = 18;

  • 开启 icp,再调用 explain 查看语句:
-- 将 icp 打开
set optimizer_switch = 'index_condition_pushdown=on';
-- 查看确认
show variables like 'optimizer_switch';

-- 用 explain 查看
explain select * from user where name like 'la%' and age = 18;

由上实验可知,区别是否开启 icp exira 字段中的 using index condition

更进一步,来看下 icp 带来的性能提升:

通过访问数据文件的次数

-- 1. 清空 status 状态
flush status;
-- 2. 查询
select * from user where name like 'la%' and age = 18;
-- 3. 查看 handler 状态
show status like '%handler%';

对比开启 icp 和 关闭 icp 关注 handler_read_next 的值

-- 开启 icp
flush status;
select * from user where name like 'la%' and age = 18;
show status like '%handler%';
+----------------------------|-------+
| variable_name              | value |
+----------------------------|-------+
| handler_commit             | 1     |
| handler_delete             | 0     |
| handler_discover           | 0     |
| handler_external_lock      | 2     |
| handler_mrr_init           | 0     |
| handler_prepare            | 0     |
| handler_read_first         | 0     |
| handler_read_key           | 1     |  
| handler_read_last          | 0     |
| handler_read_next          | 1     |  <---重点
| handler_read_prev          | 0     |
| handler_read_rnd           | 0     |
| handler_read_rnd_next      | 0     |
| handler_rollback           | 0     |
| handler_savepoint          | 0     |
| handler_savepoint_rollback | 0     |
| handler_update             | 0     |
| handler_write              | 0     |
+----------------------------|-------+
18 rows in set (0.00 sec)


-- 关闭 icp
flush status;
select * from user where name like 'la%' and age = 18;
show status like '%handler%';
+----------------------------|-------+
| variable_name              | value |
+----------------------------|-------+
| handler_commit             | 1     |
| handler_delete             | 0     |
| handler_discover           | 0     |
| handler_external_lock      | 2     |
| handler_mrr_init           | 0     |
| handler_prepare            | 0     |
| handler_read_first         | 0     |
| handler_read_key           | 1     |
| handler_read_last          | 0     |
| handler_read_next          | 3     |  <---重点
| handler_read_prev          | 0     |
| handler_read_rnd           | 0     |
| handler_read_rnd_next      | 0     |
| handler_rollback           | 0     |
| handler_savepoint          | 0     |
| handler_savepoint_rollback | 0     |
| handler_update             | 0     |
| handler_write              | 0     |
+----------------------------|-------+
18 rows in set (0.00 sec)

由上实验可知:

  • 开启 icphandler_read_next 等于 1,回表查 1 次。
  • 关闭 icphandler_read_next 等于 3,回表查 3 次。

这实验跟上面的栗子就对应上了。

索引下推限制

根据官网可知,索引下推 受以下条件限制:

  • 当需要访问整个表行时,icp 用于 rangerefeq_refref_or_null

  • icp可以用于 innodbmyisam 表,包括分区表 innodbmyisam 表。

  • 对于 innodb 表,icp 仅用于二级索引。icp 的目标是减少全行读取次数,从而减少 i/o 操作。对于 innodb 聚集索引,完整的记录已经读入 innodb 缓冲区。在这种情况下使用 icp 不会减少 i/o

  • 在虚拟生成列上创建的二级索引不支持 icpinnodb 支持虚拟生成列的二级索引。

  • 引用子查询的条件不能下推。

  • 引用存储功能的条件不能被按下。存储引擎不能调用存储的函数。

  • 触发条件不能下推。

  • 不能将条件下推到包含对系统变量的引用的派生表。(mysql 8.0.30 及更高版本)。

小结下:

  • icp 仅适用于 二级索引
  • icp 目标是 减少回表查询
  • icp 对联合索引的部分列模糊查询非常有效。

拓展:虚拟列

create table userlogin (
userid bigint,
logininfo json,
cellphone varchar(255) as (logininfo->>"$.cellphone"),
primary key(userid),
unique key idx_cellphone(cellphone)
);

cellphone :就是一个虚拟列,它是由后面的函数表达式计算而成,本身这个列不占用任何的存储空间,而索引 idx_cellphone 实质是一个函数索引

好处: 在写 sql 时可以直接使用这个虚拟列,而不用写冗长的函数。

举个栗子: 查询手机号

-- 不用虚拟列
select * from userlogin where logininfo->>"$.cellphone" = '13988888888'

-- 使用虚拟列
select * from userlogin where cellphone = '13988888888'

总结

到此这篇关于mysql索引下推的文章就介绍到这了,更多相关mysql索引下推内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《一篇文章带你了解MySQL索引下推.doc》

下载本文的Word格式文档,以方便收藏与打印。