mysql explain type详解
本文转载自最官方的 mysql explain type 字段解读
读了很多别人的笔记都杂乱不堪,很少有实例,什么都不如原装的好,所以当你读到我的笔记的时候如果觉得说的不明白,最好参考官方的手册。
我把官方的手册简单翻译了下,好多地方也还是不懂,网友补充,配合了官方的实例代码
更多请更多的参考 https://dev.mysql.com/doc/refman/5.6/en/explain-output.html#explain-join-types
下面的笔记是根据我自己的 mysql 服务的版本号来的
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.16-log |
+------------+
1 row in set (0.00 sec)
随便放一个查询结果,我们要说的就是这里的type
的值。
mysql> explain SELECT id,title FROM seo_php_article where is_delete=0 order by id asc limit 66500,500;
+----+-------------+-----------------+------+---------------+-----------+---------+-------+-------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+---------------+-----------+---------+-------+-------+-----------------------------+
| 1 | SIMPLE | seo_php_article | ref | is_delete | is_delete | 1 | const | 67500 | Using where; Using filesort |
+----+-------------+-----------------+------+---------------+-----------+---------+-------+-------+-----------------------------+
1 row in set (0.00 sec)
The type column of EXPLAIN output describes how tables are joined. The following list describes the join types, ordered from the best type to the worst:
下面的从好到坏依次解释:
system
The table has only one row (= system table). This is a special case of the const join type.
触发条件:表只有一行,这是一个const
type 的特殊情况。
const
The table has at most one matching row, which is read at the start of the query. Because there is only one row, values from the column in this row can be regarded as constants by the rest of the optimizer. const tables are very fast because they are read only once.
触发条件:最多只有一行匹配。
const
is used when you compare all parts of aPRIMARY KEY
orUNIQUE
index to constant values. In the following queries,tbl_name
can be used as a const table:
当你使用主键或者唯一索引的时候,就是const
类型,比如下面这两种查询
# 单一主键
SELECT * FROM tbl_name WHERE primary_key=1;
# 联合主键
SELECT * FROM tbl_name WHERE primary_key_part1=1 AND primary_key_part2=2;
eq_ref
One row is read from this table for each combination of rows from the previous tables. Other than the system and const types, this is the best possible join type. It is used when all parts of an index are used by the join and the index is a PRIMARY KEY or UNIQUE NOT NULL index.
触发条件:只匹配到一行的时候。除了system
和const
之外,这是最好的连接类型了。当我们使用主键索引或者唯一索引的时候,且这个索引的所有组成部分都被用上,才能是该类型。
eq_ref can be used for indexed columns that are compared using the = operator. The comparison value can be a constant or an expression that uses columns from tables that are read before this table. In the following examples, MySQL can use an eq_ref join to process ref_table
在对已经建立索引列进行=
操作的时候,eq_ref
会被使用到。比较值可以使用一个常量也可以是一个表达式。这个表达示可以是其他的表的行。
# 多表关联查询,单行匹配
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
# 多表关联查询,联合索引,多行匹配
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
ref
All rows with matching index values are read from this table for each combination of rows from the previous tables. ref is used if the join uses only a leftmost prefix of the key or if the key is not a PRIMARY KEY or UNIQUE index (in other words, if the join cannot select a single row based on the key value). If the key that is used matches only a few rows, this is a good join type.
第一句没理解透,先理解到多行匹配吧。
触发条件:触发联合索引最左原则(不知道的搜下),或者这个索引不是主键,也不是唯一索引(换句话说,如果这个在这个索引基础之上查询的结果多于一行)。
如果使用那个索引只匹配到非常少的行,也是不错的。
ref can be used for indexed columns that are compared using the = or <=> operator. In the following examples, MySQL can use a ref join to process ref_table:
在对已经建立索引列进行=
或者<=>
操作的时候,ref
会被使用到。与eq_ref
不同的是匹配到了多行
# 根据索引(非主键,非唯一索引),匹配到多行
SELECT * FROM ref_table WHERE key_column=expr;
# 多表关联查询,单个索引,多行匹配
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
# 多表关联查询,联合索引,多行匹配
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
fulltext
The join is performed using a FULLTEXT index.
使用全文索引的时候才会出现
ref_or_null
This join type is like ref, but with the addition that MySQL does an extra search for rows that contain NULL values. This join type optimization is used most often in resolving subqueries. In the following examples, MySQL can use a ref_or_null join to process ref_table:
这个查询类型和ref
很像,但是 MySQL 会做一个额外的查询,来看哪些行包含了NULL
。这种类型常见于解析子查询的优化。(我理解为 mysql 自己做的优化)
SELECT * FROM ref_table
WHERE key_column=expr OR key_column IS NULL;
index_merge
This join type indicates that the Index Merge optimization is used. In this case, the key column in the output row contains a list of indexes used, and key_len contains a list of the longest key parts for the indexes used. For more information, see Section 8.2.1.3, “Index Merge Optimization”.
在一个查询里面很有多索引用被用到,可能会触发index_merge
的优化机制。
unique_subquery
This type replaces eq_ref for some IN subqueries of the following form:
比eq_ref
复杂的地方是使用了in
的子查询,而且是子查询是主键或者唯一索引
value IN (SELECT primary_key FROM single_table WHERE some_expr)
unique_subquery is just an index lookup function that replaces the subquery completely for better efficiency.
unique_subquery
只是一个索引查找函数,它可以完全替代子查询以提高效率。明白,现在不就是在做子查询吗?
index_subquery
This join type is similar to unique_subquery. It replaces IN subqueries, but it works for nonunique indexes in subqueries of the following form:
它和unique_subquery
,但是它在子查询里使用的是非唯一索引。
value IN (SELECT key_column FROM single_table WHERE some_expr)
range
Only rows that are in a given range are retrieved, using an index to select the rows. The key column in the output row indicates which index is used. The key_len contains the longest key part that was used. The ref column is NULL for this type.
只有给定范围内的行才能被检索,使用索引来查询出多行。 输出行中的类决定了会使用哪个索引。 key_len
列表示使用的最长的 key 部分。 这个类型的ref
列是NULL。
range can be used when a key column is compared to a constant using any of the =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, or IN() operators:
# 常量比较,可能多行(但是这里的例子和上面 ref 的第一个例子不一样吗?)
SELECT * FROM tbl_name
WHERE key_column = 10;
# 范围查找
SELECT * FROM tbl_name
WHERE key_column BETWEEN 10 and 20;
# 范围查找
SELECT * FROM tbl_name
WHERE key_column IN (10,20,30);
# 多条件加范围查找
SELECT * FROM tbl_name
WHERE key_part1 = 10 AND key_part2 IN (10,20,30);
index
The index join type is the same as ALL, except that the index tree is scanned. This occurs two ways:
- If the index is a covering index for the queries and can be used to satisfy all data required from the table, only the index tree is scanned. In this case, the Extra column says Using index. An index-only scan usually is faster than ALL because the size of the index usually is smaller than the table data.
- A full table scan is performed using reads from the index to look up data rows in index order. Uses index does not appear in the Extra column.
index
类型和ALL
类型一样,区别就是index
类型是扫描的索引树。以下两种情况会触发:
- 如果索引是查询的覆盖索引,就是说索引查询的数据可以满足查询中所需的所有数据,则只扫描索引树,不需要回表查询。 在这种情况下,explain 的
Extra
列的结果是Using index
。仅索引扫描通常比ALL快,因为索引的大小通常小于表数据。 - 全表扫描会按索引的顺序来查找数据行。使用索引不会出现在
Extra
列中。
ALL
A full table scan is done for each combination of rows from the previous tables. This is normally not good if the table is the first table not marked const, and usually very bad in all other cases. Normally, you can avoid ALL by adding indexes that enable row retrieval from the table based on constant values or column values from earlier tables.
全表扫描就不用看了,赶快优化吧。
mysql explain type详解的更多相关文章
- 转载:MySQL EXPLAIN 命令详解学习
转载自:https://blog.csdn.net/mchdba/article/details/9190771 MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查 ...
- MySQL EXPLAIN 命令详解
MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提 ...
- 2.mysql explain命令详解
EXPLAIN详解 SQL编写和解析 编写过程 select-distinct-from-join-on-where-group by-having-order by-limit- 解析过程 from ...
- mysql explain参数详解
主要对几个参数做一些记录 type:显示的是访问类型 从最好到最差的连接类型为:const.eq_reg.ref.range.index和ALL 至少要达到range,基本是ref 最好是const ...
- mysql explain执行详解
1).id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它来进行查询.2).select_type列常见的有:A:simple:表示不 ...
- mysql explain语法详解--优化你的查询
原文地址:http://blog.csdn.net/zhuxineli/article/details/14455029 explain显示了mysql如何使用索引来处理select语句以及连接表.可 ...
- MySQL Explain命令详解--表的读取顺序,数据读取操作的类型等
表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度(key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的) 不损失精确 ...
- MySQL EXPLAIN 命令详解学习
http://blog.csdn.net/mchdba/article/details/9190771
- Mysql加锁过程详解(8)-理解innodb的锁(record,gap,Next-Key lock)
Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...
随机推荐
- PHP-文件、目录相关操作
PHP-文件.目录相关操作 一 目录操作(Directory 函数允许获得关于目录及其内容的信息) 相关函数: 函数 描述 chdir() 改变当前的目录. chroot() 改变根目录. clos ...
- Python单元测试框架pytest常用测试报告类型
先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...
- 关于Java注解(annotation)的简单理解
一.什么是注解? 从 JDK5 开始,Java增加对元数据的支持,也就是注解.简单理解就是代码里的特殊标志,这些标志可以在编译,类加载,运行时被读取,并执行相应的处理,以便于其他工具补充信息或者进行部 ...
- .net core 和 WPF 开发升讯威在线客服与营销系统:实现对 IE8 的完全完美支持 【干货】
本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...
- switch表达式为字符串
package EXERCISE; import java.util.*; public class HashCode { //switch判断字符串.switch表达式byte,short,int, ...
- 阅读笔记:ImageNet Classification with Deep Convolutional Neural Networks
概要: 本文中的Alexnet神经网络在LSVRC-2010图像分类比赛中得到了第一名和第五名,将120万高分辨率的图像分到1000不同的类别中,分类结果比以往的神经网络的分类都要好.为了训练更快,使 ...
- fzu2198 快来快来数一数
Accept: 204 Submit: 627 Time Limit: 1000 mSec Memory Limit : 65536 KB Problem Description n个六 ...
- JavaScript——内置对象
- 如何在windows上升级Powershell到5.1版本?
前言 此篇我们说的是Powershell5.1低版本到5.1的升级,对于Powershell6(及以上版本)可以跨平台独立安装,在windows上可与之前的版本并存. 首先要整清楚Powershell ...
- Linux 网络协议栈开发基础篇—— 网桥br0
一.桥接的概念 简单来说,桥接就是把一台机器上的若干个网络接口"连接"起来.其结果是,其中一个网口收到的报文会被复制给其他网口并发送出去.以使得网口之间的报文能够互相转发. 交换机 ...