本文转载自最官方的 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.
全表扫描就不用看了,赶快优化吧。