mysql explain的extra
导读
extra主要有是那种情况:Using index、Using filesort、Using temporary、Using where
Using where无需多说,就是使用了where筛选条件。
数据准备:
CREATE TABLE `t_blog` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(50) default NULL,
`typeId` int(11) default NULL,
`a` int(11) default '',
PRIMARY KEY (`id`),
KEY `index_1` (`title`,`typeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Using index
表示在查询中使用了覆盖索引,避免了扫描表的数据行。
mysql> EXPLAIN select title from t_blog;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | t_blog | index | NULL | index_1 | 158 | NULL | 7 | Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set
已知title字段是index_1索引的一部分,上条sql只查询title字段,只会扫描索引文件而不会扫描表的所有数据行,在extra列中,出现了Using index。
mysql> EXPLAIN select * from t_blog;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set
上条语句中,除了查询已经加了索引的字段,还查询了没有加索引的字段【a】,导致扫描了表的数据行,因此,extra列中没有出现Using index。
当只出现Using index,没出现Using where时,表示索引用于读取数据,以第一条sql为例。
当Using index 和 Using where同时出现时,表示索引用于查找动作,例如:
mysql> EXPLAIN select title from t_blog where title = 'java';
+----+-------------+--------+------+---------------+---------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+---------+---------+-------+------+--------------------------+
| 1 | SIMPLE | t_blog | ref | index_1 | index_1 | 153 | const | 1 | Using where; Using index |
+----+-------------+--------+------+---------------+---------+---------+-------+------+--------------------------+
1 row in set
Using filesort
Using filesort通常出现在order by,当试图对一个不是索引的字段进行排序时,mysql就会自动对该字段进行排序,这个过程就称为“文件排序”
mysql> EXPLAIN select * from t_blog order by title;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------+
| 1 | SIMPLE | t_blog | index | NULL | index_1 | 158 | NULL | 7 | |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------+
1 row in set
已知title是index_1索引中的第一列索引,所以单独使用时索引生效,在排序时根据索引排序,不会产生文件排序。
mysql> EXPLAIN select * from t_blog order by typeId;
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
1 row in set
虽然typeId是index_1索引的第二列,但由于缺失第一列,所以索引失效。在排序时无法根据索引排序,故mysql会自动进行排序,产生文件排序。
mysql> EXPLAIN select * from t_blog order by a;
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
1 row in set
字段a上没有任何索引,所以在排序时无法根据索引排序,因此产生文件排序。
Using filesort出现的情况:排序时无法根据索引进行排序,mysql优化器只能自己进行排序,这种情况会大大降低性能,不可取。
Using temporary
表示在查询过程中产生了临时表用于保存中间结果。mysql在对查询结果进行排序时会使用临时表,常见于group by。
group by的实质是先排序后分组,同order by一样,group by和索引息息相关。
试图对一个没有索引的字段进行分组,会产生临时表:
mysql> EXPLAIN select title from t_blog group by typeId;
+----+-------------+--------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | t_blog | index | NULL | index_1 | 158 | NULL | 7 | Using index; Using temporary; Using filesort |
+----+-------------+--------+-------+---------------+---------+---------+------+------+----------------------------------------------+
1 row in set
对一个有索引的字段进行分组就不会产生临时表:
mysql> EXPLAIN select title from t_blog group by title,typeId;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | t_blog | index | NULL | index_1 | 158 | NULL | 7 | Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set
当order by子句和group by子句的字段相同时不会产生临时表:
mysql> explain select * from t_blog b left join t_type t on b.typeId = t.id group by b.id order by b.id;
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------+
| 1 | SIMPLE | b | index | NULL | PRIMARY | 4 | NULL | 7 | |
| 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------+
2 rows in set
当order by子句和group by子句的字段不同时就会产生临时表:
mysql> explain select * from t_blog b left join t_type t on b.typeId = t.id group by b.id order by b.title;
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-----------------+
| 1 | SIMPLE | b | index | NULL | index_1 | 158 | NULL | 7 | Using temporary |
| 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-----------------+
2 rows in set
当时用left join时,若order by子句和group by子句都来自于从表时会产生临时表:
mysql> explain select * from t_blog b left join t_type t on b.typeId = t.id group by t.id order by t.id;
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+---------------------------------+
| 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 7 | Using temporary; Using filesort |
| 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+---------------------------------+
2 rows in set
mysql> explain select * from t_blog b left join t_type t on b.typeId = t.id group by t.id order by t.name;
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+---------------------------------+
| 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 7 | Using temporary; Using filesort |
| 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+---------------------------------+
2 rows in set
出现Using temporary意味着产生了临时表存储中间结果并且最后删掉了该临时表,这个过程很消耗性能。
结尾
转载自:https://blog.51cto.com/13593129/2378340
mysql explain的extra的更多相关文章
- MySQL explain结果Extra中"Using Index"与"Using where; Using index"区别探究
问题背景 最近用explain命令分析查询sql执行计划,时而能看到Extra中显示为"Using index"或者"Using where; Using Index&q ...
- MySQL explain,Extra分析(转)
explain结果中有一个Extra字段,对分析与优化SQL有很大的帮助 数据准备: create table user ( id int primary key, name varchar(20), ...
- mysql explain 的extra中using index ,using where,using index condition,using index & using where理解
using index :查找使用了索引,查询结果覆盖了索引 using where:查找使用了索引,不需要回表去查询所需的数据,查询结果是索引的一部分 using index condition:查 ...
- 【转载】 mysql explain用法
转载链接: mysql explain用法 官网说明: http://dev.mysql.com/doc/refman/5.7/en/explain-output.html 参数: htt ...
- Mysql Explain 详解(转)
原文:http://www.cnitblog.com/aliyiyi08/archive/2008/09/09/48878.html 一.语法 explain < table_name > ...
- mysql explain详解
对于经常使用mysql的兄弟们,对explain一定不会陌生.当你在一条SELECT语句前放上关键词EXPLAIN,MySQL解释它将如何处理SELECT,提供有关表如何联合和以什么次序的信息.借助于 ...
- mysql explain用法和结果的含义
重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...
- MYSQL EXPLAIN 很慢的原因
今天同事在查看一个SQL的执行计划的时候,EXPLAIN语句跑了2分钟.SQL命令类似: SELECT * FROM (SELECT USERID,COUNT(*) FROM TBNAME GROUP ...
- [转]MySQL Explain详解
在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句有 ...
随机推荐
- 四、$jQuery
1.你觉得jQuery或zepto源码有哪些写的好的地方 jquery源码封装在一个匿名函数的自执行环境中,有助于防止变量的全局污染,然后通过传入window对象参数,可以使window对象作为局部变 ...
- Linux中查看磁盘大小、文件大小、排序方法小结
一,查看磁盘空间大小的命令:dfdf命令用于查看磁盘分区上的磁盘空间,包括使用了多少,还剩多少,默认单位是KB 比如以下命令: df -hl执行结果如下: 执行的结果每列的含义: 第一列Filesys ...
- mysql中 Rank、DENSE_RANK()的区别
相同点:RANK()和DENSE_RANK()的是排名函数 不同点:RANK()是跳跃排序,即如果有两条记录重复,接下来是第三级别 如:1 2 2 4,会跳过3 DENSE_RANK()是连续排序,即 ...
- Linux部分场景非常有用的命令集1_chattr&ldd&xargs&screen&ssh&磁盘&du
这里不做详细说明或截图,仅作为记录和简单说明.注:可能只针对某一命令部分功能,不包含整个功能,若要查看全部请自行查阅文档或help 1.chattr 当某一文件或目录,不想被无意修改或删除(即使roo ...
- MySQL 5.7.30 的安装/升级(所有可能的坑都在这里)
楔子 由于之前电脑上安装的MySQL版本是比较老的了,大概是5.1的版本,不支持JSON字段功能.而最新开发部门开发的的编辑器产品,使用到了JSON字段的功能. 因此需要升级MySQL版本,升级的目标 ...
- [256个管理学理论]005.羊群效应(Herd Behavior)
羊群效应(Herd Behavior) 来自于大洋彼岸的让你看不懂的解释: “羊群效应”,也叫“从众效应”,是个人的观念或行为由于真实的或想像的群体的影响或压力,而向与多数人相一致的方向变化的现象.表 ...
- [SD喜爱语言PK大赛]001.PHP vs Node.js
引言:近日,两大编程飓风之战已经愈演愈烈.在程序员社区,一些争端因PHP与Node.js而起. 观点:其实就本人及团队而言,Language just a language!不存在高低之分,而侧重的原 ...
- [JavaWeb基础] 015.Struts2 表单验证框架
在web开发的过程中,我们经常要用到一些填写表单的操作,我们一般都要在提交表单信息的时候对表单的内容进行验证,struts2给我们提供了简单的实现接口,让我们可以很容易的对表单进行验证.下面讲解下最传 ...
- CVE-2019-7238 poc
from requests.packages.urllib3.exceptions import InsecureRequestWarning import urllib3 import reques ...
- Chisel3 - 基本数据类型
https://mp.weixin.qq.com/s/bSrM-wLRn7O_75xYKeoaEQ Chisel中的基本数据类型,不是Verilog中的Wire和Reg.Wire和Register ...