Using the EXPLAIN keyword can simulate the optimizer's execution of SQL query statements, so as to know how MySQL processes your SQL statements. Analyze the performance bottlenecks of your query statements or table structures.
➤ Through EXPLAIN, we can analyze the following results:
➤ Used as follows:
EXPLAIN SQL statement
EXPLAIN SELECT * FROM t1
Information contained in the execution plan
The sequence number of the select query, including a set of numbers, indicating the order in which select clauses or operation tables are executed in the query
There are 3 cases for the result of id
#The id is the same, the execution order is from top to bottom
[Summary] The order of loading the table is as shown in the table column above: t1 t3 t2
id is different. If it is a subquery, the sequence number of the id will increase, and the larger the id value is. The higher the priority, the sooner it will be executed
## As shown in the figure, when the id is 1, the table displays
2.2 select_typeCommon and commonly used values are as follows:
SIMPLE
Simple select query
If
in a PRIMARY query contains any complex
(derivative), and MySQL will execute it recursively These subqueries put the
results in the temporary table
UNION If the second SELECT appears after UNION, it is marked as UNION: If UNION is included in In the subquery of the FROM clause, the outer SELECT will be marked as: DERIVED
UNION RESULT SELECT
###### that obtains the result from the UNION table 2.3 table###### refers to the currently executed table######2.4 type######type shows which type is used in the query. The types included in type include as shown in the figure below Several types: #########From best to worst are: ###system > const > eq_ref > ref > range > index > all
system
The table has only one row of records (equal to the system table). This is a special column of const type. It does not usually appear and this can be ignored. const
means that it is found through the index once, and const is used to compare the primary key or unique index. Because only one row of data is matched, it is very fast. If you place the primary key in the where list, MySQL can convert the query into a constant. eq_ref
Unique index scan, for each index key, only one record in the table matches it. Commonly seen in primary key or unique index scansref
Non-unique index scans return all rows that match a single value. This is essentially an index access. It returns all rows that match a certain value. For single value rows, however, it may find multiple matching rows, so it should be a mix of find and scan. range
Retrieve only rows in a given range, use an index to select rows, the key column shows which index is used, usually in your where statement For queries such as between, , in, etc., this range scan index is better than a full table scan, because it only needs to start at a certain point in the index and end at another point, without scanning the entire index. index
Full Index Scan, the difference between Index and All is that the index type only traverses the index tree. This is usually faster than ALL because index files are usually smaller than data files. (That is to say, although all and Index both read the entire table, index is read from the index, and all is read from the hard disk) all
Full Table Scan will traverse the entire table to find matching rows possible_keys
Displays one or more indexes that may be applied to this table. If an index exists on the field involved in the query, the index will be listed, but may not be actually used by the query.
key
covering index is used in the query
(the field to be queried after selecting is exactly the same as the created index field) the same), the index only appears in the key list represents the number of bytes used in the index, This column can be used to calculate the length of the index used in the query. The shorter the length, the better without losing accuracy. The value displayed by key_len is the maximum possible length of the index field, not the actual length used. That is, key_len is calculated based on the table definition, not retrieved from the table.
使用了用临时表保存中间结果,MySQL在对查询结果排序时使用临时表。常见于排序order by和分组查询group by。
表示相应的select操作中使用了覆盖索引(Covering Index),避免访问了表的数据行,效率不错。如果同时出现using where,表明索引被用来执行索引键值的查找;如果没有同时出现using where,表明索引用来读取数据而非执行查找动作。
表明使用了where过滤
表明使用了连接缓存,比如说在查询的时候,多表join的次数非常多,那么将配置文件中的缓冲区的join buffer调大一些。
where子句的值总是false
,不能用来获取任何元组
SELECT * FROM t_user WHERE id = '1' and id = '2'
在没有GROUPBY子句的情况下,基于索引优化MIN/MAX操作或者对于MyISAM存储引擎优化COUNT(*)操作,不必等到执行阶段再进行计算,查询执行计划生成的阶段即完成优化。
优化distinct操作,在找到第一匹配的元组后即停止找同样值的动作
<derived3></derived3>
,表示查询结果来自一个衍生表,其中derived3中的3代表该查询衍生自第三个select查询,即id为3的select。【select d1.name …】执行顺序5:代表从UNION的临时表中读取行的阶段,table列的表示用第一个和第四个select的结果进行UNION操作。【两个结果union操作】
推荐学习:mysql教程
The above is the detailed content of explain usage and result analysis in MySQL (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!