Detailed explanation of MYSQL EXPLAIN results

EXPLAIN不会告诉你关于触发器、存储过程的信息或用户自定义函数对查询的影响情况。
EXPLAIN不考虑各种Cache(缓存)。
EXPLAIN不能显示MySQL在执行查询时所作的优化工作。
部分统计信息是估算的,并非精确值。	
EXPALIN只能解释SELECT操作,其他操作要重写为SELECT后查看执行计划。

1 id

select的识别符,这是select的查询序列号。
如果有两列数据id相同,则为同一组查询,由上到下执行。
如果id值不同,id值越大,优先级越高。

2 select_type

select的类型

SIMPLE (simple): Simple SELECT (does not use UNION or subqueries).
PRIMARY (primary): The outermost query in the subquery. If the query contains any complex subparts, the outermost select is marked as PRIMARY.
UNION (union): The second or subsequent SELECT statement in UNION.
DEPENDENT UNION (dependent union): The second or subsequent SELECT statement in UNION, depending on the query outside.
UNION RESULT (union result): The result of UNION, the second select in the union statement starts with all selects.
SUBQUERY (subquery): The first SELECT in the subquery, the result does not depend on the external query.
DEPENDENT SUBQUERY (dependent subquery): The first SELECT in the subquery depends on the external query.
DERIVED (derived): The SELECT (subquery of the FROM clause) of the derived table.
UNCACHEABLE SUBQUERY (uncacheable subquery): (The result of a subquery cannot be cached, the first line of the outer link must be re-evaluated)

3 table

输出结果集的表名称。

4 partitions

输出结果集的表所在的分区

5 TYPE

type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:
Null > system > const > eq_ref > ref > ref_or_null> index_merge > unique_subquery > index_subquery > range > index > ALL

Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref.
When we perform conditional queries, it is recommended to use an index, otherwise it will cause a full table scan, and the IO overhead and program performance cannot be guaranteed!

NULL : MySQL decomposes the statement during the optimization process, and does not even need to access the table or index during execution. For example, selecting the minimum value from an index column can be done through a separate index search.
system : The table has only one row (= system table). This is a special case of the const join type. When MySQL optimizes a certain part of the query and converts it to a constant, use these types (system/const) to access. If the primary key is placed in the where list, MySQL can convert the query into a constant. When the query table has only one row, use system.
const : The table has at most one matching row, which will be read at the beginning of the query. Because there is only one row, the column values ​​in this row can be considered constant by the rest of the optimizer. const tables are fast because they are only read once!
eq_ref : Similar to ref, the difference is that the index used is a unique index. For each index key value, only one record in the table matches. In simple terms, the primary key or unique key is used as the association condition in the multi-table connection. This is probably the best type of connection, except for the const type.
ref : indicates the connection matching condition of the above table, that is, which columns or constants are used to find the value on the index column.
ref_or_null : The connection type is like ref, but MySQL can specifically search for rows containing NULL values.
index_merge : This connection type indicates that the index merge optimization method is used. [Not commonly used]
unique_subquery: This type replaces the ref of the IN subquery of the following form: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery is an index search function, which can completely replace the subquery, which is more efficient. [Not commonly used]
index_subquery : This connection type is similar to unique_subquery. You can replace IN subqueries, but only suitable for non-unique indexes in subqueries of the following form: value IN (SELECT key_column FROM single_table WHERE some_expr). [Not commonly used]
range : Only retrieve rows in a given range, and use an index to select rows.
index : The connection type is the same as ALL, Full Index Scan, the difference between index and ALL is that the index type only traverses the index tree. This is usually faster than ALL, because index files are usually smaller than data files.
ALL : Full Table Scan, MySQL will traverse the entire table to find matching rows.

6 possible_keys

表示查询时,可能使用的索引。( MySQL能使用哪个索引在该表中找到行)

7 key

实际使用的索引(键),必然包含在possible_keys中。如果没有选择索引,索引是NULL。
要想强制MySQL使用或忽视possible_keys列中的索引,
在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

8 key_len

索引的长度 ( 使用的字节数 )。如果索引是NULL,则长度为NULL。
不损失精确性的情况下,长度越短越好 。key_len显示的值为索引字段的最大可能长度,
并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的。

9 ref

Which column or constant is used, together with the index, is used to find the value on the indexed column from the table. (The comparison between the column and the index indicates the connection matching condition of the above table.)

10 rows

MySQL认为它执行查询时必须检查的行数既预估扫描的行数。

11 filtered

通过表条件过滤出的行数的百分比估计值。

12 Extra

Mysql执行情况的描述和详细说明。

Distinct : After MySQL finds the first matching row, it stops searching for more rows for the current row combination.
Not exists : MySQL can optimize the query for LEFT JOIN. After finding a row that matches the LEFT JOIN standard, it will no longer check more rows in the table for the previous row combination.
range checked for each record (index map: #): MySQL did not find a good index that can be used, but found that if the column value from the previous table is known, some indexes may be available.
Using filesort : When the Query contains an order by operation, and the sorting operation that cannot be completed using the index is called "file sorting".
Using index : Only use the information in the index tree without further searching to read the actual row to retrieve the column information in the table.
Using temporary : In order to solve the query, MySQL needs to create a temporary table to hold the result set, which is common in sorting and grouping queries, and common group by and order by.
Using where : Without reading all the information in the table, the required data can be obtained only through the index. This happens when all the requested columns of the table are part of the same index, which means that the mysql server will retrieve rows in the storage engine Filter afterwards.
Using sort_union(...), Using union(...), Using intersect(...) : These functions explain how to merge index scans for index_merge join types.
Using index for group-by: Similar to the Using index method of accessing the table, Using index for group-by means that MySQL has found an index that can be used to query all the columns in the GROUP BY or DISTINCT query, without having to search the hard disk to access the actual table.
Using join buffer : The changed value emphasizes that the index is not used when obtaining the connection conditions, and the connection buffer is needed to store intermediate results. If this value appears, it should be noted that depending on the specific situation of the query, an index may need to be added to improve performance.
Impossible where : This value emphasizes that the where statement will lead to no eligible rows (it is impossible to have results by collecting statistics).
Select tables optimized away : This value means that only by using the index, the optimizer may only return one row from the aggregate function result.
No tables used : Use from dual in the Query statement or do not contain any from clause.

Guess you like

Origin blog.csdn.net/qq_37823979/article/details/107461893