016:Explain
一. Explain
1.explain说明
explain是解释SQL语句的执行计划,即显示该SQL语句怎么执行的
- 使用
explain的时候,也可以使用desc
- 使用
5.6 版本支持
DML语句进行explain解释5.6 版本开始支持
JSON格式的输出
注意:EXPLAIN查看的是执行计划,做SQL解析,不会去真的执行;且到5.7以后子查询也不会去执行。
(gcdb@localhost) 14:30:50 [mytest]> explain SELECT
-> t.TABLE_SCHEMA,
-> t.TABLE_NAME,
-> s.INDEX_NAME,
-> CARDINALITY,
-> TABLE_ROWS,
-> CARDINALITY / TABLE_ROWS AS SELECTIVITY
-> FROM
-> information_schema.TABLES t,
-> (SELECT
-> table_schema, table_name, index_name, cardinality
-> FROM
-> information_schema.STATISTICS
-> WHERE
-> (table_schema , table_name, index_name, seq_in_index) IN (SELECT
-> table_schema, table_name, index_name, MAX(seq_in_index)
-> FROM
-> information_schema.STATISTICS
-> GROUP BY table_schema , table_name , index_name)) s
-> WHERE
-> t.table_schema = s.table_schema
-> AND t.table_schema = 'employees'
-> AND t.table_name = s.table_name
-> ORDER BY SELECTIVITY;
+----+--------------------+------------+------------+------+---------------+--------------+---------+------+------+----------+--------------------------------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+------------+------------+------+---------------+--------------+---------+------+------+----------+--------------------------------------------------------------------------------------------+
| 1 | PRIMARY | t | NULL | ALL | NULL | TABLE_SCHEMA | NULL | NULL | NULL | NULL | Using where; Open_full_table; Scanned 1 database; Using temporary; Using filesort |
| 1 | PRIMARY | STATISTICS | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using where; Open_full_table; Scanned all databases; Using join buffer (Block Nested Loop) |
| 3 | DEPENDENT SUBQUERY | STATISTICS | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Open_frm_only; Scanned all databases; Using temporary; Using filesort |
+----+--------------------+------------+------------+------+---------------+--------------+---------+------+------+----------+--------------------------------------------------------------------------------------------+
3 rows in set, 1 warning (0.00 sec) -- 有warnings,这里相当于提供一个信息返回
(gcdb@localhost) 14:31:37 [mytest]> show warnings \G; -- 即将被弃用
*************************** 1. row ***************************
Level: Warning
Code: 1681
Message: 'EXTENDED' is deprecated and will be removed in a future release.
*************************** 2. row *************************** -- 显示真正的执行语句
Level: Note
Code: 1003
Message: /* select#1 */ select `t`.`TABLE_SCHEMA` AS `TABLE_SCHEMA`,`t`.`TABLE_NAME` AS `TABLE_NAME`,`information_schema`.`STATISTICS`.`INDEX_NAME` AS `index_name`,`information_schema`.`STATISTICS`.`CARDINALITY` AS `cardinality`,`t`.`TABLE_ROWS` AS `TABLE_ROWS`,(`information_schema`.`STATISTICS`.`CARDINALITY` / `t`.`TABLE_ROWS`) AS `SELECTIVITY` from `information_schema`.`TABLES` `t` join `information_schema`.`STATISTICS` where ((`information_schema`.`STATISTICS`.`TABLE_NAME` = `t`.`TABLE_NAME`) and (`information_schema`.`STATISTICS`.`TABLE_SCHEMA` = `t`.`TABLE_SCHEMA`) and (`t`.`TABLE_SCHEMA` = 'employees') and <in_optimizer>((`information_schema`.`STATISTICS`.`TABLE_SCHEMA`,`information_schema`.`STATISTICS`.`TABLE_NAME`,`information_schema`.`STATISTICS`.`INDEX_NAME`,`information_schema`.`STATISTICS`.`SEQ_IN_INDEX`),<exists>(/* select#3 */ select 1,1,1,1 from `information_schema`.`STATISTICS` group by `information_schema`.`STATISTICS`.`TABLE_SCHEMA`,`information_schema`.`STATISTICS`.`TABLE_NAME`,`information_schema`.`STATISTICS`.`INDEX_NAME` having (((<cache>(`information_schema`.`STATISTICS`.`TABLE_SCHEMA`) = `information_schema`.`STATISTICS`.`TABLE_SCHEMA`) or <cache>(isnull(`information_schema`.`STATISTICS`.`TABLE_SCHEMA`))) and ((<cache>(`information_schema`.`STATISTICS`.`TABLE_NAME`) = `information_schema`.`STATISTICS`.`TABLE_NAME`) or <cache>(isnull(`information_schema`.`STATISTICS`.`TABLE_NAME`))) and ((<cache>(`information_schema`.`STATISTICS`.`INDEX_NAME`) = `information_schema`.`STATISTICS`.`INDEX_NAME`) or <cache>(isnull(`information_schema`.`STATISTICS`.`INDEX_NAME`))) and ((<cache>(`information_schema`.`STATISTICS`.`SEQ_IN_INDEX`) = max(`information_schema`.`STATISTICS`.`SEQ_IN_INDEX`)) or isnull(max(`information_schema`.`STATISTICS`.`SEQ_IN_INDEX`))) and <is_not_null_test>(`information_schema`.`STATISTICS`.`TABLE_SCHEMA`) and <is_not_null_test>(`information_schema`.`STATISTICS`.`TABLE_NAME`) and <is_not_null_test>(`information_schema`.`STATISTICS`.`INDEX_NAME`) and <is_not_null_test>(max(`information_schema`.`STATISTICS`.`SEQ_IN_INDEX`)))))) order by `SELECTIVITY`
2 rows in set (0.00 sec)
- 参数FORMAT
- 使用
format=json不仅仅是为了格式化输出效果,还有其他有用的显示信息。 - 且当5.6版本后,使用
MySQL Workbench,可以使用visual Explain方式显示详细的图示信息。
- 使用
(gcdb@localhost) 16:24:02 [information_schema]> explain format=json SELECT table_schema, table_name, index_name, MAX(seq_in_index) FROM information_schema.STATISTICS GROUP BY table_schema , table_name , index_name \G;
*************************** 1. row ***************************
EXPLAIN: {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "12.50"
},
"grouping_operation": {
"using_temporary_table": true,
"using_filesort": true,
"cost_info": {
"sort_cost": "2.00"
},
"table": {
"table_name": "STATISTICS",
"access_type": "ALL",
"open_frm_only": true,
"scanned_databases": "all",
"used_columns": [
"TABLE_CATALOG",
"TABLE_SCHEMA",
"TABLE_NAME",
"NON_UNIQUE",
"INDEX_SCHEMA",
"INDEX_NAME",
"SEQ_IN_INDEX",
"COLUMN_NAME",
"COLLATION",
"CARDINALITY",
"SUB_PART",
"PACKED",
"NULLABLE",
"INDEX_TYPE",
"COMMENT",
"INDEX_COMMENT"
]
}
}
}
}
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
(gcdb@localhost) 16:25:29 [information_schema]>
2.Explain输出介绍
| 列 | 含义 |
|---|---|
| id | 执行计划的id标志 |
| select_type | SELECT的类型 |
| table | 输出记录的表 |
| partitions | 符合的分区,[PARTITIONS] |
| type | JOIN的类型 |
| possible_keys | 优化器可能使用到的索引 |
| key | 优化器实际选择的索引 |
| key_len | 使用索引的字节长度 |
| ref | 进行比较的索引列 |
| rows | 优化器预估的记录数量 |
| filtered | 根据条件过滤得到的记录的百分比[EXTENDED] |
| extra | 额外的显示选项 |

2.1. id
是指包含一组数字,表示查询中执行select子句或操作表的顺序。
口诀:
id相等的从上往下看,id不等的从下往上看。但是在某些场合也不一定适用
2.2. select_type
| select_type | 含义 |
|---|---|
| SIMPLE | 简单SELECT(不使用UNION或子查询等) |
| PRIMARY | 最外层的select |
| UNION | UNION中的第二个或后面的SELECT语句 |
| DEPENDENT UNION | UNION中的第二个或后面的SELECT语句,依赖于外面的查询 |
| UNION RESULT | UNION的结果 |
| SUBQUERY | 子查询中的第一个SELECT |
| DEPENDENT SUBQUERY | 子查询中的第一个SELECT,依赖于外面的查询 |
| DERIVED | 派生表的SELECT(FROM子句的子查询) |
| MATERIALIZED | 物化子查询 |
| UNCACHEABLE SUBQUERY | 不会被缓存的并且对于外部查询的每行都要重新计算的子查询 |
| UNCACHEABLE UNION | 属于不能被缓存的 UNION中的第二个或后面的SELECT语句 |
- MATERIALIZED
- 产生中间
临时表(实体) - 临时表自动
创建索引并和其他表进行关联,提高性能 - 和子查询的区别是,优化器将可以进行
MATERIALIZED的语句自动改写成join,并自动创建索引
- 产生中间
2.3. table
- 通常是用户操作的用户表
<unionM, N>UNION得到的结果表<derivedN>排生表,由id=N的语句产生<subqueryN>由子查询物化产生的表,由id=N的语句产生
2.4. type
按照图上箭头的顺序来看,成本(cost)是从小到大

2.5. extra

- Using filesort:可以使用复合索引将filesort进行优化。提高性能
- Using index:比如使用覆盖索引
- Using where: 使用where过滤条件
Extra的信息是可以作为优化的提示,但是更多的是优化器优化的一种说明
016:Explain的更多相关文章
- MYSQL数据库性能调优之三:explain分析慢查询
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句.使用方法,在select语句前加上explain就可以了. 一.explain ...
- explain之二:Explain 结果解读与实践,分析诊断工具之二
MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...
- Hive底层原理:explain执行计划详解
不懂hive中的explain,说明hive还没入门,学会explain,能够给我们工作中使用hive带来极大的便利! 理论 本节将介绍 explain 的用法及参数介绍 HIVE提供了EXPLAIN ...
- MySQL:explain 和 慢查询日志
1. 执行SQL时显示执行情况 explain + SQL语句 2. 强制使用索引 select * from t force index (a) where a between 1 ...
- MongoDB分析工具之一:explain()语句分析工具
explain(),语句分析工具 MongoDB 3.0之后,explain的返回与使用方法与之前版本有了很大的变化,介于3.0之后的优秀特色和我们目前所使用给的是3.0.7版本,本文仅针对Mongo ...
- mysql:explain分析sql
对于执行较慢的sql,可以使用explain命令查看这些sql的执行计划.查看该SQL语句有没有使用上了索引,有没有做全表扫描,这都可以通过explain命令来查看 mysql> explain ...
- mysql优化:explain 和 profile
此文转自:https://blog.csdn.net/hanjungua8144/article/details/84317829 一.SQL查询语句优化基本思路和原则 优化更需要优化的Query.定 ...
- Python练习题 016:猴子吃桃
[Python练习题 016] 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个.第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到 ...
- No.016:3Sum Closest
问题: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
随机推荐
- ES查询index对应的mapping信息
private void getMappingByIndex(String indices) throws IOException { GetMappingsRequest getMappingsRe ...
- No style sheet with given id found错误
在chrome中打开html页面,报错No style sheet with given id found,解决方如下
- Linux:centOS LAMP搭建之软件包下载地址
MySQL5.1 wget mysql-5.1.73-linux-i686-glibc23.tar.gz #二进制包 MySQL5.6 wget http://mirrors.sohu.com/mys ...
- PCB设计黄金法则永不改变
尽管目前半导体集成度越来越高,许多应用也都有随时可用的片上系统,同时许多功能强大且开箱即用的开发板也越来越可轻松获取,但许多使用案例中电子产品的应用仍然需要使用定制PCB.在一次性开发当中,即使一个普 ...
- [Scala]Scala学习笔记六 文件
1. 读取行 读取文件,可以使用scala.io.Source对象的fromFile方法.如果读取所有行可以使用getLines方法: val source = Source.fromFile(&qu ...
- caffe 细节
batch :http://www.zhihu.com/question/32673260 caffe blog: http://blog.csdn.net/abcjennifer/article/d ...
- Git详解之十 Git常用命令
下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一. ...
- Android:LinearLayout布局中Layout_weight的深刻理解
首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码 ...
- Javascrpt 速成篇】 二:js面向对象
现实世界的对象由形态和行为组成,js中对应的是属性和函数. <!DOCTYPE html> <html> <head> <meta charset=" ...
- silverlight——多次异步调用的顺序执行
遇到这样一个功能需求,对于后台的同一个服务调用多次,但要求传入的参数能够再一个执行完之后再进行另一个参数的执行. 由于silverlight支持的是异步调用机制,故无法控制服务调用何时返回.那么如果使 ...