三十七、【进阶】SQL的explain

1、explain

2、基础使用

在使用explain关键字时,只需要在所执行语句前加上explain即可

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

3、id

4、select_type

5、type

(1)当根据主键or唯一索引进行访问时,type参数会显示为const:

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

(2)当不查询任何表时,type参数显示为NULL:

sql 复制代码
mysql> explain select 'a';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
|  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
1 row in set, 1 warning (0.00 sec)

(3)使用非唯一性索引,type参数通常显示为ref:

6、possible_key

显示这张表上可能用到的索引,一个或者多个

7、Key

实际使用的索引,如果为NULL,则没有使用索引

8、Key_len

表示索引中使用的字节数,该值为索引字段最大可能长度,并非实际使用长度,在不损失精确性的前提下,长度越短越好

9、rows

MySQL认为必须要执行查询的行数,在innodb引擎的表中,是一个估计值,可能并不总是准确的。

10、filtered

表示返回结果的行数栈读取行数的百分比,filtered的值越大越好

相关推荐
Lyyaoo.18 分钟前
Spring Boot Validation 声明式参数校验
spring boot·后端·mysql
南城以南溫暖如初1472 小时前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
smilejingwei6 小时前
Trae+SQLazy 实践 SQL 国产化移植:Oracle => 达梦
数据库·sql·oracle
hunterandroid6 小时前
Android WebView JSBridge 治理实战:从线上白屏崩溃到协议化通信
android·前端
一个用户名i6 小时前
【Compose 系列】第 1 篇:认识 Compose,为什么要学它
android·android jetpack
冰之杍7 小时前
MySQL utf8mb3 → utf8mb4 完整修改方案
数据库·mysql
极客猴子8 小时前
iPhone实时转写软件推荐:会议录音功能真实体验
android·人工智能·飞书
这个DBA有点耶8 小时前
一文讲透数据库分类:关系型、非关系型、OLTP、OLAP、分布式、多模……
数据库·mysql·架构
达令哥8 小时前
告别 ARouter!基于 Google 官方 Navigation 3 + KSP 打造 Compose 时代的双轨制路由框架
android·前端
YF02118 小时前
Android App启动与权限管控
android