mysql 执行计划的理解

1、执行计划就是在sql语句之前加上explain,使用desc 也可以。
2、desc有两个选项extended和partitions,desc extended 将原sql语句进行优化,通过show warnings 可以看到优化后的sql语句。
desc partitions 可以查看使用分区表的信息。
3、比如:
mysql> desc select * from student;
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | student | ALL | NULL | NULL | NULL | NULL | 5 | |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+



id 是执行计划的编号,可以理解为方法的调用堆栈,调用堆栈先进后出,id越大,越先执行,如下:
mysql> desc select * from student where id>1 union select * from student where id<5;
+------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | student | ALL | ID | NULL | NULL | NULL | 5 | Using where |
| 2 | UNION | student | ALL | ID | NULL | NULL | NULL | 5 | Using where |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
+------+--------------+------------+------+---------------+------+---------+------+------+-------------+

mysql> desc select * from student where id in (select id from student where name=‘Andy‘);
+----+--------------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | student | ALL | NULL | NULL | NULL | NULL | 5 | Using where |
| 2 | DEPENDENT SUBQUERY | student | ALL | ID | NULL | NULL | NULL | 5 | Using where |
+----+--------------------+---------+------+---------------+------+---------+------+------+-------------+
2 rows in set



select_type 查询类型,主要的取值如下:
1、simple,只是简单的查询。如下:
mysql> desc select * from student;
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | student | ALL | NULL | NULL | NULL | NULL | 4 | |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+

2、primary/union/union result,组合查询
mysql> desc select * from student where id>1 union select * from student where id<4;
+------+--------------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+------------+-------+---------------+---------+---------+------+------+-------------+
| 1 | PRIMARY | student | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where |
| 2 | UNION | student | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
+------+--------------+------------+-------+---------------+---------+---------+------+------+-------------+

3、primary/dependent subquery,依赖子查询
mysql> desc select * from student where id in (select sid from sc where score>80);
+----+--------------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | student | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
| 2 | DEPENDENT SUBQUERY | sc | ALL | NULL | NULL | NULL | NULL | 6 | Using where |
+----+--------------------+---------+------+---------------+------+---------+------+------+-------------+

4、primary/derived, 查询的目标不是物理表,也就是使用了临时表
mysql> desc select min(id) from (select id from student where name=‘Andy‘) t1;
+----+-------------+------------+------+---------------+-----------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+-----------+---------+------+------+--------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | |
| 2 | DERIVED | student | ref | stu_index | stu_index | 51 | | 2 | Using where; Using index |
+----+-------------+------------+------+---------------+-----------+---------+------+------+--------------------------+



table 引用的表



type是指访问类型,主要的取值有 all,index,range,ref,eq_ref,const,system,null
1、all 遍历全表
mysql> desc select name from student;
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | student | ALL | NULL | NULL | NULL | NULL | 4 | |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+

2、index 使用索引,遍历索引树

mysql> create index index_name on student(name);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

mysql> show index from student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | ID | A | 3 | NULL | NULL | | BTREE | | |
| student | 1 | index_name | 1 | NAME | A | 3 | NULL | NULL | YES | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set

mysql> desc select name from student;
+----+-------------+---------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | student | index | NULL | index_name | 51 | NULL | 4 | Using index |
+----+-------------+---------+-------+---------------+------------+---------+------+------+-------------+

3、range 使用索引,扫描范围
mysql> desc select name from student where name in (‘Andy‘,‘Bill‘);
+----+-------------+---------+-------+---------------+------------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+------------+---------+------+------+--------------------------+
| 1 | SIMPLE | student | range | index_name | index_name | 51 | NULL | 3 | Using where; Using index |
+----+-------------+---------+-------+---------------+------------+---------+------+------+--------------------------+

4、ref 非唯一性索引扫描,找到所有匹配的索引值
会有多个name为Andy的记录
mysql> desc select name from student where name =‘Andy‘;
+----+-------------+---------+------+---------------+------------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------------+---------+-------+------+--------------------------+
| 1 | SIMPLE | student | ref | index_name | index_name | 51 | const | 2 | Using where; Using index |
+----+-------------+---------+------+---------------+------------+---------+-------+------+--------------------------+
id为2的查询,对于teacher的一个name,从student找到找到所有name相同的记录
mysql> desc select * from student,teacher where student.name=teacher.name;
+----+-------------+---------+-------+---------------+------------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+------------+---------+---------------------+------+-------------+
| 1 | SIMPLE | teacher | index | index_name | index_name | 195 | NULL | 1 | Using index |
| 1 | SIMPLE | student | ref | index_name | index_name | 51 | testsc.teacher.NAME | 1 | Using where |
+----+-------------+---------+-------+---------------+------------+---------+---------------------+------+-------------+
5、eq_ref,对于前一个表的字段,只有一个记录会匹配,如下:
因为id是student主键,student只有一条记录匹配teacher的id。
mysql> desc select * from student,teacher where student.id=teacher.id;
+----+-------------+---------+--------+---------------+------------+---------+-------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+--------+---------------+------------+---------+-------------------+------+-------------+
| 1 | SIMPLE | teacher | index | PRIMARY | index_name | 195 | NULL | 1 | Using index |
| 1 | SIMPLE | student | eq_ref | PRIMARY | PRIMARY | 4 | testsc.teacher.ID | 1 | |
+----+-------------+---------+--------+---------------+------------+---------+-------------------+------+-------------+
6、const mysql对查询优化,转化为常量
mysql> desc select * from student where student.id=10001;
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | student | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
1 row in set
7、system 是const的一个特例,查询的记录只有一条
mysql> desc select id from (select * from student where student.id=10001) t1;
+----+-------------+------------+--------+---------------+---------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
| 2 | DERIVED | student | const | PRIMARY | PRIMARY | 4 | | 1 | |
+----+-------------+------------+--------+---------------+---------+---------+------+------+-------+
2 rows in set
8、null 没有记录,都不需要执行查询
mysql> desc select 1;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set



possible_keys 可能用到的索引



key 用到的索引



key_len 用到的索引长度,和三个因素有关:
是否允许为null,允许为null 多出一个字节标识是不是null
是否变长,变长多出两个字节表示长度
字符编码,不同字符编码,同一个字符占用的内存不一样latin1[1],gb2312[2],gbk[2],utf8[3]



ref 匹配的条件
mysql> desc select * from student,teacher where student.name=teacher.name;
+----+-------------+---------+-------+---------------+------------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+------------+---------+---------------------+------+-------------+
| 1 | SIMPLE | teacher | index | index_name | index_name | 195 | NULL | 1 | Using index |
| 1 | SIMPLE | student | ref | index_name | index_name | 51 | testsc.teacher.NAME | 1 | Using where |
+----+-------------+---------+-------+---------------+------------+---------+---------------------+------+-------------+



rows 估算扫描的行数



Extra 额外信息,主要有
Using index
Using where
Using temporary
Using filesort 无法利用索引完成的排序



MySQL执行计划的局限性:
不考虑触发器、存储过程的信息或用户自定义函数对查询的影响情况
不考虑各种Cache
不能显示MySQL在执行查询时所作的优化工作
部分统计信息是估算的,并非精确值
只能解释SELECT操作,其他操作要重写为SELECT后查看。

时间: 2024-11-10 08:14:44

mysql 执行计划的理解的相关文章

MySQL 执行计划explain详解

MySQL 执行计划explain详解 2015-08-10 13:56:27 分类: MySQL explain命令是查看查询优化器如何决定执行查询的主要方法.这个功能有局限性,并不总会说出真相,但它的输出是可以获取的最好信息,值得花时间去了解,因为可以学习到查询是如何执行的. 调用EXPLAIN 在select之前添加explain,mysql会在查询上设置一个标记,当执行查询计划时,这个标记会使其返回关于执行计划中每一步的信息,而不是执行它.它会返回一行或多行信息,显示出执行计划中的每一部

MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析

  关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别.首先,我们来看看官方文档关于三者的简单介绍(官方文档并没有介绍Using index,Using where这种情况): Using index (JSON property: using_index) The column information is retrieved from the ta

MySQL执行计划解读

MySQL执行计划解读 http://www.cnblogs.com/ggjucheng/archive/2012/11/11/2765237.html MySQL执行计划解读 Explain语法 EXPLAIN SELECT -- 变体: 1. EXPLAIN EXTENDED SELECT -- 将执行计划"反编译"成SELECT语句,运行SHOW WARNINGS 可得到被MySQL优化器优化后的查询语句 2. EXPLAIN PARTITIONS SELECT -- 用于分区表

MySQL执行计划解读 转他人文章

Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得到被MySQL优化器优化后的查询语句 2. EXPLAIN PARTITIONS SELECT …… 用于分区表的EXPLAIN 执行计划包含的信息 id 包含一组数字,表示查询中执行select子句或操作表的顺序 id相同,执行顺序由上至下 如果是子查询,id的序号会递增,id值越大优先级越高,越

MySQL执行计划不准确 -概述

为毛 MySQL优化器的执行计划 好多时候都不准确,不是最优的呢(cpu+io)??? 因素太多了:: 存在information_schema的信息是定期刷新上去的,好多时候不是最真的,甚至相差好大(非高山峰时好好利用一下analyze table等): 现在一个企业有钱没地方花,买一大堆固态磁盘,碰巧非智能的MySQL不能很好滴跟上硬件优化的节奏,可能超过一部分的选择原理就不怎么准确了(这点相信其他数据库也是痛点吧); 环境因素/ 配置因素 等等 ... MySQL执行计划不准确 -概述,布

【转】mysql执行计划介绍

原文地址:http://www.jb51.net/article/43306.htm 1.查看mysql执行计划 explain SELECT * from shippingorder where STATUS<>4; 2.执行计划包含的信息 (1).id 含义,指示select字句或操作表的顺序. eg1:id相同,执行顺序从上到下,下面的执行计划表示,先操作t1表,然后操作t2表,最后操作t3表. eg2:若存在子查询,则子查询(内层查询)id大于父查询(外层查询),先执行子查询.id越大

MySQL执行计划 EXPLAIN参数

MySQL执行计划参数详解 转http://www.jianshu.com/p/7134286b3a09 MySQL数据库中,在SELECT查询语句前边加上“EXPLAIN”或者“DESC”关键字,即可查看该查询语句的执行计划,分析执行计划是优化慢查询的重要手段.如: EXPLAIN SELECT * FROM school; DESC SELECT * FROM school; 执行结果: 执行计划参数.png 接下来对这10个参数进行简单解释: 1.id:在整个查询中SELECT的位置: 2

如何查看MySQL执行计划

在介绍怎么查看MySQL执行计划前,我们先来看个后面会提到的名词解释: 覆盖索引: MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件 包含所有满足查询需要的数据的索引称为 覆盖索引(Covering Index) 如果要使用覆盖索引,一定要注意select列表中只取出需要的列,不可select *,因为如果将所有字段一起做索引会导致索引文件过大,查询性能下降 EXPLAIN查看执行计划的一些局限: EXPLAIN不会告诉你关于触发器.存储过程的信息或用户自定义函

MySQL执行计划的讲解

最近同事在执行线上执行一条MySQL的查询语句,数据的话在9000条左右,但使用左连接的时候查询速度大概在15秒左右~这速度确实是无法接受的~ 经过简单的修改,变为内连接的话,执行速度不到1秒. 下面是两条具体的sql: 左连接的sql如下: SELECT count(*) FROM investment i LEFT JOIN payment m ON m.bill_id = i.id; 执行结果如下: 使用内连接的sql如下: SELECT count(*) FROM investment