|
Depending on the details of your tables, columns, indexes, and the conditions in your WHERE clause, the MySQL optimizer considers many techniques to efficiently perform the lookups involved in an SQL query. A query on a huge【hjuːdʒ 巨大的;非常成功的;极多的;走红的;程度高的;】 table can be performed without reading all the rows; a join involving several tables can be performed without comparing every combination of rows. The set of operations that the optimizer chooses to perform the most efficient query is called the “query execution plan”, also known as the EXPLAIN plan. Your goals are to recognize the aspects of the EXPLAIN plan that indicate a query is optimized well, and to learn the SQL syntax and indexing techniques to improve the plan if you see some inefficient operations.
1 Optimizing Queries with EXPLAIN
The EXPLAIN statement provides information about how MySQL executes statements:
• EXPLAIN works with SELECT, DELETE, INSERT, REPLACE, and UPDATE statements.
• When EXPLAIN is used with an explainable【可解释的;可说明的;】 statement, MySQL displays information from the optimizer about the statement execution plan. That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order.
• When EXPLAIN is used with FOR CONNECTION connection_id rather than an explainable statement, it displays the execution plan for the statement executing in the named connection.
• For SELECT statements, EXPLAIN produces additional execution plan information that can be displayed using SHOW WARNINGS.
• EXPLAIN is useful for examining queries involving partitioned tables.
• The FORMAT option can be used to select the output format. TRADITIONAL presents the output in tabular【ˈtæbjələr 表格式的;制成表的;列成表的;】 format. This is the default if no FORMAT option is present. JSON format displays the information in JSON format.
With the help of EXPLAIN, you can see where you should add indexes to tables so that the statement executes faster by using indexes to find rows. You can also use EXPLAIN to check whether the optimizer joins the tables in an optimal order. To give a hint【hɪnt 提示;暗示;迹象;示意;少许;少量;征兆;秘诀;】 to the optimizer to use a join order corresponding【ˌkɔːrəˈspɑːndɪŋ 相应的;相关的;符合的;】 to the order in which the tables are named in a SELECT statement, begin the statement with SELECT STRAIGHT_JOIN rather than just SELECT.However, STRAIGHT_JOIN may prevent indexes from being used because it disables semijoin transformations.
The optimizer trace may sometimes provide information complementary【ˌkɑːmplɪˈmentri 互补的;补充的;相互补足的;】 to that of EXPLAIN. However, the optimizer trace format and content are subject to change between versions.
If you have a problem with indexes not being used when you believe that they should be, run ANALYZE TABLE to update table statistics, such as cardinality of keys, that can affect the choices the optimizer makes.
2 EXPLAIN Output Format
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. This means that MySQL reads a row from the first table, then finds a matching row in the second table, and then in the third table, and so on. When all tables are processed, MySQL outputs the selected columns and backtracks【ˈbæktræks 原路返回;(屈于压力而)改变声明(或主张),出尔反尔;退缩;折回;折返;】 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.
【MySQL Workbench has a Visual Explain capability that provides a visual representation of EXPLAIN output.】
2.1 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.Column names are shown in the table's first column; the second column provides the equivalent【ɪˈkwɪvələnt (价值、数量、意义、重要性等)相同的;相等的;】 property name shown in the output when FORMAT=JSON is used.
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_key | possible_key | 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 | 【JSON properties which are NULL are not displayed in JSON-formatted EXPLAIN output.】
• id (JSON name: select_id)
The SELECT identifier【aɪˈdentɪfaɪər 标识符,标识号,识别字(可用以进入程序或其中的数据集);】. This is the sequential【sɪˈkwenʃl 顺序的,按次序的;连续的;序列的;相继的;作为结果产生的,随之而来的;(避孕丸)按期服用以消除副作用的;】 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 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 | DEPENDENT DERIVED | dependent (true) | Derived table dependent on another 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【ˈsɪɡnɪfaɪz 表示;表达,显示(感情、意愿等);说明;预示;要紧;具有重要性;】 the use of a correlated subquery.
DEPENDENT SUBQUERY evaluation【ɪˌvæljuˈeɪʃn 评价;评审;估计;定值;计值;】 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.
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:
- : 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.
• partitions (JSON name: partitions)
The partitions【pɑːrˈtɪʃnz 分割;隔断;分治;瓜分;隔扇;隔板墙;】 from which records would be matched by the query. The value is NULL for nonpartitioned tables.
• type (JSON name: access_type)
The join type.
• 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【ˈreləvənt 相关的;有意义的;有价值的;切题的;】 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.
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 may 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.
For MyISAM tables, running ANALYZE TABLE helps the optimizer choose better indexes. For MyISAM tables, myisamchk --analyze does the same.
• 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 key_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 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 and rows × filtered shows the number of rows that are 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【rɪˈzɑːlvz 解决(问题或困难);决心;决定;表决;作出决定;作出决议;】 the query.
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.
2.2 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【kəmˈperd 相比;比较的,对照的;】 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【ˌkɑːmbɪˈneɪʃn 结合;联合;混合;结合体;联合体;混合体;(用于开密码锁的)数码组合,字码组合;(旧时的)连裤内衣;】 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;
复制代码 • 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.
• 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 =, , >, >=, |
|