MySQL Explain (Execution Plan), mysqlexplain
Today, database operations are increasingly becoming the performance bottleneck of the entire application, especially for Web applications. Concerning the database performance, this is not just something that DBAs need to worry about, but it is something that our programmers need to pay attention. When designing the database table structure and operating the database (especially the SQL statements used in table queries), we need to pay attention to the performance of data operations. Here, we will not talk about the optimization of many SQL statements, but only for the database with the most Web application MySQL.
1. explain (view Execution Plan): You can use the Explain keyword to simulate the optimizer to execute SQL query statements to know how MySql processes your SQL statements, analyze the performance bottleneck of your query statement or table structure.
(1) What can I do?
(2) how to play
1. Explain + SQL statement
2. Information contained in the execution plan
2. Glossary
(1) id
I. First case
Ii. Second case
Iii. Third case
(2). select_type
1. What are
Simple: simple select query, which does not include subqueries or union
Primary: If a query contains any complicated sub-parts, the outermost query is marked
SubQuery: Includes subqueries in the select or where list.
Derived: subqueries included in the from list are marked as derived. Mysql recursively executes these subqueries and places the results in the temporary table.
Union: If the second select statement appears after union, it is marked as union. If union is included in the subquery of the from clause, the outer select is marked as derived.
Union result: select the result obtained from the union table.
The type of query. It is mainly used to distinguish between common queries, joint queries, subqueries, and other complex queries.
(3). type: Access Data type
I. What values are there?
From Best to worst:
System> const> eq_ref> ref> range> index> All
Generally, it is better to ensure that the query reaches the range level and the ref level.
| Type |
Description |
| All |
Worst case: full table scan from start to end |
| Index |
Same as full table scan. Only the table is scanned in the index order, not the row. The main advantage is that sorting is avoided, but the overhead is still very large. If the Using index is displayed in the Extra column, it indicates that the index is being overwritten and only the index data is scanned. This is much lower than the full table scan in the index order. |
| Range |
Range scan: a limited index scan. The key column shows the index used. When using the =, <>,> =, <, <=, is null, <=>, BETWEEN, or IN operator, you can use the range |
| Ref |
An index that returns all rows that match a single value. This type of index access occurs only when a non-unique index or a unique index prefix is used. |
| Eq_ref |
Only one matching record is returned. When you use a unique index or primary key for search, it will occur (efficient) |
| Const/system |
When the primary key is placed in the where clause, mysql converts the query into a constant (efficient) |
| Null |
This means that mysql can break down the query statement in the optimization stage, and even cannot access the table or index (efficient) during the execution stage) |
(4) possible_key
I. It is displayed that one or more indexes may be applied to this table. If an index exists on the fields involved in the query, the index will be listed, but not necessarily used by the query.
(5) key
1. Actually used index. If it is null, no index is used
2. If the overwrite index is used in the query, the index only appears in the key list.
(6) key_len
1. indicates the number of bytes used in the index. You can use this column to calculate the length of the Index Used in the query. The shorter the length, the better without losing precision.
2. The value displayed by key_len is the maximum possible length of the index field, rather than the actual length. That is, key_len is calculated based on the table definition and is not retrieved from the table.
(7) ref
1. The column that shows the index is used. If used, it is a constant, and those columns or constants are used to find the value of the index column.
(8) rows
1. estimate the number of rows to be read Based on the table statistics and index selection.
(9). Extra
Additional information. Such as using index, using filesort, and using where.