概念:
- explain显示了MySQL如何使用索引来处理select语句以及连接表,我们可以根据explain的结果来优化我们的sql。
结果:
-
官方文档:
-
id
- SELECT操作的序号(select_id),用于标识查询中执行SELECT子句或操作表的顺序,id值越大,优先级越高,越先被执行。
-
select_type 查询类型
- SIMPLE 普通查询 Simple SELECT (not using UNION or subqueries)
- PRIMARY Outermost SELECT
- UNION Second or later SELECT statement in a UNION
- UNION RESULT Result of a UNION.
-
table
- 说明:扫描的表。
-
type
- 说明:type是一个很重要的指标,type从好到差依次为:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
- const 使用唯一索引,扫描一次就找到了对应的记录。 eg:根据主键查询数据
- ref 使用非唯一索引进行扫描。eg:select * from t_message t where t.owner ='jack@xxx.com' (注:在owner列上建立了非唯一索引)
- index_merge 使用了使用了索引合并。
- 索引合并:对多个索引分别进行条件扫描,然后将各自的结果进行合并(取交集、并集等操作)。
- MySql 中文文档 - 8.2.1.3 索引合并优化 | Docs4dev
- range 使用索引进行范围查询,即:在索引列上进行 LIKE、BETWEEN、IN、>= 等操作 eg:select * from t_message t where t.owner like 'jack%' (注:在owner列上建立了非唯一索引)
- index 按照索引的顺序进行全表扫描。注:index与ALL相比唯一的优势就是:查询出来的数据是按照一定顺序(即:索引的顺序)排列的。eg:select * from t_message order by id (注:id为主键)
- ALL 全表(顺序)扫描,即:不使用索引,直接读取表上的数据。eg:select * from t_message order by title eg:select * from t_message
-
possible_keys
- 说明:可以使用的索引。
-
key
- 说明:实际使用的索引。
- null 表示没有使用索引。
- PRIMARY 表示使用了主键。
-
key_len
- 说明:实际使用的索引的长度,从这个指标可以判断出复合索引中的哪些列被使用了。
-
ref
- const 使用索引进行等值查询。 eg:select * from t_message where owner='jxn' (注:在owner列上建立了索引)
-
rows
- 说明:执行该sql时,需要扫描的行数。
- 该值越小越好,若查询条件未命中索引,则全表查询,此时rows为该表的总行数。
-
filtered
-
Percentage of rows filtered by table condition
-
The
filtered
column indicates an estimated percentage of table rows that are filtered by the table condition. The maximum value is 100, which means no filtering of rows occurred. Values decreasing from 100 indicate increasing amounts of filtering.rows
shows the estimated number of rows examined androws
×filtered
shows the number of rows that are joined with the following table. For example, ifrows
is 1000 andfiltered
is 50.00 (50%), the number of rows to be joined with the following table is 1000 × 50% = 500.
-
-
Extra
- 说明:表示其它的一些信息。
- Using index
- 只从索引树中查询信息,即该sql只查询索引列的值。 eg:select owner from t_message (注:在owner列上建立了索引)
- Using index condition
- 使用了索引条件下推(index condition pushdown)
- Using where
- 使用了where语句。
- Using filesort
- 排序时没有根据索引来排序,一般可以通过在排序字段上建立合适的索引来减少或者避免。
- 注意:filesort这个名字起的很糟糕,filesort并不意味着使用了外部排序(硬盘排序)!
- Using temporary 使用了临时表
- Using sort_union(idx_2,idx_2) 使用了索引合并