三十七、【进阶】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的值越大越好

相关推荐
bksczm21 分钟前
MySQL之存储引擎
数据库·mysql
橙光成长1 小时前
android-ad-cleaner:android免 Root 批量关闭系统开屏与营销推送
android·1024程序员节
计算机毕设定制辅导-无忧学长1 小时前
《基于微信小程序健康习惯激励系统设计与实现》
java·spring boot·mysql·微信小程序·uniapp·健康习惯激励系统
深圳雨林凯AI1 小时前
图案裂变的底色衰减:alpha通道、亮度对比与白墨分色的量化拆解
android
大模型最新论文速读1 小时前
text-to-sql:从学术玩具到生产落地
数据库·sql
mmsx1 小时前
Android 模块化收口:能力组合框架、手写 LifecycleOwner 与热修复共存
android
Decay__2 小时前
15_MySQL 数据同步与迁移方案 —— 复制 / DataX / Canal / 异构同步(对标 Oracle GoldenGate)
运维·数据库·sql·mysql·架构
zjx23882 小时前
oracle11g转Gbase8s库SQL常见问题修改
数据库·sql
雪芽蓝域zzs2 小时前
第 2 章:整合 MySQL + MyBatis,搭建数据库基础、实体类、Mapper、测试查询
数据库·mysql·mybatis
杨云龙UP3 小时前
Oracle 同义词(SYNONYM)详解及 Toad for Oracle SQL 智能补全配置实战
运维·数据库·sql·oracle·同义词·toad·toad for oracle