Mysql的explain官方文档翻译

2023-10-30

原文地址:
https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain-extra-information

先复制进来,每天翻译一段,有兴趣的小伙伴可以一块加入进来翻译【日拱一卒】

The EXPLAIN statement provides information about how MySQL executes statements. EXPLAIN works with SELECT, DELETE, INSERT, REPLACE, and UPDATE statements.

EXPLAIN语句提供了有关MySQL如何执行语句的信息。explain与select、delete、insert、replace和update语句一起使用。

EXPLAIN returns a row of information for each table used in the SELECT 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.

EXPLAIN为select语句中使用的每个表返回一行信息。它按照MySQL在处理语句时读取的顺序列出输出中的表。MySQL使用嵌套循环联接方法解析所有联接。这意味着MySQL从第一个表中读取一行,然后在第二个表、第三个表等中找到匹配的行。当处理完所有表后,MySQL会通过表列表输出选定的列和回溯,直到找到一个表,其中有更多匹配的行。从该表中读取下一行,该过程将继续执行下一个表。
Alioo旁白:如果一个sql中嵌套了n层,那么执行计划也将输出n行来展示。

EXPLAIN output includes partition information. Also, for SELECT statements, EXPLAIN generates extended information that can be displayed with SHOW WARNINGS following the EXPLAIN (see Section 8.8.3, “Extended EXPLAIN Output Format”).

EXPLAIN计划 输出信息中包括了分区信息。此外,对于select语句,explain生成扩展信息Extra,可以在EXPLAIN语句后执行SHOW WARNINGS显示警告(参见第8.8.3节“扩展解释输出格式”)。
**Alioo旁白:执行过explain… 可以接着执行show warnings;*查看格式化之后的sql *

mysql> explain select * from t where a=1 and b>50000 order by c limit 0,100 ;
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key   | key_len | ref   | rows  | filtered | Extra                       |
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
|  1 | SIMPLE      | t     | NULL       | ref  | idx_a         | idx_a | 5       | const | 10000 |    33.33 | Using where; Using filesort |
+----+-------------+-------+------------+------+---------------+-------+---------+-------+-------+----------+-----------------------------+
1 row in set, 1 warning (0.01 sec)

mysql>
mysql> show warnings;
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                                                               |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select `alioo_dev`.`t`.`id` AS `id`,`alioo_dev`.`t`.`a` AS `a`,`alioo_dev`.`t`.`b` AS `b`,`alioo_dev`.`t`.`c` AS `c` from `alioo_dev`.`t` where ((`alioo_dev`.`t`.`a` = 1) and (`alioo_dev`.`t`.`b` > 50000)) order by `alioo_dev`.`t`.`c` limit 0,100 |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Note
In older MySQL releases, partition and extended information was produced using EXPLAIN PARTITIONS and EXPLAIN EXTENDED. Those syntaxes are still recognized for backward compatibility but partition and extended output is now enabled by default, so the PARTITIONS and EXTENDED keywords are superfluous and deprecated. Their use results in a warning, and they will be removed from EXPLAIN syntax in a future MySQL release.

You cannot use the deprecated PARTITIONS and EXTENDED keywords together in the same EXPLAIN statement. In addition, neither of these keywords can be used together with the FORMAT option.

提示
在旧的MySQL版本中,分区和扩展信息是使用explain partitions和explain extended生成的。这些语法仍然可以识别为向后兼容,但是分区和扩展输出现在默认启用,因此分区和扩展关键字是多余的,不推荐使用。它们的使用会导致一个警告,在未来的MySQL版本中,它们将从解释语法中删除。
不能在同一个explain语句中同时使用不推荐使用的分区PARTITIONS和扩展关键字EXTENDED。此外,这些关键字都不能与格式选项一起使用。

Note
MySQL Workbench has a Visual Explain capability that provides a visual representation of EXPLAIN output. See Tutorial: Using Explain to Improve Query Performance.

提示
MySQ LWorkbench具有可视化解释功能,提供解释输出的可视化表示。请参见教程:使用解释来提高查询性能。

EXPLAIN Output Columns

EXPLAIN Join Types

EXPLAIN Extra Information

EXPLAIN Output Interpretation

EXPLAIN Output Columns
This section describes the output columns produced by EXPLAIN. Later sections provide additional information about the type and Extra columns.

Each output row from EXPLAIN provides information about one table. Each row contains the values summarized in Table 8.1, “EXPLAIN Output Columns”, 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 FORMAT=JSON is used.

Table 8.1 EXPLAIN Output Columns

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

Note
JSON properties which are NULL are not displayed in JSON-formatted EXPLAIN output.

id (JSON name: select_id)

The SELECT identifier. This is the sequential number of the SELECT within the query. The value can be NULL if the row refers to the union result of other rows. In this case, the table column shows a value like <unionM,N> to indicate that the row refers to the union of the rows with id values of M and N.

select_type (JSON name: none)

The type of SELECT, which can be any of those shown in the following table. A JSON-formatted EXPLAIN exposes the SELECT type as a property of a query_block, unless it is SIMPLE or PRIMARY. The JSON names (where applicable) are also shown in the table.

select_type Value JSON Name Meaning
SIMPLE None Simple SELECT (not using UNION or subqueries)
PRIMARY None Outermost SELECT
UNION None Second or later SELECT statement in a UNION
DEPENDENT UNION dependent (true) Second or later SELECT statement in a UNION, dependent on outer query
UNION RESULT union_result Result of a UNION.
SUBQUERY None First SELECT in subquery
DEPENDENT SUBQUERY dependent (true) First SELECT in subquery, dependent on outer query
DERIVED None Derived table
MATERIALIZED materialized_from_subquery Materialized subquery
UNCACHEABLE SUBQUERY cacheable (false) A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
UNCACHEABLE UNION cacheable (false) The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)
DEPENDENT typically signifies the use of a correlated subquery. See Section 13.2.10.7, “Correlated Subqueries”.

DEPENDENT SUBQUERY evaluation differs from UNCACHEABLE SUBQUERY evaluation. For DEPENDENT SUBQUERY, the subquery is re-evaluated only once for each set of different values of the variables from its outer context. For UNCACHEABLE SUBQUERY, 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 Section 8.10.3.1, “How the Query Cache Operates”). Subquery caching occurs during query execution, whereas the query cache is used to store results only after query execution finishes.

When you specify FORMAT=JSON with EXPLAIN, the output has no single property directly equivalent to select_type; the query_block property corresponds to a given SELECT. Properties equivalent to most of the SELECT subquery types just shown are available (an example being materialized_from_subquery for MATERIALIZED), and are displayed when appropriate. There are no JSON equivalents for SIMPLE or PRIMARY.

The select_type value for non-SELECT statements displays the statement type for affected tables. For example, select_type is DELETE for DELETE statements.

table (JSON name: table_name)

The name of the table to which the row of output refers. This can also be one of the following values:

<unionM,N>: The row refers to the union of the rows with id values of M and N.

: The row refers to the derived table result for the row with an id value of N. A derived table may result, for example, from a subquery in the FROM clause.

: The row refers to the result of a materialized subquery for the row with an id value of N. See Section 8.2.2.2, “Optimizing Subqueries with Materialization”.

partitions (JSON name: partitions)

The partitions from which records would be matched by the query. The value is NULL for nonpartitioned tables. See Section 22.3.5, “Obtaining Information About Partitions”.

type (JSON name: access_type)

The join type. For descriptions of the different types, see EXPLAIN Join Types.

possible_keys (JSON name: possible_keys)

The possible_keys column indicates the indexes from which MySQL can choose 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 EXPLAIN. That means that some of the keys in possible_keys might not be usable in practice with the generated table order.

If this column is NULL (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 WHERE 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 EXPLAIN again. See Section 13.1.8, “ALTER TABLE Syntax”.

To see what indexes a table has, use SHOW INDEX FROM tbl_name.

key (JSON name: key)

The key column indicates the key (index) that MySQL actually decided to use. If MySQL decides to use one of the possible_keys indexes to look up rows, that index is listed as the key value.

It is possible that key will name an index that is not present in the possible_keys value. This can happen if none of the possible_keys 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 InnoDB, a secondary index might cover the selected columns even if the query also selects the primary key because InnoDB stores the primary key value with each secondary index. If key is NULL, MySQL found no index to use for executing the query more efficiently.

To force MySQL to use or ignore an index listed in the possible_keys column, use FORCE INDEX, USE INDEX, or IGNORE INDEX in your query. See Section 8.9.4, “Index Hints”.

For MyISAM tables, running ANALYZE TABLE helps the optimizer choose better indexes. For MyISAM tables, myisamchk --analyze does the same. See Section 13.7.2.1, “ANALYZE TABLE Syntax”, and Section 7.6, “MyISAM Table Maintenance and Crash Recovery”.

key_len (JSON name: key_length)

The key_len column indicates the length of the key that MySQL decided to use. The value of key_len enables you to determine how many parts of a multiple-part key MySQL actually uses. If the key column says NULL, the len_len column also says NULL.

Due to the key storage format, the key length is one greater for a column that can be NULL than for a NOT NULL column.

ref (JSON name: ref)

The ref column shows which columns or constants are compared to the index named in the key column to select rows from the table.

If the value is func, the value used is the result of some function. To see which function, use SHOW WARNINGS following EXPLAIN to see the extended EXPLAIN output. The function might actually be an operator such as an arithmetic operator.

rows (JSON name: rows)

The rows column indicates the number of rows MySQL believes it must examine to execute the query.

For InnoDB tables, this number is an estimate, and may not always be exact.

filtered (JSON name: filtered)

The filtered column indicates an estimated percentage of table rows that will be 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 and rows × filtered shows the number of rows that will be joined with the following table. For example, if rows is 1000 and filtered is 50.00 (50%), the number of rows to be joined with the following table is 1000 × 50% = 500.

Extra (JSON name: none)

This column contains additional information about how MySQL resolves the query. For descriptions of the different values, see EXPLAIN Extra Information.

There is no single JSON property corresponding to the Extra column; however, values that can occur in this column are exposed as JSON properties, or as the text of the message property.

EXPLAIN Join Types
The type column of EXPLAIN output describes how tables are joined. In JSON-formatted output, these are found as values of the access_type property. 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

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 a PRIMARY KEY or UNIQUE index to constant values. In the following queries, tbl_name can be used as a const table:

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.

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:

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:

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:

SELECT * FROM ref_table
WHERE key_column=expr OR key_column IS NULL;
See Section 8.2.1.13, “IS NULL Optimization”.

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”.

unique_subquery

This type replaces eq_ref for some IN subqueries of the following form:

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.

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:

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.

range can be used when a key column is compared to a constant using any of the =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, LIKE, or IN() operators:

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.

MySQL can use this join type when the query uses only columns that are part of a single index.

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.

EXPLAIN Extra Information
The Extra column of EXPLAIN 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 Extra value. For some of these, there is a specific property. The others display as the text of the message property.

If you want to make your queries as fast as possible, look out for Extra column values of Using filesort and Using temporary, or, in JSON-formatted EXPLAIN output, for using_filesort and using_temporary_table properties equal to true.

Child of ‘table’ pushed join@1 (JSON: message 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 NDB Cluster, when pushed-down joins are enabled. See the description of the ndb_join_pushdown server system variable for more information and examples.

const row not found (JSON property: const_row_not_found)

For a query such as SELECT … FROM tbl_name, the table was empty.

Deleting all rows (JSON property: message)

For DELETE, some storage engines (such as MyISAM) support a handler method that removes all table rows in a simple and fast way. This Extra value is displayed if the engine uses this optimization.

Distinct (JSON property: distinct)

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.

FirstMatch(tbl_name) (JSON property: first_match)

The semijoin FirstMatch join shortcutting strategy is used for tbl_name.

Full scan on NULL key (JSON property: message)

This occurs for subquery optimization as a fallback strategy when the optimizer cannot use an index-lookup access method.

Impossible HAVING (JSON property: message)

The HAVING clause is always false and cannot select any rows.

Impossible WHERE (JSON property: message)

The WHERE clause is always false and cannot select any rows.

Impossible WHERE noticed after reading const tables (JSON property: message)

MySQL has read all const (and system) tables and notice that the WHERE clause is always false.

LooseScan(m…n) (JSON property: message)

The semijoin LooseScan strategy is used. m and n are key part numbers.

No matching min/max row (JSON property: message)

No row satisfies the condition for a query such as SELECT MIN(…) FROM … WHERE condition.

no matching row in const table (JSON property: message)

For a query with a join, there was an empty table or a table with no rows satisfying a unique index condition.

No matching rows after partition pruning (JSON property: message)

For DELETE or UPDATE, the optimizer found nothing to delete or update after partition pruning. It is similar in meaning to Impossible WHERE for SELECT statements.

No tables used (JSON property: message)

The query has no FROM clause, or has a FROM DUAL clause.

For INSERT or REPLACE statements, EXPLAIN displays this value when there is no SELECT part. For example, it appears for EXPLAIN INSERT INTO t VALUES(10) because that is equivalent to EXPLAIN INSERT INTO t SELECT 10 FROM DUAL.

Not exists (JSON property: message)

MySQL was able to do a LEFT JOIN 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 LEFT JOIN criteria. Here is an example of the type of query that can be optimized this way:

SELECT * FROM t1 LEFT JOIN t2 ON t1.id=t2.id
WHERE t2.id IS NULL;
Assume that t2.id is defined as NOT NULL. In this case, MySQL scans t1 and looks up the rows in t2 using the values of t1.id. If MySQL finds a matching row in t2, it knows that t2.id can never be NULL, and does not scan through the rest of the rows in t2 that have the same id value. In other words, for each row in t1, MySQL needs to do only a single lookup in t2, regardless of how many rows actually match in t2.

Plan isn’t ready yet (JSON property: none)

This value occurs with EXPLAIN FOR CONNECTION 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 Extra value, depending on the progress of the optimizer in determining the full execution plan.

Range checked for each record (index map: N) (JSON property: message)

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 range or index_merge 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 Section 8.2.1.2, “Range Optimization”, and Section 8.2.1.3, “Index Merge Optimization”, 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 SHOW INDEX for the table. The index map value N is a bitmask value that indicates which indexes are candidates. For example, a value of 0x19 (binary 11001) means that indexes 1, 4, and 5 will be considered.

Scanned N databases (JSON property: message)

This indicates how many directory scans the server performs when processing a query for INFORMATION_SCHEMA tables, as described in Section 8.2.3, “Optimizing INFORMATION_SCHEMA Queries”. The value of N can be 0, 1, or all.

Select tables optimized away (JSON property: message)

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 GROUP BY 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:

SELECT MIN(c1), MIN(c2) FROM t1;
Suppose that MIN(c1) can be retrieved by reading one index row and MIN(c2) can be retrieved by reading one row from a different index. That is, for each column c1 and c2, 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 Extra value does not occur if the rows to read are not deterministic. Consider this query:

SELECT MIN(c2) FROM t1 WHERE c1 <= 10;
Suppose that (c1, c2) is a covering index. Using this index, all rows with c1 <= 10 must be scanned to find the minimum c2 value. By contrast, consider this query:

SELECT MIN(c2) FROM t1 WHERE c1 = 10;
In this case, the first index row with c1 = 10 contains the minimum c2 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 MyISAM, but not InnoDB), this Extra value can occur for COUNT(*) queries for which the WHERE clause is missing or always true and there is no GROUP BY clause. (This is an instance of an implicitly grouped query where the storage engine influences whether a deterministic number of rows can be read.)

Skip_open_table, Open_frm_only, Open_full_table (JSON property: message)

These values indicate file-opening optimizations that apply to queries for INFORMATION_SCHEMA tables, as described in Section 8.2.3, “Optimizing INFORMATION_SCHEMA Queries”.

Skip_open_table: Table files do not need to be opened. The information has already become available within the query by scanning the database directory.

Open_frm_only: Only the table’s .frm file need be opened.

Open_full_table: The unoptimized information lookup. The .frm, .MYD, and .MYI files must be opened.

Start temporary, End temporary (JSON property: message)

This indicates temporary table use for the semijoin Duplicate Weedout strategy.

unique row not found (JSON property: message)

For a query such as SELECT … FROM tbl_name, no rows satisfy the condition for a UNIQUE index or PRIMARY KEY on the table.

Using filesort (JSON property: using_filesort)

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 WHERE clause. The keys then are sorted and the rows are retrieved in sorted order. See Section 8.2.1.14, “ORDER BY Optimization”.

Using index (JSON property: using_index)

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 InnoDB tables that have a user-defined clustered index, that index can be used even when Using index is absent from the Extra column. This is the case if type is index and key is PRIMARY.

Using index condition (JSON property: using_index_condition)

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 (“push down”) reading full table rows unless it is necessary. See Section 8.2.1.5, “Index Condition Pushdown Optimization”.

Using index for group-by (JSON property: using_index_for_group_by)

Similar to the Using index table access method, Using index for group-by indicates that MySQL found an index that can be used to retrieve all columns of a GROUP BY or DISTINCT 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 Section 8.2.1.15, “GROUP BY Optimization”.

Using join buffer (Block Nested Loop), Using join buffer (Batched Key Access) (JSON property: using_join_buffer)

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. (Block Nested Loop) indicates use of the Block Nested-Loop algorithm and (Batched Key Access) indicates use of the Batched Key Access algorithm. That is, the keys from the table on the preceding line of the EXPLAIN output will be buffered, and the matching rows will be fetched in batches from the table represented by the line in which Using join buffer appears.

In JSON-formatted output, the value of using_join_buffer is always either one of Block Nested Loop or Batched Key Access.

Using MRR (JSON property: message)

Tables are read using the Multi-Range Read optimization strategy. See Section 8.2.1.10, “Multi-Range Read Optimization”.

Using sort_union(…), Using union(…), Using intersect(…) (JSON property: message)

These indicate the particular algorithm showing how index scans are merged for the index_merge join type. See Section 8.2.1.3, “Index Merge Optimization”.

Using temporary (JSON property: using_temporary_table)

To resolve the query, MySQL needs to create a temporary table to hold the result. This typically happens if the query contains GROUP BY and ORDER BY clauses that list columns differently.

Using where (JSON property: attached_condition)

A WHERE 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 Extra value is not Using where and the table join type is ALL or index.

Using where has no direct counterpart in JSON-formatted output; the attached_condition property contains any WHERE condition used.

Using where with pushed condition (JSON property: message)

This item applies to NDB tables only. It means that NDB 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 “pushed down” to the cluster’s data nodes and is evaluated on all data nodes simultaneously. This eliminates the need to send nonmatching rows over the network, and can speed up such queries by a factor of 5 to 10 times over cases where Condition Pushdown could be but is not used. For more information, see Section 8.2.1.4, “Engine Condition Pushdown Optimization”.

Zero limit (JSON property: message)

The query had a LIMIT 0 clause and cannot select any rows.

EXPLAIN Output Interpretation
You can get a good indication of how good a join is by taking the product of the values in the rows column of the EXPLAIN output. This should tell you roughly how many rows MySQL must examine to execute the query. If you restrict queries with the max_join_size system variable, this row product also is used to determine which multiple-table SELECT statements to execute and which to abort. See Section 5.1.1, “Configuring the Server”.

The following example shows how a multiple-table join can be optimized progressively based on the information provided by EXPLAIN.

Suppose that you have the SELECT statement shown here and that you plan to examine it using EXPLAIN:

EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,
tt.ProjectReference, tt.EstimatedShipDate,
tt.ActualShipDate, tt.ClientID,
tt.ServiceCodes, tt.RepetitiveID,
tt.CurrentProcess, tt.CurrentDPPerson,
tt.RecordVolume, tt.DPPrinted, et.COUNTRY,
et_1.COUNTRY, do.CUSTNAME
FROM tt, et, et AS et_1, do
WHERE tt.SubmitTime IS NULL
AND tt.ActualPC = et.EMPLOYID
AND tt.AssignedPC = et_1.EMPLOYID
AND tt.ClientID = do.CUSTNMBR;
For this example, make the following assumptions:

The columns being compared have been declared as follows.

Table Column Data Type
tt ActualPC CHAR(10)
tt AssignedPC CHAR(10)
tt ClientID CHAR(10)
et EMPLOYID CHAR(15)
do CUSTNMBR CHAR(15)
The tables have the following indexes.

Table Index
tt ActualPC
tt AssignedPC
tt ClientID
et EMPLOYID (primary key)
do CUSTNMBR (primary key)
The tt.ActualPC values are not evenly distributed.

Initially, before any optimizations have been performed, the EXPLAIN statement produces the following information:

table type possible_keys key key_len ref rows Extra
et ALL PRIMARY NULL NULL NULL 74
do ALL PRIMARY NULL NULL NULL 2135
et_1 ALL PRIMARY NULL NULL NULL 74
tt ALL AssignedPC, NULL NULL NULL 3872
ClientID,
ActualPC
Range checked for each record (index map: 0x23)
Because type is ALL 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, VARCHAR and CHAR are considered the same if they are declared as the same size. tt.ActualPC is declared as CHAR(10) and et.EMPLOYID is CHAR(15), so there is a length mismatch.

To fix this disparity between column lengths, use ALTER TABLE to lengthen ActualPC from 10 characters to 15 characters:

mysql> ALTER TABLE tt MODIFY ActualPC VARCHAR(15);
Now tt.ActualPC and et.EMPLOYID are both VARCHAR(15). Executing the EXPLAIN statement again produces this result:

table type possible_keys key key_len ref rows Extra
tt ALL AssignedPC, NULL NULL NULL 3872 Using
ClientID, where
ActualPC
do ALL PRIMARY NULL NULL NULL 2135
Range checked for each record (index map: 0x1)
et_1 ALL PRIMARY NULL NULL NULL 74
Range checked for each record (index map: 0x1)
et eq_ref PRIMARY PRIMARY 15 tt.ActualPC 1
This is not perfect, but is much better: The product of the rows 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 tt.AssignedPC = et_1.EMPLOYID and tt.ClientID = do.CUSTNMBR comparisons:

mysql> ALTER TABLE tt MODIFY AssignedPC VARCHAR(15),
MODIFY ClientID VARCHAR(15);
After that modification, EXPLAIN produces the output shown here:

table type possible_keys key key_len ref rows Extra
et ALL PRIMARY NULL NULL NULL 74
tt ref AssignedPC, ActualPC 15 et.EMPLOYID 52 Using
ClientID, where
ActualPC
et_1 eq_ref PRIMARY PRIMARY 15 tt.AssignedPC 1
do eq_ref PRIMARY PRIMARY 15 tt.ClientID 1
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 tt.ActualPC column are evenly distributed, and that is not the case for the tt table. Fortunately, it is easy to tell MySQL to analyze the key distribution:

mysql> ANALYZE TABLE tt;
With the additional index information, the join is perfect and EXPLAIN produces this result:

table type possible_keys key key_len ref rows Extra
tt ALL AssignedPC NULL NULL NULL 3872 Using
ClientID, where
ActualPC
et eq_ref PRIMARY PRIMARY 15 tt.ActualPC 1
et_1 eq_ref PRIMARY PRIMARY 15 tt.AssignedPC 1
do eq_ref PRIMARY PRIMARY 15 tt.ClientID 1
The rows column in the output from EXPLAIN is an educated guess from the MySQL join optimizer. Check whether the numbers are even close to the truth by comparing the rows product with the actual number of rows that the query returns. If the numbers are quite different, you might get better performance by using STRAIGHT_JOIN in your SELECT statement and trying to list the tables in a different order in the FROM clause. (However, STRAIGHT_JOIN may prevent indexes from being used because it disables semijoin transformations. See Section 8.2.2.1, “Optimizing Subqueries, Derived Tables, and View References with Semijoin Transformations”.)

It is possible in some cases to execute statements that modify data when EXPLAIN SELECT is used with a subquery; for more information, see Section 13.2.10.8, “Derived Tables”.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mysql的explain官方文档翻译 的相关文章

  • 如何将 php Web 应用程序转换为桌面应用程序并保留数据库 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我们有一个用 PHP 开发的 Web 应用程序 但大多数客户并没有一直连接到互联网 那么 有没有办法将应用程序转换为桌面应用程序 以便
  • 更新重复密钥上的复合密钥 [重复]

    这个问题在这里已经有答案了 我需要更新新行 如果两者都满足 date dat and empId who 作为复合键 但如果其中之一或两者不同 则插入 sql INSERT INTO history SET endtimestamp now
  • Laravel Group By 和 Order By 不起作用

    我尝试制作一个Laravel 5 8项目 项目中的数据是这样的 id purch name prcvalue 1 10234 Nabila 100 2 10234 Nadeera 450 3 10234 Nabila 540 4 10234
  • Clojure MySQL 语法错误异常(“[...] 靠近 '???????????????' [...]”)

    除了建立连接之外 我在使用 clojure contrib sql 做任何事情时都遇到困难 我有一个 mysqld 在 localhost 3306 上运行 数据库名为clj db 用户 clj user localhost 和密码 clj
  • 我可以在一个查询中更新/选择表吗?

    我需要在查看页面时选择数据并更新 视图 列 有没有一种方法可以在一个查询中执行此操作 或者我是否必须使用不同的查询 如果您不想 不需要使用事务 则可以创建一个存储过程 该过程首先更新视图计数 然后选择值并将其返回给用户
  • 在 PHP 中将十进制/双精度/浮点值与 PDO 绑定的最佳方法是什么?

    看来类常量只涵盖PDO PARAM BOOL PDO PARAM INT and PDO PARAM STR用于绑定 您只是将十进制 浮点 双精度值绑定为字符串还是有更好的方法来处理它们 MySQLi 允许使用 d 类型表示 double
  • 高效插入和更新时检查唯一性

    我的员工表中有 2 列 每列值必须是唯一的 staff code staff name staff id staff code staff name 1 MGT Management 2 IT IT staff 当向表中插入或更新项目时 我
  • 如何使用 PHP 获取列中的所有值?

    我一直在到处寻找这个问题 但仍然找不到解决方案 如何从 mySQL 列中获取所有值并将它们存储在数组中 例如 表名称 客户 列名称 ID 名称 行数 5 我想获取此表中所有 5 个名称的数组 我该如何去做呢 我正在使用 PHP 我试图 SE
  • SQL:查找每个跑步者跑步之间的平均天数

    因此 如果我们给出下表 runner ran Carol 2011 02 01 Alice 2011 02 01 Bob 2011 02 01 Carol 2011 02 02 Bob 2011 02 02 Bob 2011 02 03 B
  • 如何在数据库中保存未来(!)日期

    这个问题专门涉及未来的日期和时间 对于过去的值 UTC 无疑是首选 我想知道是否有人对拯救生命的 最佳 方法有建议futureMySQL 数据库中的日期和时间 或者就此而言一般来说 特别是在该列可以保存不同时区时间的情况下 考虑到时区规则可
  • 项目链接在 Wamp 服务器上不起作用

    我正在另一台计算机上安装 Wamp 服务器来运行中型数据库和 UI 我已成功阻止 IIS 并将服务器路由到 Localhost 8080 但是每当我尝试从 localhost 主页访问我的项目时 在 www 文件中 我被重定向到页面未找到错
  • MYSql 前 10 名及其他总计

    我的查询运行良好 但我只需要前 10 个供应商 然后我需要将所有剩余的总计放在 所有其他 行中 如果没有单独的查询 我该如何做到这一点LIMIT 10 18446744073709551615 SELECT VENDOR fullname
  • 创建rest api url以连接mysql数据库

    我想学习如何创建一个rest api url 以便我可以使用该url获取信息并将信息发布到我的mysql数据库中 谷歌搜索了很多并阅读了各种文章 但没有找到任何精确的内容可以学习 所有内容均以 about api 开头 以已创建的其余 ur
  • MySQL 选择第一个字符在哪里

    如何选择单元格的第一个字符并使用它来定义返回的内容 看看MySQL 字符串 和 控制流 功能 http dev mysql com doc refman 5 1 en functions html 例如 SELECT IF LEFT myF
  • 如何在 MySQL 中创建查询以根据日期和独特字段减去连续行?

    基于SQL根据日期和另一列减去两行 https stackoverflow com questions 12310221 sql subtract two rows based on date and another column我有一个好
  • MySQL 中的 group_concat 性能问题

    我添加了一个group concat到一个查询并杀死了性能 添加之前和之后的解释计划是相同的 所以我对如何优化它感到困惑 这是查询的简化版本 SELECT curRow curRow 1 AS row number docID docTyp
  • mysqldb接口错误

    我对 mysqldb python 的 mysql 模块 有一个非常奇怪的问题 我有一个文件 其中包含用于在表中插入记录的查询 如果我从文件中调用函数 它就可以正常工作 但是当尝试从另一个文件调用其中一个函数时 它会抛出一个 mysql e
  • VB.NET 和 MySql UPDATE 查询

    我的代码在这里没有错误 至少在我调试它时没有错误 我使用VS 2010 但我希望发生的是 当我单击添加按钮时 文本框 txtQty 中的数字将添加到当前保存在 数量 列中的数字中 例如 txtQty 100 该列上的当前值为 200 我想将
  • MySQL 中的类型:BigInt(20) 与 Int(20)

    我想知道两者之间有什么区别BigInt MediumInt and Int是 很明显 它们会允许更大的数量 不过 我可以做一个Int 20 or a BigInt 20 这会让人觉得这并不一定与尺寸有关 一些见解会很棒 只是有点好奇 我一直
  • 有什么方法可以在MySQL中的表名位置使用变量吗?

    我想在表名称位置使用变量 例如 SELECT FROM targetTableName 然而它会出错 有什么方法可以在MySQL中的表名位置使用变量吗 您显示的查询不起作用有两个原因 插入到查询中的用户定义变量将被视为使用字符串文字 而不是

随机推荐

  • python模拟登录网站_Python模拟登录的几种方法

    目录 正文 方法一 直接使用已知的cookie访问 特点 简单 但需要先在浏览器登录 原理 简单地说 cookie保存在发起请求的客户端中 服务器利用cookie来区分不同的客户端 因为http是一种无状态的连接 当服务器一下子收到好几个请
  • 【error】Doubbo 服务启动异常:java.lang.RuntimeException: [source error] getPropertyValue,问题分析,解决方案

    目录 1 报错信息 2 原因分析 3 解决方案 4 提示 1 报错信息 java lang RuntimeException source error getPropertyValue Ljava lang Object Ljava lan
  • R语言—向量

    向量 vector R 语言最基本的数据结构是向量 类似于数学上的集合的概念 由一个或多个元素构成 向量其实是用于存储数值型 字符型 或逻辑型数据的一维数组 创建向量 c 函数 gt a lt 1 给a赋值1 gt a 显示a的值 1 1
  • 解密企业级PPPoE:部署、配置和管理的最佳实践

    亲爱的读者朋友们 今天 我将带你一起探索企业级PPPoE 这个让你畅快玩转互联网的神奇协议 首先 让我们来了解一下什么是PPPoE 它代表着 点对点协议以太网 是一种强大而灵活的网络连接协议 对于企业来说 部署PPPoE意味着你可以轻松实现
  • Java实现实现简单算法之最长对称字符串

    题目 已知一字符串 求其内包含的最长对称字符串 例 已知字符串 google 输出最长对称字符串 goog 已知abada 输出aba 已知sdghjdgzzgdah 输出 dgzzgd 看到题目时 大家第一反应都是模棱两可的 好像可以这样
  • Selenium+Python3之:多线程进行跨浏览器测试

    python多线程跨浏览器测试 1 引言 2 跨浏览器操作及定义 2 1 啥是跨浏览器测试 2 2 为啥要进行跨浏览器测试 2 3 跨浏览器测试执行 3 代码编写实战 1 引言 在WebUI自动化方面的博文 我也是有好一段时间没有更新了 这
  • 什么是期货交易的技术分析(期货交易市场技术分析)

    什么是期货买卖的本领领会 拓展材料 1 期货本领领会规则 期货本领领会的规则是伴随趋向 期货本领领会的表面普通 期货本领领会的表面普通是创造在三个有理的假如之上的 商场动作具备容纳性和容纳性 价钱以一种趋向的办法兴盛 汗青将重演 在这三个假
  • 如何利用Python爬虫在网上接单,一周赚7800元,一天只要两小时 !

    1 兼职处理数据 互联网时代下 越来越多的人离不开电脑办公 而与电脑办公分隔不开的 就是处理电脑上保存的数据 虽然说Excel整理数据功能很强大 但在Python面前 曾经统治职场的它也得败下阵来 因为Python在搜集整理分析数据的过程中
  • ConstraintLayout 高级特性,工具总结

    layout constraintWidth 用法 xml中 app layout constrainedWidth true 作用 使得该view的宽受限于他的约束 app layout constrainedHeight true 同理
  • ChatGPT专业应用:基于关键词撰写原创文章

    正文共 485 字 阅读大约需要 2 分钟 内容运营 SEO投放必备技巧 您将在2分钟后获得以下超能力 基于关键词撰写原创文章 Beezy评级 B级 经过简单的寻找 大部分人能立刻掌握 主要节省时间 推荐人 Kim 编辑者 Linda 此图
  • QT CREATOR 插件开发:添加新的工程类型(上)

    Qt Creator 中 新的工程类型将出现在 文件 gt 新建 菜单项中 我们可以通过打开的选择工程类型的对话框来找到所需要的工程 在本章中 我们将学习如何向上面所示的对话框中添加新的工程类型 Core IWizard接口 Qt Crea
  • 基于富芮坤fr8016 蓝牙5.0 芯片设计的BLE HID Joystick 游戏摇杆设备

    文章目录 ble hid 学习笔记 HID报告描述符与BLE HID profile之间关系 1 HID报告描述符 富芮坤fr8016 设计Joystick例子 1 描述X轴Y轴Z轴 2 描述按钮 3 Joystick 报告描述符 4 程序
  • Linux服务 Nginx(二)

    Linux服务 Nginx 二 最权威的资料 官方文档http nginx org en docs 主配置段的指令 正常运行的必备配置 1 user USERNAME GROUPNAME 指定运行worker进程的用户和组 Syntax u
  • c语言利用穷举法求1-100内的质数

    方法 include
  • 广告数据集mapreduce实验(词频统计)

    本实验是使用广告数据集 通过mapreduce获取购买者年龄人数分布 数据列分别为 ad id xyz campaign id age gender Impressions Clicks Total Conversion Approved
  • 基于MNIST实现GAN(pytorch)

    基于MNIST实现生成对抗网络 pytorch逐行实现 本文是pytorch逐行实现GAN网络 作为一个基础GAN框架来学习 以后编写复杂的GAN的衍生网络框架都是同样的思想 import numpy as np import torch
  • Linux 下获取Root权限的几种方法

    方法分为永久性获取root权限以及非永久性获取Root权限 非永久性获取Root权限 非永久性获取Root权限可以在我们要键入的命令之前加上sudo前缀 如我们要键入的命令是 rm 以Root用户执行的方式就是 sudo rm 这样在每个需
  • 苹果的AI野心:内耗、反击与挑战

    图片来源 由无界AI生成 作者丨山核 苹果一年一度的秋季 春晚 时间越来越近 但在大模型浪潮下 苹果何时推出自己的 苹果GPT 成了另一个关注的话题 毕竟 前有华为 后有小米 在中国手机厂商争相将大模型装进移动终端的同时 苹果却依旧对AI大
  • ubuntu16.04 conda新建环境安装新版gcc教程

    首先 使用conda新建环境 conda create name yourenvname python 3 x 最新版的gcc移步链接 上面是我所选择的版本号 选择了第一个命令 创建一个如上图类似的软链接即可 这一步一定要寻找bin下面的新
  • Mysql的explain官方文档翻译

    原文地址 https dev mysql com doc refman 5 7 en explain output html explain extra information 先复制进来 每天翻译一段 有兴趣的小伙伴可以一块加入进来翻译