PrefaceIn daily work, we sometimes run slow queries to record some SQL statements that take a long time to execute. Finding these SQL statements does not mean that the job is done. Sometimes we often use the explain command to view the execution plan of these SQL statements to see whether the SQL statement uses an index and whether a full table scan is performed. This can be viewed through the explain command. So we have a deep understanding of MySQL's cost-based optimizer, and we can also get a lot of details about the access strategies that may be considered by the optimizer, and which strategy is expected to be adopted by the optimizer when running SQL statements. (QEP: sql generates an execution plan query execution plan) 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) 1. Introduction to EXPLAINUsing the EXPLAIN keyword can simulate the optimizer to execute SQL query statements, so as to know how MySQL processes your SQL statements. Analyze the performance bottleneck of your query statement or table structure. Through explain we can get the following information:
Usage: explain + sql statement. The fields included are as follows 2. Meaning of each field in the execution plan2.1 id The id is the same, and the execution order is from top to bottom The id is different. If it is a subquery, the id number will increase. The larger the id value, the higher the priority and the earlier it will be executed. If the ids are the same or different and exist at the same time, the same id can be considered as a group. The execution is from top to bottom in the same group, and the group with the largest id will be executed first. 2.4 type Type shows which type is used in the query. The types included in type include the following: system > const > eq_ref > ref > range > index > all
2.5 possible_keys and key possible_keys shows the possible indexes that may be applied to this table, one or more. If an index exists on the fields involved in the query, the index will be listed, but it may not be actually used by the query. The index actually used by the key. If it is NULL, no index is used. (Possible reasons include no index or index failure) 2.6 key_len Indicates 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. 2.7 rows Based on table statistics and index selection, roughly estimate the number of rows needed to find the required records, that is, the fewer the better. 2.8 Extra 2.8.1 Using filesort This means that MySQL will use an external index to sort the data instead of reading it in the order of the indexes in the table. Sorting operations in MySQL that cannot be performed using indexes are called "file sorts". 2.8.2 Using temporary A temporary table is used to store intermediate results. MySQL uses a temporary table when sorting query results. Commonly used in order by sorting and group by query. 2.8.3 Using index Indicates that a covering index is used in the corresponding select operation, avoiding access to the table's data rows, which is efficient. If using where appears at the same time, it means that the index is used to perform index key value lookups; if using where does not appear at the same time, it means that the index is used to read data rather than perform lookups. 2.8.4 Using join buffer Indicates that the connection cache is used. For example, when querying, the number of multi-table joins is very large, so increase the join buffer in the configuration file. SummarizeThis is the end of this article about the in-depth analysis of MySQL explain usage and results. For more relevant MySQL explain usage and results content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML Tutorial: HTML horizontal line segment
>>: Implementation of running springboot project with Docker
Preface Recently, due to work needs, I need to in...
Today I encountered the MySQL service 1067 error ...
Table of contents What is a Promise? Usage of rej...
Recently, I encountered a problem in the process ...
After installing Jenkins, the initial download of...
What is Virtual Memory? First, I will directly qu...
Dark background style page design is very popular...
Formation of the grid system In 1692, the newly c...
For various reasons, sometimes you need to modify...
Table of contents background Problem Analysis 1. ...
<br />When thoughts were divided into East a...
Table of contents Preface 1. What is phantom read...
This article uses examples to illustrate the impa...
I recently helped someone with a project and the ...
In the previous blog, Xiao Xiong updated the meth...