The EXPLAIN command is the primary way to see how the query optimizer decides to execute a query. This feature has limitations and doesn't always tell the truth.
To call EXPLAIN, you only need to add the EXPLAIN command before the SELECT keyword in the query SQL.
Columns in EXPLAIN
Column | Meaning |
id | Identifies the row to which the SELECT belongs. The larger the id is, the priority is executed, and the same id is executed from top to bottom. |
select_type | Query types: divided into simple and complex types; complex types are divided into three categories: simple subqueries, derived tables (subqueries in the FROM clause), and UNION queries. |
table | This column shows which table is being accessed. |
type | Access type, MYSQL decides how to find rows in the table. |
possible_keys | index of possible choices. |
key | The actual index used. |
key_len | The number of bytes used in the index. |
ref | Which column or constant to use with the index key column in the query. |
rows | MYSQL estimates the number of rows to read in order to find the desired row, and by multiplying the values of all the rows columns together, you can roughly estimate the number of rows the entire query will check. |
Extra | Extra information that doesn't fit in other columns. |
select_type列
value | Meaning |
SIMPLE | Simple query, excluding subqueries and UNIONs. |
PRIMARY | If the query has any complex subsections, the outermost section is marked PRIMARY. |
UNION | The second statement or subsequent SELECT in a UNION is marked as UNION. |
DEPENDENT UNION | The second statement or the following statement in the UNION, independent of the outer query. |
UNIONRESULT | result of UNION. |
SUBQUERY | A subquery SELECT contained in the SELECT list is marked SUBQUERY. |
DEPENDENT | SELECT relies on data found in the outer query. |
DEPENDENT SUBQUERY | The first SELECT in the subquery, independent of the outer query. |
DERIVED | SELECT contained in a subquery of the FROM clause. |
MATERIALIZED | Materialized subqueries. |
UNCACHEABLESUBQUERY | Subqueries whose result sets cannot be cached must be re-evaluated for each row of the outer query. |
UNCACHEABLEUNION | The second or subsequent statements in the UNION are non-cacheable subqueries. |
type column
value | Meaning |
All | Full table scan. |
index | Like a full table scan, except that MYSQL scans the table in index order instead of row. |
range | Retrieve rows within a given range using an index. A limited index scan, starting at a point in the index, returns rows matching this range. |
ref | An index access that occurs only when using a non-unique index or a non-unique prefix of a unique index. |
ref_or_null | A variant on ref, MYSQL must perform a second lookup on the results of the first lookup to find NULL entries. |
eq_ref | Using this kind of index search, at most only one eligible record is returned, which is only seen when MYSQL uses the primary key or unique index. |
const | The table has at most one matching row, which is read at the start of the query. Because there is only one value, the optimizer treats the column value as a constant. |
system | The table has only one row of data, which is a special case of the const connection type. |