加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

MySQL EXPLAIN结果集分析 - 附带大量案例

发布时间:2019-09-18 00:07:43 所属栏目:编程 来源:佚名
导读:EXPLAIN:查看SQL语句的执行计划 EXPLAIN命令可以帮助我们深入了解MySQL基于开销的优化器,还可以获得很多可能被优化器考虑到的访问策略的细节,以及当运行SQL语句时哪种策略预计会被优化器采用,在优化慢查询时非常有用。 执行explain之后结果集包含如下

dependent subquery: 子查询中的第一个select查询,依赖于外部查询的结果集

  1. mysql> explain select * from test where id in (select id  from test where id = 1000 union all select id from test2) ; 
  2. +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ 
  3. | id | select_type        | table      | type   | possible_keys | key     | key_len | ref   | rows  | Extra           | 
  4. +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ 
  5. |  1 | PRIMARY            | test       | ALL    | NULL          | NULL    | NULL    | NULL  | 68505 | Using where     | 
  6. |  2 | DEPENDENT SUBQUERY | test       | const  | PRIMARY       | PRIMARY | 8       | const |     1 | Using index     | 
  7. |  3 | DEPENDENT UNION    | test2      | eq_ref | PRIMARY       | PRIMARY | 8       | func  |     1 | Using index     | 
  8. | NULL | UNION RESULT       | <union2,3> | ALL    | NULL          | NULL    | NULL    | NULL  |  NULL | Using temporary | 
  9. +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ 

derived: 用于from子句中有子查询的情况,mysql会递归执行这些子查询,此结果集放在临时表中

  1. mysql> explain select * from (select * from test2 where id = 1000)a; 
  2. +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ 
  3. | id | select_type | table      | type   | possible_keys | key     | key_len | ref   | rows | Extra | 
  4. +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ 
  5. |  1 | PRIMARY     | <derived2> | system | NULL          | NULL    | NULL    | NULL  |    1 | NULL  | 
  6. |  2 | DERIVED     | test2      | const  | PRIMARY       | PRIMARY | 8       | const |    1 | NULL  | 
  7. +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ 
  8. 3、table 

3、table

table用来表示输出行所引用的表名

4、type(重要)

type表示访问类型,下面依次解释各种类型,类型顺序从最好到最差排列

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读