mysql explain用法和结果的含义
重点是第二种用法,需要深入的了解。
先看一个例子:
- mysql> explain select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- 1 row in set (0.03 sec)
加上extended后之后:
- mysql> explain extended select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | 100.00 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- 1 row in set, 1 warning (0.00 sec)
有必要解释一下这个长长的表格里每一列的含义:
id | SELECT识别符。这是SELECT的查询序列号 |
select_type |
SELECT类型,可以为以下任何一种:
|
table |
输出的行所引用的表 |
type |
联接类型。下面给出各种联接类型,按照从最佳类型到最坏类型进行排序:
|
possible_keys |
指出MySQL能使用哪个索引在该表中找到行 |
key | 显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。 |
key_len | 显示MySQL决定使用的键长度。如果键是NULL,则长度为NULL。 |
ref | 显示使用哪个列或常数与key一起从表中选择行。 |
rows | 显示MySQL认为它执行查询时必须检查的行数。多行之间的数据相乘可以估算要处理的行数。 |
filtered | 显示了通过条件过滤出的行数的百分比估计值。 |
Extra |
该列包含MySQL解决查询的详细信息
|
一.select_type的说明
1.UNION:
当通过union来连接多个查询结果时,第二个之后的select其select_type为UNION。
- mysql> explain select * from t_order where order_id=100 union select * from t_order where order_id=200;
- +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
- | 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | 2 | UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | NULL | UNION RESULT | <</span>union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
- +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
- 3 rows in set (0.34 sec)
2.DEPENDENT UNION与DEPENDENT SUBQUERY:
当union作为子查询时,其中第二个union的select_type就是DEPENDENT UNION。
第一个子查询的select_type则是DEPENDENT SUBQUERY。
- mysql> explain select * from t_order where order_id in (select order_id from t_order where order_id=100 union select order_id from t_order where order_id=200);
- +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
- | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | Using where |
- | 2 | DEPENDENT SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
- | 3 | DEPENDENT UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
- | NULL | UNION RESULT | <</span>union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
- +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
- 4 rows in set (0.03 sec)
3.SUBQUERY:
子查询中的第一个select其select_type为SUBQUERY。
- mysql> explain select * from t_order where order_id=(select order_id from t_order where order_id=100);
- +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
- | 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | 2 | SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |
- +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
- 2 rows in set (0.03 sec)
4.DERIVED:
当子查询是from子句时,其select_type为DERIVED。
- mysql> explain select * from (select order_id from t_order where order_id=100) a;
- +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
- | 1 | PRIMARY | <</span>derived2> | system | NULL | NULL | NULL | NULL | 1 | |
- | 2 | DERIVED | t_order | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |
- +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
- 2 rows in set (0.03 sec)
二.type的说明
1.system,const
见上面4.DERIVED的例子。其中第一行的type就是为system,第二行是const,这两种联接类型是最快的。
2.eq_ref
在t_order表中的order_id是主键,t_order_ext表中的order_id也是主键,该表可以认为是订单表的补充信息表,他们的关系是1对1,在下面的例子中可以看到b表的连接类型是eq_ref,这是极快的联接类型。
- mysql> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id;
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
- | 1 | SIMPLE | b | ALL | order_id | NULL | NULL | NULL | 1 | |
- | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | test.b.order_id | 1 | Using where |
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
- 2 rows in set (0.00 sec)
3.ref
下面的例子在上面的例子上略作了修改,加上了条件。此时b表的联接类型变成了ref。因为所有与a表中order_id=100的匹配记录都将会从b表获取。这是比较常见的联接类型。
- mysql> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id and a.order_id=100;
- +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
- | 1 | SIMPLE | a | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | 1 | SIMPLE | b | ref | order_id | order_id | 4 | const | 1 | |
- +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
- 2 rows in set (0.00 sec)
4.ref_or_null
user_id字段是一个可以为空的字段,并对该字段创建了一个索引。在下面的查询中可以看到联接类型为ref_or_null,这是mysql为含有null的字段专门做的处理。在我们的表设计中应当尽量避免索引字段为NULL,因为这会额外的耗费mysql的处理时间来做优化。
- mysql> explain select * from t_order where user_id=100 or user_id is null;
- +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
- | 1 | SIMPLE | t_order | ref_or_null | user_id | user_id | 5 | const | 50325 | Using where |
- +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
- 1 row in set (0.00 sec)
5.index_merge
经常出现在使用一张表中的多个索引时。mysql会将多个索引合并在一起,如下例:
- mysql> explain select * from t_order where order_id=100 or user_id=10;
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
- | 1 | SIMPLE | t_order | index_merge | PRIMARY,user_id | PRIMARY,user_id | 4,5 | NULL | 2 | Using union(PRIMARY,user_id); Using where |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
- 1 row in set (0.09 sec)
6.unique_subquery
该联接类型用于替换value IN (SELECT primary_key FROM single_table WHERE some_expr)这样的子查询的ref。注意ref列,其中第二行显示的是func,表明unique_subquery是一个函数,而不是一个普通的ref。
- mysql> explain select * from t_order where order_id in (select order_id from t_order where user_id=10);
- +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
- | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | Using where |
- | 2 | DEPENDENT SUBQUERY | t_order | unique_subquery | PRIMARY,user_id | PRIMARY | 4 | func | 1 | Using where |
- +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
- 2 rows in set (0.00 sec)
7.index_subquery
该联接类型与上面的太像了,唯一的差别就是子查询查的不是主键而是非唯一索引。
- mysql> explain select * from t_order where user_id in (select user_id from t_order where order_id>10);
- +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
- | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | Using where |
- | 2 | DEPENDENT SUBQUERY | t_order | index_subquery | PRIMARY,user_id | user_id | 5 | func | 50324 | Using index; Using where |
- +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
- 2 rows in set (0.00 sec)
8.range
按指定的范围进行检索,很常见。
- mysql> explain select * from t_order where user_id in (100,200,300);
- +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
- | 1 | SIMPLE | t_order | range | user_id | user_id | 5 | NULL | 3 | Using where |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
- 1 row in set (0.00 sec)
9.index
在进行统计时非常常见,此联接类型实际上会扫描索引树,仅比ALL快些。
- mysql> explain select count(*) from t_order;
- +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
- | 1 | SIMPLE | t_order | index | NULL | user_id | 5 | NULL | 100649 | Using index |
- +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
- 1 row in set (0.00 sec)
10.ALL
完整的扫描全表,最慢的联接类型,尽可能的避免。
- mysql> explain select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- 1 row in set (0.00 sec)
三.extra的说明
1.Distinct
MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。对于此项没有找到合适的例子,求指点。
2.Not exists
因为b表中的order_id是主键,不可能为NULL,所以mysql在用a表的order_id扫描t_order表,并查找b表的行时,如果在b表发现一个匹配的行就不再继续扫描b了,因为b表中的order_id字段不可能为NULL。这样避免了对b表的多次扫描。
- mysql> explain select count(1) from t_order a left join t_order_ext b on a.order_id=b.order_id where b.order_id is null;
- +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
- | 1 | SIMPLE | a | index | NULL | express_type | 1 | NULL | 100395 | Using index |
- | 1 | SIMPLE | b | ref | order_id | order_id | 4 | test.a.order_id | 1 | Using where; Using index; Not exists |
- +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
- 2 rows in set (0.01 sec)
3.Range checked for each record
这种情况是mysql没有发现好的索引可用,速度比没有索引要快得多。
- mysql> explain select * from t_order t, t_order_ext s where s.order_id>=t.order_id and s.order_id<</span>=t.order_id and t.express_type>5;
- +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
- | 1 | SIMPLE | t | range | PRIMARY,express_type | express_type | 1 | NULL | 1 | Using where |
- | 1 | SIMPLE | s | ALL | order_id | NULL | NULL | NULL | 1 | Range checked for each record (index map: 0x1) |
- +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
- 2 rows in set (0.00 sec)
4.Using filesort
在有排序子句的情况下很常见的一种情况。此时mysql会根据联接类型浏览所有符合条件的记录,并保存排序关键字和行指针,然后排序关键字并按顺序检索行。
- mysql> explain select * from t_order order by express_type;
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100395 | Using filesort |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
- 1 row in set (0.00 sec)
5.Using index
这是性能很高的一种情况。当查询所需的数据可以直接从索引树中检索到时,就会出现。上面的例子中有很多这样的例子,不再多举例了。
6.Using temporary
发生这种情况一般都是需要进行优化的。mysql需要创建一张临时表用来处理此类查询。
- mysql> explain select * from t_order a left join t_order_ext b on a.order_id=b.order_id group by b.order_id;
- +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
- | 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 100395 | Using temporary; Using filesort |
- | 1 | SIMPLE | b | ref | order_id | order_id | 4 | test.a.order_id | 1 | |
- +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
- 2 rows in set (0.00 sec)
7.Using where
当有where子句时,extra都会有说明。
8.Using sort_union(...)/Using union(...)/Using intersect(...)
下面的例子中user_id是一个检索范围,此时mysql会使用sort_union函数来进行索引的合并。而当user_id是一个固定值时,请参看上面type说明5.index_merge的例子,此时会使用union函数进行索引合并。
- mysql> explain select * from t_order where order_id=100 or user_id>10;
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
- | 1 | SIMPLE | t_order | index_merge | PRIMARY,user_id | user_id,PRIMARY | 5,4 | NULL | 2 | Using sort_union(user_id,PRIMARY); Using where |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
- 1 row in set (0.00 sec)
对于Using intersect的例子可以参看下例,user_id与express_type发生了索引交叉合并。
- mysql> explain select * from t_order where express_type=1 and user_id=100;
- +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
- | 1 | SIMPLE | t_order | index_merge | user_id,express_type | user_id,express_type | 5,1 | NULL | 1 | Using intersect(user_id,express_type); Using where |
- +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
- 1 row in set (0.00 sec)
9.Using index for group-by
表明可以在索引中找到分组所需的所有数据,不需要查询实际的表。
- mysql> explain select user_id from t_order group by user_id;
- +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
- | 1 | SIMPLE | t_order | range | NULL | user_id | 5 | NULL | 3 | Using index for group-by |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
- 1 row in set (0.00 sec)
除了上面的三个说明,还需要注意rows的数值,多行之间的数值是乘积的关系,可以估算大概要处理的行数,如果乘积很大,那就很有优化的必要了
mysql explain用法和结果的含义的更多相关文章
- mysql explain用法和结果的含义(转)
重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...
- mysql explain用法和结果的含义
转自:http://blog.chinaunix.net/uid-540802-id-3419311.html explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择 ...
- 【开发笔记】- MySQL EXPLAIN用法和结果的含义
转自:http://blog.chinaunix.net/uid-540802-id-3419311.html explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择 ...
- [转]Mysql explain用法和性能分析
本文转自:http://blog.csdn.net/haifu_xu/article/details/16864933 from @幸福男孩 MySQL中EXPLAIN解释命令是显示mysql如何 ...
- mysql中explain用法和结果的含义
explain select * from user explain select * from user explain extended select * from user explain e ...
- mysql explain中的type列含义和extra列的含义
很多朋友在用mysql进行调优的时候都肯定会用到explain来看select语句的执行情况,这里简单介绍结果中两个列的含义. 1 type列 官方的说法,说这列表示的是“访问类型”,更通俗一点就是: ...
- mysql explain详解
对于经常使用mysql的兄弟们,对explain一定不会陌生.当你在一条SELECT语句前放上关键词EXPLAIN,MySQL解释它将如何处理SELECT,提供有关表如何联合和以什么次序的信息.借助于 ...
- mysql explain和profiling
语法:explain select .... 变体: 1.explain extended select .... 将执行计划“反编译”成select语句: 运行show warnings 可以得到 ...
- mysql explain 分析sql语句
鉴于最近做的事情,需要解决慢sql的问题,现补充一点sql语句性能分析之explain的使用方式! 综合返回数据情况,分析各个参数,可以了解sql 使用方法:explain + sql语句 如 :e ...
随机推荐
- ( 译、持续更新 ) JavaScript 上分小技巧(四)
后续如有内容,本篇将会照常更新并排满15个知识点,以下是其他几篇译文的地址: 第一篇地址:( 译.持续更新 ) JavaScript 上分小技巧(一) 第二篇地址:( 译.持续更新 ) JavaScr ...
- [Android]GOF23种设计模式 & Android中的设计模式
GOF23种设计模式 设计原则: 1. 单一职责原则(SRP):就一个类而言,应该仅有一个引起它变化的原因 2. 开放-封闭原则(OCP):软件实体(类.模块.函数等)应该可以扩展,但是不可修改.即对 ...
- UVA1395 Slim Span(枚举最小生成树)
题意: 求最小生成树中,最大的边减去最小的边 最小值. 看了题解发现真简单=_= 将每条边进行从小到大排序,然后从最小到大一次枚举最小生成树,当构成生成树的时候,更新最小值 #include < ...
- HTTPS背后的加密算法
当你在浏览器的地址栏上输入https开头的网址后,浏览器和服务器之间会在接下来的几百毫秒内进行大量的通信.InfoQ的这篇文章对此有非常详细的描述.这些复杂的步骤的第一步,就是浏览器与服务器之间协商一 ...
- 转:Python K-means代码
#coding: UTF-8 import pearson_distance from pearson_distance import pearson_distance from math impor ...
- 如何写好一篇高质量的paper
http://blog.csdn.net/tiandijun/article/details/41775223 这篇文章来源于中科院Zhouchen Lin 教授的report,有幸读到,和大家分享一 ...
- ecshop目录结构
ECShop 最新程序 的结构图及各文件相应功能介绍ECShop文件结构目录┣ activity.php 活动列表┣ affiche.php 广告处理文件┣ affiliate.php 生成商品列表┣ ...
- setTimeout不断重复执行
setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. setTimeout(code,millisec) code 必需.要调用的函数后要执行的 JavaScript 代码串. ...
- Mini ORM——PetaPoco笔记(转)
记录一下petapoco官网博客的一些要点.这些博客记录了PetaPoco是如何一步步改进的. 目录: Announcing PetaPoco PetaPoco-Improvements PetaPo ...
- C#中事件的使用
C#中事件的使用 http://www.cnblogs.com/wayfarer/archive/2004/04/20/6712.html 用一个例子来说明事件的使用. 创建一个简单的类,名为Fil ...