当前位置: 首页 > news >正文

mysql 执行计划 type详解

文章目录

        • 前言
        • all
        • index
        • range
        • index_subquery
        • unique_subquery
        • index_merge
        • ref_or_null
        • ref
        • eq_ref
        • const
        • system

前言

当前mysql版本为8.0.23

# 测试数据和结构如下

# 创建user表
CREATE TABLE `user` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `name` varchar(20) DEFAULT 'xie' COMMENT '名称',
  `age` int unsigned DEFAULT '18' COMMENT '年龄',
  PRIMARY KEY (`id`),
  KEY `idx_user_deleted_at` (`deleted_at`),
  KEY `index_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

# user表插入测试数据
INSERT INTO `user` (`id`, `created_at`, `updated_at`, `deleted_at`, `name`, `age`) VALUES (1,'2022-09-05 19:27:09.468','2022-09-05 19:27:09.468',NULL,'xie',27),(2,'2022-09-05 19:27:09.468','2022-09-05 19:27:09.468',NULL,'rui',28),(3,'2022-09-05 19:27:09.468','2022-09-05 19:27:09.468',NULL,'rui',29),(4,'2022-09-12 13:41:05.000','2022-09-12 13:41:08.000',NULL,'m',22),(5,'2022-09-12 13:41:17.000','2022-09-12 13:41:20.000',NULL,'g',21),(6,'2022-09-12 13:41:30.000','2022-09-12 13:41:32.000',NULL,'l',20);

# 创建log表
CREATE TABLE `log` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_name` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_user_name` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

# log表插入测试数据
INSERT INTO `log` (`id`, `user_name`) VALUES (2,'rui'),(1,'xie');

执行效率 system > const > eq_ref > ref > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > all

all

全表扫描

mysql> desc select * from user;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | user  | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

index

全索引扫描,扫描全部索引来获取数据

mysql> desc select id from user;
+----+-------------+-------+------------+-------+---------------+---------------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key                 | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+---------------------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | user  | NULL       | index | NULL          | idx_user_deleted_at | 8       | NULL |    6 |   100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

range

扫描部分索引来获取数据。
注意数据要稍微多一点,否则mysql可能认为还不如走全索引呢,导致出现的type 是 index

mysql> desc select id from user where id between 1 and 2;
+----+-------------+-------+------------+-------+-----------------------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type  | possible_keys               | key     | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+-------+------------+-------+-----------------------------+---------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | user  | NULL       | range | PRIMARY,idx_user_deleted_at | PRIMARY | 8       | NULL |    2 |   100.00 | Using where; Using index |
+----+-------------+-------+------------+-------+-----------------------------+---------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

index_subquery

使用索引关联字查询

mysql> explain select * from user where name not in (select name from user);
+----+--------------------+-------+------------+----------------+---------------+------------+---------+------+------+----------+-------------------------------------------------+
| id | select_type        | table | partitions | type           | possible_keys | key        | key_len | ref  | rows | filtered | Extra                                           |
+----+--------------------+-------+------------+----------------+---------------+------------+---------+------+------+----------+-------------------------------------------------+
|  1 | PRIMARY            | user  | NULL       | ALL            | NULL          | NULL       | NULL    | NULL |    6 |   100.00 | Using where                                     |
|  2 | DEPENDENT SUBQUERY | user  | NULL       | index_subquery | index_name    | index_name | 63      | func |    2 |   100.00 | Using where; Using index; Full scan on NULL key |
+----+--------------------+-------+------------+----------------+---------------+------------+---------+------+------+----------+-------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

unique_subquery

和 index_subquery 类似,只不过 unique_subquery 提供的是唯一索引 ,index_subquery 提供的是普通索引

mysql> explain select * from user where name not in (select id from user);
+----+--------------------+-------+------------+-----------------+---------------+---------+---------+------+------+----------+-------------------------------------------------+
| id | select_type        | table | partitions | type            | possible_keys | key     | key_len | ref  | rows | filtered | Extra                                           |
+----+--------------------+-------+------------+-----------------+---------------+---------+---------+------+------+----------+-------------------------------------------------+
|  1 | PRIMARY            | user  | NULL       | ALL             | NULL          | NULL    | NULL    | NULL |    6 |   100.00 | Using where                                     |
|  2 | DEPENDENT SUBQUERY | user  | NULL       | unique_subquery | PRIMARY       | PRIMARY | 8       | func |    1 |   100.00 | Using where; Using index; Full scan on NULL key |
+----+--------------------+-------+------------+-----------------+---------------+---------+---------+------+------+----------+-------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

index_merge

查询中使用到多个索引组合使用

explain select * from user where id between 1 and 3  and name = "xie";
+----+-------------+-------+------------+-------------+--------------------+--------------------+---------+------+------+----------+--------------------------------------------------+
| id | select_type | table | partitions | type        | possible_keys      | key                | key_len | ref  | rows | filtered | Extra                                            |
+----+-------------+-------+------------+-------------+--------------------+--------------------+---------+------+------+----------+--------------------------------------------------+
|  1 | SIMPLE      | user  | NULL       | index_merge | PRIMARY,index_name | index_name,PRIMARY | 71,8    | NULL |    1 |   100.00 | Using intersect(index_name,PRIMARY); Using where |
+----+-------------+-------+------------+-------------+--------------------+--------------------+---------+------+------+----------+--------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

ref_or_null

允许值可以为null(可以是null也可以是其它值)

mysql> explain select * from user where name = "xie" or name is null;
+----+-------------+-------+------------+-------------+---------------+------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type        | possible_keys | key        | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-------+------------+-------------+---------------+------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | user  | NULL       | ref_or_null | index_name    | index_name | 63      | const |    2 |   100.00 | Using index condition |
+----+-------------+-------+------------+-------------+---------------+------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

ref

使用非唯一性索引进行查询

mysql> explain select * from user where name = "xie";
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key        | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | user  | NULL       | ref  | index_name    | index_name | 63      | const |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

eq_ref

使用唯一性索引进行查找

mysql> explain select * from user where id in (select id from log);
+----+-------------+-------+------------+--------+---------------+-----------------+---------+--------------+------+----------+-------------+
| id | select_type | table | partitions | type   | possible_keys | key             | key_len | ref          | rows | filtered | Extra       |
+----+-------------+-------+------------+--------+---------------+-----------------+---------+--------------+------+----------+-------------+
|  1 | SIMPLE      | log   | NULL       | index  | PRIMARY       | index_user_name | 603     | NULL         |    2 |   100.00 | Using index |
|  1 | SIMPLE      | user  | NULL       | eq_ref | PRIMARY       | PRIMARY         | 8       | proxy.log.id |    1 |   100.00 | Using where |
+----+-------------+-------+------------+--------+---------------+-----------------+---------+--------------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

const

这个表最多只有一个匹配行

mysql> explain select * from user where id = 3;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | user  | NULL       | const | PRIMARY       | PRIMARY | 8       | const |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

system

表中只有一条数据,等同于系统表
在myisam存储引擎下可以模拟出,平时也可以不用考虑

相关文章:

  • Java进阶篇之泛型
  • 发送post请求渲染el-table,并实现搜索和分页功能
  • [RK3568 Android11] 时间同步机制
  • 6、Mybatis-Plus wrapper的使用
  • 基于Web的爬虫系统设计与实现
  • Kubernets---配置 Pod 使用投射卷作存储
  • springcloud和分布式微服务学习笔记
  • 【“在路上”疫情信息检测】
  • `算法知识` 模意义下的乘法逆元
  • 微信小程序 | 游戏开发之接宝石箱子游戏
  • 丙烯酰氧乙基三甲基氯化铵(DAC)接枝聚苯乙烯伯胺微球微粒/聚苯乙烯包覆硅胶复合微球
  • 拿下Transformer
  • WPS-系统右键:开启后无法显示
  • C++学习笔记(1)--- 常量、数据类型、运算符
  • AI在工业机器人系统中的应用
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Apache的80端口被占用以及访问时报错403
  • gcc介绍及安装
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • java取消线程实例
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Linux下的乱码问题
  • PAT A1092
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • Service Worker
  • spring boot下thymeleaf全局静态变量配置
  • sublime配置文件
  • 阿里云应用高可用服务公测发布
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 来,膜拜下android roadmap,强大的执行力
  • 类orAPI - 收藏集 - 掘金
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 思维导图—你不知道的JavaScript中卷
  • 一些css基础学习笔记
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #WEB前端(HTML属性)
  • ${ }的特别功能
  • (1)常见O(n^2)排序算法解析
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (三分钟)速览传统边缘检测算子
  • (微服务实战)预付卡平台支付交易系统卡充值业务流程设计
  • (一)UDP基本编程步骤
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)mysql使用Navicat 导出和导入数据库
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • ***通过什么方式***网吧
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法