table of Contents
Mysql Optimization (from official documents) - Chapter 11. (query plan papers)
1 EXPLAIN Output Format
As more EXPLAIN content, so there will only extract some important places, the details can refer to the original .
EXPLAIN interpretation of the results:
Column | JSON Name | Meaning |
---|---|---|
id |
select_id |
The SELECT identifier |
select_type |
None | The SELECT type |
table |
table_name |
The table for the output row |
partitions |
partitions |
The matching partitions |
type |
access_type |
The join type |
possible_keys |
possible_keys |
The possible indexes to choose |
key |
key |
The index actually chosen |
key_len |
key_length |
The length of the chosen key |
ref |
ref |
The columns compared to the index |
rows |
rows |
Estimate of rows to be examined |
filtered |
filtered |
Percentage of rows filtered by table condition |
Extra |
None | Additional information |
Pick some important columns:
table
That is the table name, but it is also possible that the following three cases.
<union*M*,*N*>
: Representing a plurality of tablesunion
, where M and N are the first columnid
<derived*N*>
: Indicates derived from a table in the other tables, supra, M and N represents the first columnid
, it is usually derived from the table in thefrom
statement after theselect
<subquery*N*>
: Indicates the subquery materialized, N of the first rowid
.
partitions
For tables with the list of valid partition
type
That is
join type
, there are commonsystem``, const, eq_ref, ALL
and so on, the next section will detail.possible_keys
It represents the index to be selected
key
The index represents the actually used, comes from
possible_keys
, whenpossible_keys
is notNULL
the time, this column also has the potential tonone
, because sometimes with the index does not define efficiency will be higher than sweep the table.key_len
Mysql decided to use the length of the index (which parts), when there may be a
NULL
time,key_len
the length may be larger than the column.ref
It represents a constant for comparison and index or a column.
rows, filtered
rows
Represents the number of lines to be scanned Mysql estimated,filtered
the number of rows can be estimated Mysql filtered (the value here is the number of rows returned to the user), such as rows 100, filtered 70%, then returned to the user estimated Mysql 70 rows of data.Extra
Mysql represent some additional information to resolve a query, such as
Using Index,Using filesort
and so on.
EXPLAIN Join Types
For type column, often used for join
the scene, will be described with reference to low efficiency:
system
const
A special case, this table represents only one row of dataconst
It represents at most only one row of data matches, although the table may have multiple rows of data, but for a table and its join, which requires only one row of data, can be regarded as
const
frequently usedprimary key
orunique key
, such as the following statement:SELECT * FROM tbl_name WHERE primary_key=1; SELECT * FROM tbl_name WHERE primary_key_part1=1 AND primary_key_part2=2;
eq_ref
Mysql index equal operation may be used, it applies only to equal, and the index type must
primary key
Alternativelyunique not null
, when the join, which is in addition tosystem
andconst
best case.ref
When an index is not
primary key
, orunique index
when, or only use the leftmost and they will use theref
case for=
or<=>
fulltext
ref_or_null
Ref and the like, but in addition, we also need to find additional null field, often used in some cases sub-queries, such as the following statement:
SELECT * FROM ref_table WHERE key_column=expr OR key_column IS NULL;
index_merge
Said they would use Section 8.2.1.3, "Index Merge Optimization" . Optimization techniques
unique_subquery
And
eq_ref
an equivalent situation, but for the sub-queries, such as the following statement:value IN (SELECT primary_key FROM single_table WHERE some_expr)
index_subquery
And the
unique_subquery
like, but is specific to the sub-queriesnonunique index
, such the following statement:value IN (SELECT key_column FROM single_table WHERE some_expr)
range
Generally range will be used to find the index, in this case,
key
the column represents the index is used,key_len
represents the longest key part to be used,ref
isNULL
, in the index column and applies a constant to be compared, comprising: a=, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, LIKE, 或者IN
, for example, the following statement: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
Similar
ALL
, but in this case the index scan, usually,Extra
asUsing Index
ALL
The worst case, this case can only be a full table scan, this time should consider creating an index to speed up the
join
pace.
EXPLAIN Extra Information
This section is omitted, because the Extra
information sequence are relatively straightforward, can be met is unclear reference herein .
2 Extended EXPLAIN Output Format
In the EXPLAIN
following, the implementation of SHOW WARNINGS
the command, you will see EXPLAIN
some additional information, and in 8.0.12
after applicable conditions of the additional information as follows: SELECT, DELETE, INSERT, REPLACE和UPDATE
in the 8.0.12
before, this additional information only SELECT
will produce lower sentence, a look at the following example:
mysql> EXPLAIN
SELECT t1.a, t1.a IN (SELECT t2.a FROM t2) FROM t1\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: t1
type: index
possible_keys: NULL
key: PRIMARY
key_len: 4
ref: NULL
rows: 4
filtered: 100.00
Extra: Using index
*************************** 2. row ***************************
id: 2
select_type: SUBQUERY
table: t2
type: index
possible_keys: a
key: a
key_len: 5
ref: NULL
rows: 3
filtered: 100.00
Extra: Using index
2 rows in set, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: /* select#1 */ select `test`.`t1`.`a` AS `a`,
<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in
( <materialize> (/* select#2 */ select `test`.`t2`.`a`
from `test`.`t2` where 1 having 1 ),
<primary_index_lookup>(`test`.`t1`.`a` in
<temporary table> on <auto_key>
where ((`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `t1.a
IN (SELECT t2.a FROM t2)` from `test`.`t1`
1 row in set (0.00 sec)
Note that you can see these queries may be overridden, and contains special tags, so these statements are not necessarily legitimate SQL
statement, which is just to indicate how specific Mysql to parse and execute.
For these special markers, partially explained as follows:
<auto_key>
It is automatically generated temporary key
( expr) Indicates that the statement is cached in memory, it could be use to.
( Query the fragment )
The statement is rewritten became EXISTSAnd so on, there are many, here it is not introduced one by one.
3 Extended EXPLAIN Output Format
EXPLAIN FOR CONNECTION
Statements used in some cases performance diagnosis, such as: a session
time of execution of a very long, you can in one another session
using this command, specify a particular connection id
, then you can see the amount of reasons why the implementation of such a long time, the command format is as follows:
EXPLAIN [options] FOR CONNECTION connection_id;
This command applies to SELECT
, DELETE
, INSERT
, REPLACE
, and UPDATE
. However, the order does not apply to any prepared statement.
connection_id
It is an conection
identifier that is id
available from the INFORMATION_SCHEMA
PROCESSLIST
table or the SHOW PROCESSLIST
acquisition of the statement, but the user must have the relevant authority, or can not get.