ps : Emphasize that rows is the core indicator, and most statements with small rows must be executed quickly. So optimization statements are basically optimizing rows
1.explain what is
explain is abbreviated to view the execution plan, and the explain keyword can be used to simulate the optimizer to execute SQL query statements, so as to know how MySQL processes SQL statements
2.explain how to use
Syntax: explain + [SQL语句]
3. Explain the role
Table reading order
Operation type of data read operation
Which indexes can be used
Which indexes are actually used
References between tables
How many rows in each table are queried by the optimizer
4. Field information contained in the execution plan table
explainselect*from t01;
5. Description of the planning table fields
id field
Field
Description
id
The sequence number of the query in the execution plan selected by MySQL Query Optimizer. Indicates the order in which the select clause or operation table is executed in the query. The larger the id value, the higher the priority, and the earlier it will be executed; if the id is the same, the execution order is from top to bottom
select_type field
select_type query type
Description
SIMPLE
Simple select query, without union and subquery
PRIMARY
The outermost select query
UNION
The second or subsequent select query in UNION does not depend on the result set of the external query
DEPENDENT UNION
The second or subsequent select query in UNION depends on the result set of the external query
SUBQUERY
The first select query in the subquery does not depend on the result set of the external query
DEPENDENT SUBQUERY
The first select query in the subquery depends on the result set of the external query
DERIVED
Used when there is a subquery in the from clause. MySQL will execute these subqueries recursively and put the results in a temporary table
UNCACHEABLE SUBQUERY
Subqueries whose result set cannot be cached must be re-evaluated for each row of the outer query
UNCACHEABLE UNION
The second or subsequent select query in UNION is a non-cacheable subquery
table field
Field
Description
table
The table referenced by the output row
type field
Very important items, showing the type of connection used, sorted from the best to the worst type
type: connection type
Description
system
The table has only one row (=system table). This is a special case of const connection type
const
const is used when comparing PRIMARY KEY with a constant value. When the query table has only one row, use System
eq_ref
const is used when comparing PRIMARY KEY with a constant value. When the query table has only one row, use System
ref
The connection cannot select a single row based on keywords, and it may find multiple rows that meet the conditions. It is called ref because the index is compared with a reference value. This reference value is either a constant or the result value from a multi-table query in a table
ref_or_null
Like ref, but MySQL must find the null entry in the result of the first search, and then perform a second search.
index_merge
Explain that index merge optimization is used
unique_subquery
Use this type in some IN queries instead of the regular ref: value IN (SELECT primary_key FROM single_table WHERE some_expr)
index_subquery
This type is used in some IN queries, similar to unique_subquery, but the query is a non-unique index: value IN (SELECT key_column FROM single_table WHERE some_expr)
range
Only retrieve rows in a given range, use an index to select rows. The key column shows which index is used. When using =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN or IN operators, when comparing key columns with constants, you can use range
index
A full table scan is only performed in the index order instead of rows when scanning the table. The main advantage is to avoid sorting, but the overhead is still very large
all
Worst case, full table scan from beginning to end
possible_keys field
Field
Description
possible_keys
Indicate which indexes MySQL can use on this table to facilitate queries. If it is empty, there is no index available
key field
Field
Description
key
MySQL actually chooses the index to use from possible_key. If it is NULL, the index is not used. In rare cases, MYSQL will choose an under-optimized index. In this case, you can use USE INDEX (indexname) in the SELECT statement to force the use of an index or IGNORE INDEX (indexname) to force MYSQL to ignore the index
key_len field
Field
Description
key_len
The length of the index used. Without loss of accuracy, the shorter the length, the better.
ref field
Field
Description
ref
Show which column of the index is used
rows field
Field
Description
rows
MYSQL thinks it must check the number of rows used to return the requested data
Extra field
extra item
Description
Using filesort
Means that MySQL will use an external index to sort the results, instead of reading the relevant content in the index order from the table. It may be sorted in memory or on disk. Sorting operations that cannot be done using indexes in MySQL are called "file sorting"
Using temporary
Indicates that MySQL uses temporary tables when sorting query results. Commonly used in sorting order by and grouping query group by
6. Example
explain select *from t01 where id=100000;
explain select *from t01 where id>10000andid<20000;
explain select *from t01 where id>20000;