mysql和explain哪个好(MySQL EXPLAIN输出列的详细解释)
mysql和explain哪个好
MySQL EXPLAIN输出列的详细解释1. 简介
EXPLAIN语句提供有关 MySQL 如何执行语句的信息。
EXPLAIN与SELECT、DELETE、INSERT、REPLACE和UPDATE语句一起使用。
|
mysql> EXPLAIN SELECT * FROM employees WHERE emp_no = 10001; +----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | employees | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set , 1 warning (0.00 sec) |
简单来讲,通过EXPLAIN可以分析出SQL语句走没走索引,走的是什么索引。
EXPLAIN为SELECT语句中使用的每个表返回一行信息,它按照 MySQL 在处理语句时读取它们的顺序列出了输出中的表。
MySQL 使用嵌套循环连接(Nested-Loop Join Algorithms)解析所有连接,这意味着 MySQL 从第一个表中读取一行,然后在第二个表,第三个表中找到匹配的行,依此类推。处理完所有表后,MySQL将通过表列表输出选定的列后回溯直到找到一个表,其中存在更多匹配的行。从该表中读取下一行,然后继续下一个表。
2.EXPLAIN 输出列
- MySQL版本 5.7.33
- Windows10 64位
从上图看到 EXPLAIN 的结果中,包括的表头id、select_type、table、partitions、type、possible_keys、key、key_len、ref、rows、filtered、Extra,这些字段的意思我们来学习然后通过实例进行了解一下。
2.1 id
SELECT 标识符,查询中 SELECT 的顺序号。如果该行引用其他行的并集结果,则该值可以为NULL。在这种情况下,表列显示类似<unionM,N>的值,以指示该行引用 id 值为 M 和 N 的行的并集。
id 值分三种情况:
id 相同,执行顺序由上至下
|
mysql> EXPLAIN ( -> SELECT * FROM employees emp -> LEFT JOIN dept_emp de ON emp.emp_no = de.emp_no -> LEFT JOIN departments dept ON dept.dept_no = de.dept_no -> WHERE emp.emp_no = 10001); + ----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + ----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+-------+ | 1 | SIMPLE | emp | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | | 1 | SIMPLE | de | NULL | ref | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | | 1 | SIMPLE | dept | NULL | eq_ref | PRIMARY | PRIMARY | 12 | employees.de.dept_no | 1 | 100.00 | NULL | + ----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+-------+ 3 rows in set , 1 warning (0.03 sec) |
id不相同,如果是子查询,id的序号会递增,id的值越大被执行的优先级越高
|
mysql> EXPLAIN SELECT * FROM employees emp -> WHERE emp.emp_no NOT IN ( SELECT de.emp_no FROM dept_emp de -> WHERE de.dept_no NOT IN ( SELECT dept_no FROM departments WHERE dept_name = 'Development' )); + ----+-------------+-------------+------------+-------+-------------------+-----------+---------+-------+--------+----------+--------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + ----+-------------+-------------+------------+-------+-------------------+-----------+---------+-------+--------+----------+--------------------------+ | 1 | PRIMARY | emp | NULL | ALL | NULL | NULL | NULL | NULL | 299468 | 100.00 | Using where | | 2 | SUBQUERY | de | NULL | index | PRIMARY | dept_no | 12 | NULL | 308493 | 100.00 | Using where ; Using index | | 3 | SUBQUERY | departments | NULL | const | PRIMARY ,dept_name | dept_name | 122 | const | 1 | 100.00 | Using index | + ----+-------------+-------------+------------+-------+-------------------+-----------+---------+-------+--------+----------+--------------------------+ 3 rows in set , 1 warning (0.00 sec) |
id相同和不相同都存在
如果id相同可以认为是一组,同一组id执行顺序由上至下,不同组之间,id值越大被执行的优先级越高。
|
mysql> EXPLAIN SELECT * FROM employees emp -> WHERE emp.emp_no IN ( SELECT de.emp_no FROM dept_emp de -> WHERE de.dept_no IN ( SELECT dept_no FROM departments WHERE dept_name LIKE '%Develop%' )); + ----+--------------+-------------+------------+-------+-----------------+-----------+---------+-------------------------------+--------+----------+----------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + ----+--------------+-------------+------------+-------+-----------------+-----------+---------+-------------------------------+--------+----------+----------------------------------------------------+ | 1 | SIMPLE | <subquery2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | 100.00 | NULL | | 1 | SIMPLE | emp | NULL | ALL | PRIMARY | NULL | NULL | NULL | 299468 | 0.00 | Using where ; Using join buffer (Block Nested Loop) | | 2 | MATERIALIZED | departments | NULL | index | PRIMARY | dept_name | 122 | NULL | 9 | 11.11 | Using where ; Using index | | 2 | MATERIALIZED | de | NULL | ref | PRIMARY ,dept_no | dept_no | 12 | employees.departments.dept_no | 38561 | 100.00 | Using index | + ----+--------------+-------------+------------+-------+-----------------+-----------+---------+-------------------------------+--------+----------+----------------------------------------------------+ 4 rows in set , 1 warning (0.01 sec) |
2.2 select_type
查询的类型,主要用来区别普通查询,联合查询,子查询等复杂查询。
包含SIMPLE、PRIMARY、UNION、DEPENDENT UNION、UNION RESULT、SUBQUERY、DEPENDENT SUBQUERY、DERIVED、MATERIALIZED、UNCACHEABLE SUBQUERY、UNCACHEABLE UNION
SIMPLE
简单的SELECT,不使用UNION或子查询。
|
mysql> EXPLAIN select * from employees where emp_no=10001; + ----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + ----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | employees | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | + ----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set , 1 warning (0.00 sec) |
PRIMARY
查询中若包含任何复杂的子部分,最外层的查询则被标记为PRIMARY
|
mysql> EXPLAIN SELECT * FROM employees emp -> WHERE emp.emp_no IN ( SELECT max (emp_no) FROM dept_emp); + ----+--------------------+-------+------------+------+---------------+------+---------+------+--------+----------+------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + ----+--------------------+-------+------------+------+---------------+------+---------+------+--------+----------+------------------------------+ | 1 | PRIMARY | emp | NULL | ALL | NULL | NULL | NULL | NULL | 299468 | 100.00 | Using where | | 2 | DEPENDENT SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away | + ----+--------------------+-------+------------+------+---------------+------+---------+------+--------+----------+------------------------------+ 2 rows in set , 1 warning (0.00 sec) |
UNION
第二个或更靠后的 SELECT 语句出现在 UNION 之后,则被标记为 UNION
|
mysql> EXPLAIN ( SELECT emp_no,dept_no FROM dept_emp LIMIT 10) -> UNION -> SELECT emp_no,dept_no FROM dept_manager; + ----+--------------+--------------+------------+-------+---------------+---------+---------+------+--------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + ----+--------------+--------------+------------+-------+---------------+---------+---------+------+--------+----------+-----------------+ | 1 | PRIMARY | dept_emp | NULL | index | NULL | dept_no | 12 | NULL | 308493 | 100.00 | Using index | | 2 | UNION | dept_manager | NULL | index | NULL | dept_no | 12 | NULL | 24 | 100.00 | Using index | | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary | + ----+--------------+--------------+------------+-------+---------------+---------+---------+------+--------+----------+-----------------+ 3 rows in set , 1 warning (0.00 sec) |
DEPENDENT UNION
与 UNION 相同,它出现在 UNION 或 UNION ALL语句中,但是此查询受外部查询的影响
| UNION RESULT union_result Result of a UNION.
| SUBQUERY None First SELECT in subquery
| DEPENDENT SUBQUERY dependent (true) First SELECT in subquery, dependent on outer query
| DERIVED None Derived table
| MATERIALIZED materialized_from_subquery Materialized subquery
| UNCACHEABLE SUBQUERY cacheable (false) A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
| UNCACHEABLE UNION cacheable (false) The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)
总结
到此这篇关于MySQL EXPLAIN输出列的文章就介绍到这了,更多相关MySQL EXPLAIN输出列内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!
原文链接:https://juejin.cn/post/6960967935724781598?
- mysql根据子节点查询父节点(mysql 递归查找菜单节点的所有子节点的方法)
- mysql各种备份方式(MySQL 逻辑备份与恢复测试的相关总结)
- phpstudy怎么升级mysql(phpStudy中升级MySQL版本到5.7.17的方法步骤)
- mysql字段多有什么问题(MySQL编码不一致可能引起的一些问题)
- mysql事务未提交会锁表吗(Mysql事务中Update是否会锁表?)
- Mysql官方性能测试工具mysqlslap的使用简介(Mysql官方性能测试工具mysqlslap的使用简介)
- mysql主从复制忽略问题(MySQL复制问题的三个参数分析)
- mysql如何给一个表批量添加字段(mysql从一张表查询批量数据并插入到另一表中的完整实例)
- 如何使java与sqlserver数据库连接(java连接mysql数据库 java连接sql server数据库)
- mysql 存储引擎有哪些(MySQL 常见存储引擎的优劣)
- mysql时间戳和datetime对比(MySQL时间设置注意事项的深入总结)
- mysql要先下服务端再下客户端吗(解读MySQL的客户端和服务端协议)
- mysql 使用小结(Mysql Online DDL的使用详解)
- mysql数据字段默认值怎么设置(MySQL 字段默认值该如何设置)
- mysql 查询json(MySQL处理JSON常见函数的使用)
- mysql恢复数据库(MySQL 两种恢复数据的方法)
- 小米音乐可绑定QQ音乐, QQ音乐 真的会消失在小米的设备上吗(小米音乐可绑定QQ音乐)
- 小米Watch S1评测 或许能成为小米冲击高端可穿戴设备的里程碑(小米WatchS1评测或许能成为小米冲击高端可穿戴设备的里程碑)
- 手机QQ与小米路由器在一起 明天揭晓,敬请期待(手机QQ与小米路由器在一起)
- 小米音乐与 QQ 音乐合作,便捷迁移会员(小米音乐与QQ音乐合作)
- 小米推出米兔儿童电话手表奥特曼版,799 元,支持微信 QQ(小米推出米兔儿童电话手表奥特曼版)
- 贾怀胤唱《白龙马》 炸场 了 没想到京剧还能这么玩(贾怀胤唱白龙马)
热门推荐
- 云服务器自建服务器成本比较(云服务器与服务器租用之间的区别在哪里?)
- python中字符串截取规则(Python中的字符串切片截取字符串的详解)
- dedecms官方安装手册(Linux下安装DedeCMS及安全设置教程)
- dedecms列表样式修改(dedecms5.7sp1评论添加字段的实现方法)
- html5应用的几个建议
- jquery filter方法
- h5页面强制关注微信公众号(Html5页面获取微信公众号的openid的方法)
- ant design 的vue批量操作(ant desing vue table 实现可伸缩列的完整例子)
- vmware能安装macos吗(VMware15/16解锁VMware安装MacOS的步骤详解)
- div左右布局自适应(如何让一个div居于页面正中间实现方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9