MYSQL explain execution plan

  • 2020-05-07 20:35:33
  • OfStack

To use the method, add explain to the select statement:
For example: explain select * from test1
Explanation of EXPLAIN column:
table: shows which table this row is about
type: this is the important column that shows what type of connection is used. The connection types from best to worst are const, eq_reg, ref, range, indexhe, and ALL
possible_keys: shows the indexes that might be applied to this table. If null, there is no possible index. You can select an appropriate statement from the WHERE statement for the relevant field
key: the actual index used. If NULL, no index is used. Very rarely, MYSQL will choose an index that is not sufficiently optimized. In this case, you can use USE INDEX (indexname) in SELECT statements to force 1 index or IGNORE INDEX (indexname) to force MYSQL to ignore indexes
key_len: length of the index used. The shorter the length, the better, without losing accuracy
ref: shows which 1 column of the index is used and, if possible, 1 constant
rows: the number of rows that MYSQL considers necessary to return the requested data
Extra: additional information about how MYSQL parses queries. This will be discussed in table 4.3, but the bad examples you can see here are Using temporary and Using filesort, meaning MYSQL cannot use indexes at all, resulting in slow retrieval
The meaning of the description returned by the extra column
Once Distinct has found a row that matches the row associated with it, it is no longer searched
Not exists: MYSQL optimizes LEFT JOIN, once it finds a line matching LEFT JOIN, it stops searching
Range checked for each Record (index map:#) : no ideal index was found, so for every combination of rows from the previous table, MYSQL checks which index is used and USES it to return rows from the table. This is 1 of the slowest joins using the index
Using filesort: when you see this, the query needs to be optimized. MYSQL requires additional steps to find out how to sort the returned rows. It sorts all rows by connection type and by row pointer to all rows that store the sort key value and match the condition
Using index: column data is returned from a table that only USES the information in the index without reading the actual action, which occurs when all the requested columns of the table are part of the same index
When Using temporary sees this, the query needs to be optimized. Here, MYSQL needs to create a temporary table to store the results, which usually happens on ORDER BY for different column sets, not GROUP BY
Where used USES WHERE clauses to restrict which rows match the next table or are returned to the user. This can happen if you don't want to return all the rows in the table and the join types are ALL or index, or if the query has a problem with the interpretation of different join types (sorted in order of efficiency)
The system table has only one row: the system table. This is a special case of the const connection type
const: the maximum value of one record in the table matches this query (the index can be primary key or 1 only). Because there's only one row, this value is actually a constant, because MYSQL reads this value and treats it as a constant
eq_ref: in joins, MYSQL reads one record from the table for each record in the previous table when the query USES all of the primary or only keys of the index
ref: this join type occurs only if the query USES a key that is not a unique or primary key, or parts of these types (for example, using the leftmost prefix). For each row union of the previous table, all records are read from the table. This type depends heavily on how many records are matched against the index-the less the better
range: this join type returns rows in a range using an index, such as using > or < What happens when you look up something
index: this join type does a full scan for each record in the previous table (better than ALL because index 1 is smaller than table data)
ALL: this connection type does a full scan for every previous record associated with it, which is generally bad and should be avoided

Related articles: