Detailed one MySQL Explain

This switched: https://www.cnblogs.com/xuanzhi201111/p/4175635.html

In their daily work, we will have time to record the query will slow down some of the relatively long time to execute SQL statements, SQL identify these statements does not mean the job done, something we often use explain this command to see one of these SQL statements execution plan, see the SQL statement has no use on the index, have not done a full table scan, which can be viewed by explain command. So we understand MySQL's cost-based optimizer, you can also get a lot of details may be optimized access policy is taken into account, as well as when running SQL statements which strategy is expected to be adopted optimizer. (QEP: sql execution plan to generate a query Execution plan)

Copy the code

mysql> explain select * from servers;
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | servers | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
1 row in set (0.03 sec)

Copy the code

expain have 10 out of the information, namely, id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra, the following occur for these fields may be interpreted:

A,  the above mentioned id

     My understanding is that the order of execution of SQL identifier, execute SQL descending

1. id are the same, the order from top to bottom

2. If the subquery, id serial number is incremented, id value the greater the higher the priority, the first to be executed

3.id if the same, may be considered as a group, from the order of execution down; in all groups, the greater the id value, the higher priority, the first execution

 

 

二、select_type

      Shows the type of each query select clause

(1) SIMPLE (simple SELECT, not using UNION or subqueries)

(2) PRIMARY (if the query contains any complex sub-portion, the outermost layer is marked as a PRIMARY select)

(3) UNION (UNION second or later SELECT statement)

(4) DEPENDENT UNION (second or later SELECT statement UNION, depending on the outside of the query)

(5) UNION RESULT (UNION result)

(6) SUBQUERY (first sub-SELECT query)

(7) DEPENDENT SUBQUERY (first subquery SELECT, depending on the outside of the query)

(8) DERIVED (derived table SELECT, FROM clause subqueries)

(9) UNCACHEABLE SUBQUERY (the result of a sub-query can not be cached, it must re-evaluate the first row are linked)

 

Three, table

This line of display data about which tables, and sometimes not true table name, see derivedx (x is a number, I understand that the results of the first steps performed)

Copy the code

mysql> explain select * from (select * from ( select * from t1 where id=2602) a) b;
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
| id | select_type | table      | type   | possible_keys     | key     | key_len | ref  | rows | Extra |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
|  1 | PRIMARY     | <derived2> | system | NULL              | NULL    | NULL    | NULL |    1 |       |
|  2 | DERIVED     | <derived3> | system | NULL              | NULL    | NULL    | NULL |    1 |       |
|  3 | DERIVED     | t1         | const  | PRIMARY,idx_t1_id | PRIMARY | 4       |      |    1 |       |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+

Copy the code

 

Four, type

MySQL find a way to express the desired line in the table, also known as "access type."

Commonly used types are:  ALL, index, Range, REF, eq_ref, const, System, NULL (from left to right, from poor to good performance)

ALL: Full Table Scan, MySQL will traverse the whole table to find matching rows

index: Full Index Scan, index and index types to distinguish ALL traversing the index tree only

range: only retrieves a row of a given range, using an index to select the rows

ref: indicates the matching condition of the connection table, that is, which is used to find the column or constant value in the index column

eq_ref: REF Similarly, the difference in the index using a unique index for each index key, only one record in the table match, simply put, is to use a multi-table join as a primary key or a unique key associated with conditions

const, system: when a part of MySQL query optimization, and converted to a constant, using these types of access. As will be placed where the primary key list, MySQL can convert the query to a constant, const type of system is a special case, in the case where only one row of table queries, the use of system

NULL: MySQL decomposition statement in the optimization process, even without access to a table or index when performing, for example, selected from an indexed column in the minimum can be done through a separate index lookup.

 

五、possible_keys

It indicates which indexes MySQL could use to find records in a table, query if there is an index, the index will be listed on the fields involved, but not necessarily be queried using

This column is completely independent of the order table shown EXPLAIN output. This means that some key possible_keys in fact can not be generated by using the order table.
If the column is NULL, no relevant index. In this case, you can see by examining the WHERE clause if it refers to some column or columns for the index to improve the performance of your query. If so, create an appropriate index and check the query again with EXPLAIN

 

Six, Key

The key column display key (index) MySQL actually decided to use

If you do not select an index, the key is NULL. To force MySQL to use or ignore possible_keys column index, use FORCE INDEX in the query, USE INDEX or IGNORE INDEX.

 

七, key_len

Indicates the number of bytes used in the index, the index can be used in the query is calculated by the column length (the maximum possible length values ​​of the index fields displayed key_len, not actual length, i.e., is calculated on the basis key_len table definition, not retrieved by the table)

Without loss of accuracy, a length as short as possible 

 

Eight, ref

Connecting the matching conditions are shown in Table, i.e. constant, or column which is used to find the value of the index column

 

Nine, rows

 According to represent MySQL table statistics and index selection, the estimated number of lines needed to find records to be read

 

Ten, Extra

This column contains detailed information to solve the MySQL query, the following situations:

Using where: when tabular data is read and not only from the actual use of the information in the index action returns, which occurred in the part of all requests on the table columns are the same index, expressed mysql server filtered and then in storage engine to retrieve rows

Using temporary: MySQL expressed the need to use temporary tables to store the result set, common in the sorting and grouping queries

Using filesort: MySQL can not use indexes to complete the sorting operation is called "Sort files"

Using join buffer: used to change the value of the index is not stressed while obtaining a connection condition, and the need to connect a buffer to store intermediate results. If this value is present, it should be noted, you may need to add an index based on the specific circumstances of the query can be improved.

Impossible where: This value is highlighted where a statement would cause no qualifying rows.

Aggregate function may only result in the return line by using only the index value means the optimizer: Select tables optimized away

 

 

Summary:
• EXPLAIN will not tell you about triggers, stored procedures or user-defined functions from prejudice to a query
• EXPLAIN not consider Cache
• EXPLAIN does not show MySQL optimization work done in the execution of the query
• Some statistics are estimates, not exact values
• EXPALIN can only be explained SELECT operations, other operations to be rewritten as a SELECT after viewing the execution plan.

 

Published 171 original articles · won praise 46 · views 190 000 +

Guess you like

Origin blog.csdn.net/LHDZ_BJ/article/details/89533314