Introduction
- Adding keywords before the sql statement
explain
will display the execution information of sql instead of the result of sql execution, as follows
EXPLAIN SELECT * FROM student JOIN score ON student.`id` = score.`student_id`;
returns as follows
- Let's introduce the meaning of the representatives in the query results one at a time
search result
id
- You can see the id as the order of execution. The larger the id, the higher the priority. When the id is the same, it will be executed from top to bottom
- If the id is empty, it will be executed sequentially from top to bottom
select_type
It is used to indicate whether a simple query or a complex query, and it has the following values
- simple: simple query, excluding subqueries and union queries, as follows
EXPLAIN SELECT * FROM student WHERE id = 3;
2. primary: the outermost layer of the complex query select
, the outermost query in SQL with subqueries is primary
3. union: the second select or the latter one of the union statement, the UNION RESULT is the merged result as follows
EXPLAIN SELECT * FROM student WHERE id = 3
UNION
SELECT * FROM student WHERE id = 4;
- subquery: Indicates the subquery in select, not in the from statement
EXPLAIN SELECT * FROM student WHERE id = (SELECT student_id FROM score WHERE id=2);
5. DERIVED: The select of the derived table, the subquery behind from,
EXPLAIN SELECT * FROM (
SELECT * FROM score WHERE id IN (1,3,5)
) t WHERE t.id =1;
Also appears as SIMPLE in some versions
table
- Indicates which table the data comes from
- Sometimes it is not the name of the real table, but a virtual table. The last digit of the virtual table is a number, which represents the query of the id
type
- Connection type, this is more difficult, and it is an important focus of our optimization, which directly reflects whether our SQL statement is efficient
- This field has many values, we mainly focus on these fields system, const, eq_ref, ref, range, index, all
- The order of performance from good to check is: system>const>eq_ref>ref>range>index>all (important)
system
There is only one row in the table. This is a special case of the const type, which usually does not appear, so this field can be ignored
const
- Indicates that the index is found once, const is used to compare the primary key or unique index, because only one row of data is matched, so it is very fast
primary key
It should be noted that the or index is used in the queryunique
, so if a row of data is queried, it is not necessarily const
EXPLAIN SELECT * FROM attend_schedule_shift WHERE id = 12;
id is generally the primary key, so the type of query here is const
eq_ref
Unique index scan, only one record in the table is preset to match. Generally, two tables are associated, and the field in the association condition is a primary key or a unique index
EXPLAIN SELECT * FROM attend_schedule_shift
JOIN attend_schedule_section
ON attend_schedule_shift.id = attend_schedule_section.`sche_shift_id`;
The primary key id used in the above sql statement attend_schedule_shift
is associated with other tables, so attend_schedule_shift
the query for this table is eq_ref
ref
- Non-unique index scan, returning all rows for a single value
- It is also an index access in essence. It returns all the rows that match a single value. It may find multiple eligible rows, so it should be a mixture of search and scan.
explain select * from time_attend_data where company_id = 420;
- Composite index
time_attend_data
There is a composite index composed of company_id and date - Only one index value is used
- This single value is
company_id = 420
range
Essentially also an index lookup, this index must be a separate index
- Retrieves the given range of rows, using an index to select rows, and the key column shows which index to use.
- In the general conditional query, queries such as >, <, in, and between appear, but it is not necessarily
explain select * from time_attend_data where `id` between 300 and 600;
index
Traverse the index tree. Usually faster than ALL because index files are usually smaller than data files. Both all and index read the entire table, but index is retrieved from the index, while all is retrieved from the hard disk.
EXPLAIN SELECT `company_id`,`date` FROM time_attend_data;
Generally, the index is directly followed by select
ALL
- full table scan
possible_keys
- Displays indexes that may be applied to this table, but not necessarily actually used by the query. If the class is NULL, then there is no associated index.
- You can improve performance by checking where yourself to see if you can add an appropriate index
key
- The actual index to use.
- If
possible_keys
there is a value, but the key is null, it may be that the data in the table is not much. Mysql thinks that it is not helpful for the current query and chooses the full table query. - If you want to force the use of or ignore
possible_keys
the index in mysql, then use when queryingforce index
,ignore index
key_len
- Indicates the number of bytes used in the index, which can be used to calculate the length of the index used in the query.
- Generally speaking, the longer the index length, the higher the precision and low efficiency; the shorter the index length, high efficiency, but low precision.
- The length of the index is not really used, it is an estimated value
The maximum length of the index is 768 bytes. When the length is too large, mysql will perform a process similar to the leftmost prefix, and extract the first half of the characters for indexing. When the field is null, 1 byte is needed to record.
Calculation Rules
- string
- char(n): n numbers or letters occupy n bytes, and Chinese characters occupy 3n bytes
- varchar(n): n numbers or letters occupy n bytes, and Chinese characters occupy 3n+2 bytes (+2 is used to store the string length)
- number type
- tinyint: 1 byte
- smallint: 2 bytes
- int: 4 bytes
- bigint: 8 bytes
- time type
- date: 3 bytes
- timestamp: 4 bytes
- datetime: 8 bytes
ref
Indicates which column is used, and the constant indicates that this column is equal to a certain constant.
rows
Roughly the number of rows that need to be read to find the desired record.
filter
Indicates the percentage of selected rows and read rows, 100 means 100% is selected, 80 means 80% is read
extra
Here are some, additional, more important information
Using filesort
- Using external index sorting instead of reading according to the index order in the table (generally needs to be optimized)
- The sorting that MYSQL cannot use the index to complete is called file sorting
Using temporary
Temporary tables are used to store intermediate results. Common in sorting order by and grouping query group by (best optimization)
using index
Indicates that in the select statement, the covering index is used, and the value is directly obtained from the index without reading data from the disk.
using where
where filter is used
using index condition
Newly added after 5.6, it means that the query column has an indexed column, and the index condition should be judged first