一. 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的信息是可以作为优化的提示,但是更多的是优化器优化的一种说明

最新文章

  1. PL/SQL数据库,Oracle登录
  2. 4Web镇之旅:开始链接
  3. Linux中__init、__devinit等内核优化宏【转】
  4. SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件
  5. SQL Server 创建表分区
  6. 开发中常用的 $.extend 总结
  7. css 弹性盒兼容性写法,直接复制粘贴
  8. Java设计模式之职责链设计模式
  9. notepad++中双击选中字符串高亮颜色设置
  10. hibernate框架学习之数据抓取(加载)策略helloworld
  11. python全局解释器锁(GIL)
  12. Jquery的Ajax中contentType和dataType的区别
  13. Matlab中使用LaTeX
  14. 20165310_Exp2实验三《敏捷开发与XP实践》
  15. python爬取当当网的书籍信息并保存到csv文件
  16. java的MAP接口方法
  17. CentOS-6.5安装配置JDK-7
  18. mysql 数据库设计规范
  19. php 快速上手
  20. [BZOJ 1412] 狼与羊的故事

热门文章

  1. TensorFlow 的使用步骤
  2. MoreEffectiveC++Item35(异常)(条款9-15)
  3. Python中函数练习
  4. LINUX系统下的shell命令---diff、cut、sort、uniq等
  5. ZK单机最简配置
  6. 十图详解TensorFlow数据读取机制(附代码)
  7. mongodb 使用
  8. &quot;PEP:8 expected 2 blank lines ,found 1&quot;
  9. BZOJ:5457: 城市(线段树合并)(尚待优化)
  10. Typescript : 遍历Array的方法:for, forEach, every等