原文地址:https://dev.MysqL.com/doc/refman/5.7/en/explain-output.HTML
The statement provIDes information about the execution plan for a statement.
returns a row of information for each table used in the statement. It Lists the tables in the output in the order that MysqL would read them while processing the statement. MysqL resolves all joins using a nested-loop join method. This means that MysqL reads a row from the first table,and then finds a matching row in the second table,the third table,and so on. When all tables are processed,MysqL outputs the selected columns and backtracks through the table List until a table is found for which there are more matching rows. The next row is read from this table and the process continues with the next table.
Before MysqL 5.7.3,when the keyword is used, produces extra information that can be vIEwed by issuing a statement following the statement. also displays the
column. See title="9.8.3 EXPLAIN EXTENDED Output Format" href="https://dev.MysqL.com/doc/refman/5.7/en/explain-extended.HTML">Section 9.8.3,“EXPLAIN EXTENDED Output Format”. As of MysqL 5.7.3,extended output is enabled by default and the
keyword is unnecessary.
You cannot use the and
keywords together in the same statement. In addition,neither of these keywords can be used together with the
option. (
causes
to display extended and partition information automatically; using
has no effect on
output.)
This section describes the output columns produced by . Later sections provIDe additional information about the and columns.
Each output row from provIDes information about one table. Each row contains the values summarized in ,and described in more detail following the table. Column names are shown in the table's first column; the second column provides the equivalent property name shown in the output when is used.
<div >Note
JsON propertIEs which are are not displayed in JsON-formatted
output.
(JsON name: )The IDentifIEr. This is the sequential number of the within the query. The value can be if the row refers to the union result of other rows. In this case,the column shows a value like M
,N
>
to indicate that the row refers to the union of the rows with values of M
and N
.name="explain_select_type"> (JsON name: none)The type of ,which can be any of those shown in the following table. A JsON-formatted exposes the type as a property of a ,unless it is or . The JsON names (where applicable) are also shown in the table.
Value </tr>
<tr>
<td scope="row"><code >PRIMARY</td>
<td>None</td>
<td>Outermost <a title="14.2.9 SELECT Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/select.html"><code >SELECT
</td></tr>
<tr>
<td scope="row"><a title="14.2.9.3 UNION Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/union.html"><code >UNION</td>
<td>None</td>
<td>Second or later <a title="14.2.9 SELECT Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/select.html"><code >SELECT statement in a <a title="14.2.9.3 UNION Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/union.html"><code >UNION
</td>
</tr>
<tr>
<td scope="row"><code >DEPENDENT UNION</td>
<td>
<code >dependent (<code >true)</td>
<td>Second or later <a title="14.2.9 SELECT Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/select.html"><code >SELECT statement in a <a title="14.2.9.3 UNION Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/union.html"><code >UNION,dependent on outer query</td>
</tr>
<tr>
<td scope="row"><code >UNION RESulT</td>
<td><code >union_result</td>
<td>Result of a <a title="14.2.9.3 UNION Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/union.html"><code >UNION.</td>
</tr>
<tr>
<td scope="row"><code >SUBquery</td>
<td>None</td>
<td>First <a title="14.2.9 SELECT Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/select.html"><code >SELECT in subquery</td>
</tr>
<tr>
<td scope="row"><code >DEPENDENT SUBquery</td>
<td>
<code >dependent (<code >true)</td>
<td>First <a title="14.2.9 SELECT Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/select.html"><code >SELECT in subquery,dependent on outer query</td>
</tr>
<tr>
<td scope="row"><code >DERIVED</td>
<td>None</td>
<td>Derived table <a title="14.2.9 SELECT Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/select.html"><code >SELECT (subquery in <code >FROM clause)</td>
</tr>
<tr>
<td scope="row"><code >MATERIAliZED</td>
<td><code >materialized_from_subquery</td>
<td>Materialized subquery</td>
</tr>
<tr>
<td scope="row"><code >UNCACHEABLE SUBquery</td>
<td>
<code >cacheable (<code >false)</td>
<td>A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query</td>
</tr>
<tr>
<td scope="row"><code >UNCACHEABLE UNION</td>
<td>
<code >cacheable (<code >false)</td>
<td>The second or later select in a <a title="14.2.9.3 UNION Syntax" href="https://dev.mysql.com/doc/refman/5.7/en/union.html"><code >UNION that belongs to an uncacheable subquery (see <code >UNCACHEABLE SUBquery)</td>
</tr>
typically signifIEs the use of a correlated subquery. See . evaluation differs from evaluation. For ,the subquery is re-evaluated only once for each set of different values of the variables from its outer context. For ,the subquery is re-evaluated for each row of the outer context.
Cacheability of subqueries differs from caching of query results in the query cache (which is described in ). Subquery caching occurs during query execution,whereas the query cache is used to store results only after query execution finishes.
When you specify with ,the output has no single property directly equivalent to ; the property corresponds to a given . PropertIEs equivalent to most of the subquery types just shown are available (an example being for ),and are displayed when appropriate. There are no JsON equivalents for or .
As of MysqL 5.7.2,the value for non- statements displays the statement type for affected tables. For example, is for statements.
(JSON name: )The name of the table to which the row of output refers. This can also be one of the following values:
M
,N
>
: The row refers to the union of the rows with values of M
and N
.N
>
: The row refers to the derived table result for the row with an value of N
. A derived table may result,for example,from a subquery in the clause.N
>
: The row refers to the result of a materialized subquery for the row with an value of N
. See . (JSON name: )The partitions from which records would be matched by the query. This column is displayed only if the keyword is used. The value is for nonpartitioned tables. See .
name="explain_type"> (JsON name: )The join type. For descriptions of the different types,see Join Types.
name="explain_possible_keys"> (JsON name: )The column indicates which indexes MysqL can choose from use to find the rows in this table. Note that this column is totally independent of the order of the tables as displayed in the output from . That means that some of the keys in might not be usable in practice with the generated table order.
If this column is (or undefined in JsON-formatted output),there are no relevant indexes. In this case,you may be able to improve the performance of your query by examining the clause to check whether it refers to some column or columns that would be suitable for indexing. If so,create an appropriate index and check the query with again. See .
To see what indexes a table has,use tbl_name
. (JsON name: )The column indicates the key (index) that MysqL actually decIDed to use. If MysqL decIDes to use one of the indexes to look up rows,that index is Listed as the key value.
It is possible that will name an index that is not present in the value. This can happen if none of the indexes are suitable for looking up rows,but all the columns selected by the query are columns of some other index. That is,the named index covers the selected columns,so although it is not used to determine which rows to retrIEve,an index scan is more efficIEnt than a data row scan.
For ,a secondary index might cover the selected columns even if the query also selects the primary key because stores the primary key value with each secondary index. If is ,MysqL found no index to use for executing the query more efficIEntly.
To force MysqL to use or ignore an index Listed in the column,use , ,or in your query. See .
For tables,running helps the optimizer choose better indexes. For tables, myisamchk --analyze does the same. See ,and .
(JSON name: )The column indicates the length of the key that MySQL decided to use. The value of enables you to determine how many parts of a multiple-part key MySQL actually uses. If the column says ,the column also says .
Due to the key storage format,the key length is one greater for a column that can be than for a column.
(JsON name: )The column shows which columns or constants are compared to the index named in the column to select rows from the table.
If the value is ,the value used is the result of some function. To see which function,use followed by . The function might actually be an operator such as an arithmetic operator.
(JSON name: )The column indicates the number of rows MySQL believes it must examine to execute the query.
For tables,this number is an estimate,and may not always be exact.
name="explain_filtered"> (JsON name: )The column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, shows the estimated number of rows examined and × / shows the number of rows that will be joined with prevIoUs tables. Before MysqL 5.7.3,this column is displayed if you use . As of MysqL 5.7.3,extended output is enabled by default and the keyword is unnecessary.
name="explain_extra"> (JsON name: none)This column contains additional information about how MysqL resolves the query. For descriptions of the different values,see Extra Information.
There is no single JsON property corresponding to the column; however,values that can occur in this column are exposed as JsON propertIEs,or as the text of the property.
name="explain-join-types">EXPLAIN Join TypesThe column of output describes how tables are joined. In JsON-formatted output,these are found as values of the property. The following List describes the join types,ordered from the best type to the worst:
The table has only one row (= system table). This is a special case of the join type.
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. tables are very fast because they are read only once.
is used when you compare all parts of a or index to constant values. In the following querIEs, tbl_name
can be used as a table:
tbl_name
WHERE primary_key
=1;SELECT * FROM <em >tbl_name
WHERE <em >primary_key_part1
=1 AND <em >primary_key_part2
=2;
One row is read from this table for each combination of rows from the prevIoUs tables. Other than the and 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 or index.
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 join to process ref_table
:
ref_table
,other_table
WHERE ref_table
.key_column
=other_table
.column
;SELECT * FROM <em >ref_table
,<em >other_table
WHERE <em >ref_table
.<em >key_columnpart1
=<em >othertable
.<em >column
AND <em >ref_table
.<em >key_column_part2
=1;
All rows with matching index values are read from this table for each combination of rows from the prevIoUs tables. is used if the join uses only a leftmost prefix of the key or if the key is not a or 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.
can be used for indexed columns that are compared using the or
operator. In the following examples,MysqL can use a join to process ref_table
:
ref_table
WHERE key_column
=expr
;SELECT * FROM <em >ref_table
,<em >other_table
WHERE <em >ref_table
.<em >key_columnpart1
=<em >othertable
.<em >column
AND <em >ref_table
.<em >key_column_part2
=1;
The join is performed using a index.
This join type is like ,but with the addition that MysqL does an extra search for rows that contain values. This join type optimization is used most often in resolving subquerIEs. In the following examples,MysqL can use a join to process ref_table
:
ref_table
WHERE key_column
=expr
OR key_column
IS NulL;See .
This join type indicates that the Index Merge optimization is used. In this case,the column in the output row contains a List of indexes used,and contains a List of the longest key parts for the indexes used. For more information,see .
This type replaces for some subquerIEs of the following form:
value
IN (SELECT primary_key
FROM single_table WHERE some_expr)is just an index lookup function that replaces the subquery completely for better efficIEncy.
This join type is similar to . It replaces subquerIEs,but it works for nonunique indexes in subquerIEs of the following form:
value IN (SELECT key_column FROM single_table WHERE some_expr)
Only rows that are in a given range are retrIEved,using an index to select the rows. The column in the output row indicates which index is used. The
contains the longest key part that was used. The
column is
for this type.
can be used when a key column is compared to a constant using any of the , , , =, , , , , ,or operators:
tbl_name WHERE key_column = 10;SELECT * FROM <em >
tbl_name
WHERE <em >key_column
BETWEEN 10 and 20;SELECT * FROM <em >
tbl_name
WHERE <em >key_column
IN (10,20,30);SELECT * FROM <em >
tbl_name
WHERE <em >key_part1
= 10 AND <em >key_part2
IN (10,30);
The join type is the same as ,except that the index tree is scanned. This occurs two ways:
column says . An index-only scan usually is faster than 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. does not appear in the column.MysqL can use this join type when the query uses only columns that are part of a single index.
name="IDm140007032367152">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 ,and usually very bad in all other cases. normally,you can avoID by adding indexes that enable row retrIEval from the table based on constant values or column values from earlIEr tables.
name="explain-extra-information">EXPLAIN Extra informationThe column of output contains additional information about how MysqL resolves the query. The following List explains the values that can appear in this column. Each item also indicates for JsON-formatted output which property displays the value. For some of these,there is a specific property. The others display as the text of the property.
If you want to make your querIEs as fast as possible,look out for column values of and ,or,in JsON-formatted output,for and propertIEs equal to .
table
' pushed join@1
(JsON: text)This table is referenced as the child of table
in a join that can be pushed down to the NDB kernel. ApplIEs only in MysqL Cluster,when pushed-down joins are enabled. See the description of the server system variable for more information and examples. (JsON property: )For a query such as tbl_name
,the table was empty. (JsON property: )For ,some storage engines (such as ) support a handler method that removes all table rows in a simple and fast way. This value is displayed if the engine uses this optimization.
(JsON property: )MysqL is looking for distinct values,so it stops searching for more rows for the current row combination after it has found the first matching row.
tbl_name
)
(JsON property: )The semi-join FirstMatch join shortcutting strategy is used for tbl_name
. (JsON property: )This occurs for subquery optimization as a fallback strategy when the optimizer cannot use an index-lookup access method.
(JsON property: )The clause is always false and cannot select any rows.
(JsON property: )The clause is always false and cannot select any rows.
(JsON property: )MysqL has read all (and ) tables and notice that the clause is always false.
m
..n
)
(JsON property: )The semi-join LooseScan strategy is used. m
and n
are key part numbers. (JsON property: )No row satisfIEs the condition for a query such as condition
. (JsON property: )For a query with a join,there was an empty table or a table with no rows satisfying a unique index condition.
(JsON property: )For or ,the optimizer found nothing to delete or update after partition pruning. It is similar in meaning to for statements.
(JsON property: )The query has no clause,or has a clause.
For or statements, displays this value when there is no part. For example,it appears for because that is equivalent to .
(JsON property: )MysqL was able to do a optimization on the query and does not examine more rows in this table for the prevIoUs row combination after it finds one row that matches the criteria. Here is an example of the type of query that can be optimized this way:
Assume that is defined as . In this case,MysqL scans and looks up the rows in using the values of . If MysqL finds a matching row in ,it kNows that can never be ,and does not scan through the rest of the rows in that have the same value. In other words,for each row in ,MysqL needs to do only a single lookup in ,regardless of how many rows actually match in .
(JsON property: none)This value occurs with when the optimizer has not finished creating the execution plan for the statement executing in the named connection. If execution plan output comprises multiple lines,any or all of them Could have this value,depending on the progress of the optimizer in determining the full execution plan.
N
)
(JsON property: )MysqL found no good index to use,but found that some of indexes might be used after column values from preceding tables are kNown. For each row combination in the preceding tables,MysqL checks whether it is possible to use a or access method to retrIEve rows. This is not very fast,but is faster than performing a join with no index at all. The applicability criteria are as described in ,and ,with the exception that all column values for the preceding table are kNown and consIDered to be constants.
Indexes are numbered beginning with 1,in the same order as shown by for the table. The index map value N
is a bitmask value that indicates which indexes are candIDates. For example,a value of (binary 11001) means that indexes 1,4,and 5 will be consIDered.N
databases
(JsON property: )This indicates how many directory scans the server performs when processing a query for tables,as described in . The value of N
can be 0,1,or .
(JSON property: )The optimizer determined 1) that at most one row should be returned,and 2) that to produce this row,a deterministic set of rows must be read. When the rows to be read can be read during the optimization phase (for example,by reading index rows),there is no need to read any tables during query execution.
The first condition is fulfilled when the query is implicitly grouped (contains an aggregate function but no clause). The second condition is fulfilled when one row lookup is performed per index used. The number of indexes read determines the number of rows to read.
Consider the following implicitly grouped query:
Suppose that can be retrieved by reading one index row and can be retrieved by reading one row from a different index. That is,for each column and ,there exists an index where the column is the first column of the index. In this case,one row is returned,produced by reading two deterministic rows.
This value does not occur if the rows to read are not deterministic. Consider this query:
Suppose that is a covering index. Using this index,all rows with must be scanned to find the minimum value. By contrast,consider this query:
In this case,the first index row with contains the minimum value. Only one row must be read to produce the returned row.
For storage engines that maintain an exact row count per table (such as ,but not ),this value can occur for queries for which the clause is missing or always true and there is no clause. (This is an instance of an implicitly grouped query where the storage engine influences whether a deterministic number of rows can be read.)
, , (JSON property: )These values indicate file-opening optimizations that apply to queries for tables,“Optimizing INFORMATION_SCHEMA Queries”.
: table files do not need to be opened. The information has already become available within the query by scanning the database directory.: Only the table's file need be opened.: The unoptimized information lookup. The , ,and files must be opened., (JsON property: )This indicates temporary table use for the semi-join Duplicate Weedout strategy.
(JsON property: )For a query such as tbl_name
,no rows satisfy the condition for a index or on the table. (JsON property: )MysqL must do an extra pass to find out how to retrIEve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the clause. The keys then are sorted and the rows are retrIEved in sorted order. See .
(JSON property: )The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.
For tables that have a user-defined clustered index,that index can be used even when is absent from the column. This is the case if is and is .
(JsON property: )tables are read by accessing index tuples and testing them first to determine whether to read full table rows. In this way,index information is used to defer (
(JSON property: )Similar to the table access method, indicates that MySQL found an index that can be used to retrieve all columns of a or query without any extra disk access to the actual table. Additionally,the index is used in the most efficient way so that for each group,only a few index entries are read. For details,see .
, (JsON property: )tables from earlIEr joins are read in portions into the join buffer,and then their rows are used from the buffer to perform the join with the current table. indicates use of the Block nested-Loop algorithm and indicates use of the Batched Key Access algorithm. That is,the keys from the table on the preceding line of the output will be buffered,and the matching rows will be fetched in batches from the table represented by the line in which appears.
In JsON-formatted output,the value of is always either one of or .
(JsON property: )tables are read using the Multi-Range Read optimization strategy. See title="9.2.1.13 Multi-Range Read Optimization" href="https://dev.MysqL.com/doc/refman/5.7/en/mrr-optimization.HTML">Section 9.2.1.13,“Multi-Range Read Optimization”.
, , (JsON property: )These indicate how index scans are merged for the join type. See title="9.2.1.4 Index Merge Optimization" href="https://dev.MysqL.com/doc/refman/5.7/en/index-merge-optimization.HTML">Section 9.2.1.4,“Index Merge Optimization”.
(JsON property: )To resolve the query,MysqL needs to create a temporary table to hold the result. This typically happens if the query contains and clauses that List columns differently.
(JsON property: )A clause is used to restrict which rows to match against the next table or send to the clIEnt. Unless you specifically intend to fetch or examine all rows from the table,you may have something wrong in your query if the value is not and the table join type is or .
has no direct counterpart in JsON-formatted output; the property contains any condition used.
(JsON property: )This item applIEs to tables only. It means that MysqL Cluster is using the Condition Pushdown optimization to improve the efficIEncy of a direct comparison between a nonindexed column and a constant. In such cases,the condition is
(JSON property: )The query had a clause and cannot select any rows.
EXPLAIN Output InterpretationYou can get a good indication of how good a join is by taking the product of the values in the column of the output. This should tell you roughly how many rows MysqL must examine to execute the query. If you restrict querIEs with the system variable,this row product also is used to determine which multiple-table statements to execute and which to abort. See title="6.1.1 Configuring the Server" href="https://dev.MysqL.com/doc/refman/5.7/en/server-configuration.HTML">Section 6.1.1,“Configuring the Server”.
The following example shows how a multiple-table join can be optimized progressively based on the information provIDed by .
Suppose that you have the statement shown here and that you plan to examine it using :
For this example,make the following assumptions:
The columns being compared have been declared as follows. (primary key) (primary key) values are not evenly distributed.Initially,before any optimizations have been performed,the statement produces the following information:
Because is for each table,this output indicates that MysqL is generating a Cartesian product of all the tables; that is,every combination of rows. This takes quite a long time,because the product of the number of rows in each table must be examined. For the case at hand,this product is 74 × 2135 × 74 × 3872 = 45,268,558,720 rows. If the tables were bigger,you can only imagine how long it would take.
One problem here is that MysqL can use indexes on columns more efficIEntly if they are declared as the same type and size. In this context, and are consIDered the same if they are declared as the same size. is declared as and is ,so there is a length mismatch.
To fix this disparity between column lengths,use to lengthen from 10 characters to 15 characters:
ALTER table tt MODIFY ActualPC VARCHAR(15);
Now and are both . Executing the statement again produces this result:
This is not perfect,but is much better: The product of the values is less by a factor of 74. This version executes in a couple of seconds.
A second alteration can be made to eliminate the column length mismatches for the and comparisons:
ALTER table tt MODIFY AssignedPC VARCHAR(15), -> MODIFY ClIEntID VARCHAR(15);
After that modification, produces the output shown here:
At this point,the query is optimized almost as well as possible. The remaining problem is that,by default,MysqL assumes that values in the column are evenly distributed,and that is not the case for the table. Fortunately,it is easy to tell MysqL to analyze the key distribution:
ANALYZE table tt;
With the additional index information,the join is perfect and produces this result:
name="IDm140007032046432">The column in the output from is an educated guess from the MysqL join optimizer. Check whether the numbers are even close to the truth by comparing the product with the actual number of rows that the query returns. If the numbers are quite different,you might get better performance by using in your statement and trying to List the tables in a different order in the clause. (However, may prevent indexes from being used because it disables semi-join transformations. See .)
It is possible in some cases to execute statements that modify data when is used with a subquery; for more information,see title="14.2.10.8 SubquerIEs in the FROM Clause" href="https://dev.MysqL.com/doc/refman/5.7/en/from-clause-subquerIEs.HTML">Section 14.2.10.8,“SubquerIEs in the FROM Clause”.
总结 以上是内存溢出为你收集整理的看懂mysql执行计划--官方文档全部内容,希望文章能够帮你解决看懂mysql执行计划--官方文档所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
mysql表名查询sql
上一篇
2022-06-02
mysql悲观锁总结和实践--转
下一篇
2022-06-02
评论列表(0条)