MySQL query optimization (EXPLAIN)
Query optimizer
Query bottleneck
- CPU saturation (read data to memory or persist data to hard disk)
- IO saturation (read data is much larger than the memory capacity)
- Hardware constraints (top, free, iostat, vmstat and other system performance status)
Execution plan (EXPLAIN)
Use the EXPLAIN keyword to simulate the query optimizer to execute the SQL query, so as to know how the MySQL database processes the SQL statement, and from which aspect the SQL statement should be optimized
effect
- View the reading order of the table
- Data read operation type
- Which indexes can be used
- Which indexes are actually used
- Reference relationship between tables
- How many rows of data are queried by the optimizer
Execution plan field introduction
structure
id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---|
Explanation
1.id
The sequence number of the select query, including a set of numbers, indicates the order in which the select clause or operation table is executed in the query. There are three cases for this value
id相同:执行顺序由上至下
id不同:值越大优先级越高,越先被执行
说明:id相同为同组,从上往下顺序执行,id不同,则值越大,优先级越高,越先执行
2. select_type
Query type, used to distinguish complex queries such as ordinary queries, joint queries, and sub-queries. There are 6 values in total
Field | Description |
---|---|
SIMPLE | Simple query, no subquery or UNION |
PRIMARY | The main query, the query contains sub-queries, the outermost query is marked as PRIMARY |
SUBQUERY | Subquery |
DERIVED | The sub-queries contained in the FORM will be marked as DERIVED (derived) + ID, MySQL will recursively execute these sub-queries, and save the result set in a temporary table |
UNION | Joint inquiry. If the second SELECT appears in UNION, it will be marked as UNION. If UNION is included in the FROM subquery, the outer SELECT will be marked as DERIVED |
UNION RESULT | SELECT to get results from UNION table |
3. table
slightly
4. type
Field | Description |
---|---|
system | The table has only one record (system table), this is a special column of const type, usually does not appear, can be ignored |
const | Indicates that the query is successful once through the index, and const is used to compare the primary key or the unique index. Because it only matches one row of indexes, it is fast. For example, if the primary key is placed in the where condition, the MySQL database can convert the query to a constant |
eq_ref | Unique index scan, for each index, only one record in the table matches, common in primary key or unique index scan |
ref | A non-unique index scan returns all rows that match a single value. Essentially it is also an index access, returning all rows of matching values, it will find multiple pieces of data that meet the conditions, which is a mixture of search and scan |
range | Only retrieve rows in a given range, use the index to select rows, and the key column shows which index is used, which usually appears in queries such as between, <, >, in. This range scan index is better than a full table scan. It starts at one point of the index and ends at another point instead of scanning the entire index. |
index | Full table index scan. The difference between index and all is that the index type only traverses the index tree, which is usually faster than all, because the index file is usually smaller than the data file (that is, both index and all read the entire table, but the index is read from the index , And all reads from the hard disk) |
all | Full table scan. Traverse the entire table data to find matching rows |
5. possible-keys
Displays one or more indexes that may be applied. If there is an index on the field of the query statement where condition, the index will be listed, but it may not be actually used in the query.
6. key
The index actually used. If it is null, the index is not used. If a compound index is used in the query, the index only appears in the key list.
7. key_len
Indicates the number of bytes used in the index. Without loss of accuracy, the shorter the length, the better. It represents the maximum possible length of the index field, not the actual length used. It is calculated according to the table definition, and is not obtained by searching in the table.
8. ref
The column that shows the index is used, if possible, is a constant, which means to find the value on the index column.
9. rows
According to table statistics and index selection, roughly estimate the number of rows that need to be read to find the required record. In theory, the less the better
10. ExTra
Contains important additional information that is not suitable for display in other columns.
Field value | Description |
---|---|
Using filesort (Important, needs optimization) |
Sort within files. MySQL will sort the data using an external index instead of reading it in the order of the index in the table. The sorting operation in MySQL that cannot be done with the index is called "file sorting" (this will happen if the order by field is not indexed, and the speed will be very slow) |
Using temporary (Important, needs optimization) |
Use a temporary table to save intermediate results, MySQL saves query results in a temporary table, which greatly reduces system performance, which is common in order by and group by |
Using index (important) |
This operation uses a covering index to avoid accessing data rows. If using where appears at the same time, it means that the index is used for searching (with a where statement). If using where does not appear, it means that the index is used to read data instead of searching ( No where statement) |
using where | Use where conditions to filter |
impossible where | The value of the where clause is false, and nothing can be found (id=3 and id=5) |
select tables optimized away(了解) | In the absence of a group by clause, optimize the MIN/MAX operation based on the index or optimize the COUNT(*) operation for the ISAM storage engine for M. You do not need to wait until the execution stage to calculate, and the query execution plan generation stage is optimized |
distinct | Optimize distinct operation, stop searching after finding the first matching data |
Covering index
One is index coverage.
- The select data column can be obtained from the index, without having to read the data row, MySQL can use the index to return the fields in the select list, and it will not hesitate to read the data file again according to the index. In short, the query column is to be built Index coverage.