EXPLAIN
Output Columns
This section
describes the output columns produced by EXPLAIN. Later
sections provide additional information about the type and Extra columns.
Each output
row from EXPLAIN provides
information about one table. Each row contains the values summarized in Table 8.1,
“EXPLAIN Output Columns”, and described in more detail following the
table.
Table 8.1 EXPLAIN
Output Columns
Column
|
Meaning
|
The SELECT identifier
|
|
The SELECT type
|
|
The table
for the output row
|
|
The
matching partitions
|
|
The join
type
|
|
The
possible indexes to choose
|
|
The index
actually chosen
|
|
The length
of the chosen key
|
|
The columns
compared to the index
|
|
Estimate of
rows to be examined
|
|
Percentage
of rows filtered by table condition
|
|
Additional
information
|
• id
The SELECT identifier.
This is the sequential number of the SELECT within the
query. The value can be NULL if the row refers to the union result of other rows. In this case, the table column shows a value like <unionM,N> to indicate that the row refers
to the union of the rows with id values of M and N.
•
select_type Value
|
•
Meaning
|
• SIMPLE
|
|
• PRIMARY
|
• Outermost SELECT
|
• DEPENDENT UNION
|
|
• UNION RESULT
|
• Result of a UNION.
|
• SUBQUERY
|
• First SELECT in subquery
|
• DEPENDENT SUBQUERY
|
• First SELECT in
subquery, dependent on outer query
|
• DERIVED
|
|
• UNCACHEABLE SUBQUERY
|
• A subquery for which the result cannot be
cached and must be re-evaluated for each row of the outer query
|
• UNCACHEABLE UNION
|
• The second or later select in a UNION that belongs
to an uncacheable subquery (see UNCACHEABLE
SUBQUERY)
|
•
DEPENDENT typically signifies the use of a
correlated subquery. See Section 13.2.10.7,
“Correlated Subqueries”.
DEPENDENT SUBQUERY evaluation differs from UNCACHEABLE SUBQUERY
evaluation. For DEPENDENT SUBQUERY, the subquery is re-evaluated only once
for each set of different values of the variables from its outer context. For
UNCACHEABLE SUBQUERY, the subquery is re-evaluated for each row of the outer context.
Cacheability of subqueries differs from caching of query results in the
query cache (which is described in Section 8.10.3.1,
“How the Query Cache Operates”). Subquery caching occurs during
query execution, whereas the query cache is used to store results only after
query execution finishes.
|
• table
The name of
the table to which the row of output refers. This can also be one of the
following values:
◦
<unionM,N>: The row refers to the union of the rows
with id values of M and N.
◦
<derivedN>: The row refers to the derived
table result for the row with an id value of N. A derived
table may result, for example, from a subquery in the FROM clause.
•
• partitions
The partitions from which records would be matched by the query. This
column is displayed only if the PARTITIONS keyword is used. The value is NULL for nonpartitioned tables. See Section 19.3.4,
“Obtaining Information About Partitions”.
• possible_keys
The possible_keys column indicates which indexes MySQL can choose from use
to find the rows in this table. Note that this column is totally independent of
the order of the tables as displayed in the output from EXPLAIN. That means
that some of the keys in possible_keys might not be usable in practice with the generated table
order.
If this column is NULL, there are no relevant indexes. In this
case, you may be able to improve the performance of your query by examining the
WHERE
clause to check whether it refers to some column or columns that would be
suitable for indexing. If so, create an appropriate index and check the query
with EXPLAIN again. See Section 13.1.7,
“ALTER TABLE Syntax”.
To see what
indexes a table has, use SHOW INDEX FROM tbl_name.
• key
The key column indicates the key (index)
that MySQL actually decided to use. If MySQL decides to use one of the possible_keys indexes to
look up rows, that index is listed as the key value.
It is possible that key will name an index that is not present in the possible_keys value. This
can happen if none of the possible_keys indexes are suitable for looking up rows,
but all the columns selected by the query are columns of some other index. That
is, the named index covers the selected columns, so although it is not used to
determine which rows to retrieve, an index scan is more efficient than a data
row scan.
For InnoDB, a secondary index might cover the
selected columns even if the query also selects the primary key because InnoDB stores the
primary key value with each secondary index. If key is NULL, MySQL found no index to use for executing
the query more efficiently.
To force MySQL to use or ignore an
index listed in the possible_keys column, use FORCE INDEX, USE INDEX, or IGNORE INDEX in your query. See Section 8.9.3,
“Index Hints”.
For MyISAM and NDB tables, running ANALYZE TABLE helps
the optimizer choose better indexes. For NDB tables, this also improves performance of
distributed pushed-down joins. For MyISAM tables, myisamchk --analyze
does the same as ANALYZE TABLE. See Section 7.6,
“MyISAM Table Maintenance and Crash Recovery”.
• key_len
The key_len column indicates the length of the key that MySQL decided to use. The
length is NULL if the key column says NULL. Note that the value of key_len enables you to determine how many parts of
a multiple-part key MySQL actually uses.
• ref
The ref column shows which columns or
constants are compared to the index named in the key column to select rows from the table.
• rows
The rows column indicates the number of
rows MySQL believes it must examine to execute the query.
For InnoDB tables, this
number is an estimate, and may not always be exact.
• filtered
The filtered column indicates an estimated percentage of table rows that will be
filtered by the table condition. That is, rows shows the estimated number of rows
examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables. This
column is displayed if you use EXPLAIN EXTENDED.
• Extra
This column
contains additional information about how MySQL resolves the query. For
descriptions of the different values, see EXPLAIN Extra Information.
EXPLAIN
Join Types
The type column of EXPLAIN output
describes how tables are joined. The following list describes the join types,
ordered from the best type to the worst:
• system
The table has
only one row (= system table). This is a special case of the const join type.
•
const
The table has
at most one matching row, which is read at the start of the query. Because
there is only one row, values from the column in this row can be regarded as
constants by the rest of the optimizer. const tables are
very fast because they are read only once.
const is used when
you compare all parts of a PRIMARY KEY or UNIQUE index to constant values. In the following
queries, tbl_name can be used as a const table:
SELECT * FROM tbl_name WHERE primary_key=1;
•
•
SELECT * FROM tbl_name
•
WHERE primary_key_part1=1 AND primary_key_part2=2;
•
•
eq_ref
One row is
read from this table for each combination of rows from the previous tables.
Other than the system and const types, this is
the best possible join type. It is used when all parts of an index are used by
the join and the index is a PRIMARY KEY or UNIQUE NOT NULL index.
eq_ref can be used
for indexed columns that are compared using the = operator. The comparison value can be a
constant or an expression that uses columns from tables that are read before
this table. In the following examples, MySQL can use an eq_ref join to
process ref_table:
SELECT * FROM ref_table,other_table
•
WHERE ref_table.key_column=other_table.column;
•
•
SELECT * FROM ref_table,other_table
•
WHERE ref_table.key_column_part1=other_table.column
•
AND
ref_table.key_column_part2=1;
•
•
ref
All rows with
matching index values are read from this table for each combination of rows
from the previous tables. ref is used if the
join uses only a leftmost prefix of the key or if the key is not a PRIMARY KEY or UNIQUE index (in
other words, if the join cannot select a single row based on the key value). If
the key that is used matches only a few rows, this is a good join type.
ref can be used for
indexed columns that are compared using the = or <=> operator. In the following examples, MySQL
can use a ref join to process ref_table:
SELECT * FROM ref_table WHERE key_column=expr;
•
•
SELECT * FROM ref_table,other_table
•
WHERE ref_table.key_column=other_table.column;
•
•
SELECT * FROM ref_table,other_table
•
WHERE ref_table.key_column_part1=other_table.column
•
AND
ref_table.key_column_part2=1;
•
•
ref_or_null
This join
type is like ref, but with the
addition that MySQL does an extra search for rows that contain NULL values. This join type
optimization is used most often in resolving subqueries. In the following
examples, MySQL can use a ref_or_null join to
process ref_table:
SELECT * FROM ref_table
•
WHERE key_column=expr OR key_column IS NULL;
• index_merge
This join
type indicates that the Index Merge optimization is used. In this case, the key column in the output row contains
a list of indexes used, and key_len contains a list of the longest key parts
for the indexes used. For more information, see Section 8.2.1.4,
“Index Merge Optimization”.
•
unique_subquery
This type
replaces eq_ref for some IN subqueries of the following form:
value IN (SELECT primary_key FROM single_table WHERE some_expr)
•
unique_subquery is just an index lookup function that replaces the
subquery completely for better efficiency.
•
index_subquery
This join
type is similar to unique_subquery. It
replaces IN subqueries, but it works for nonunique indexes in subqueries of the
following form:
value IN (SELECT key_column FROM single_table WHERE some_expr)
•
•
range
Only rows
that are in a given range are retrieved, using an index to select the rows. The
key column in the
output row indicates which index is used. The key_len contains the longest key part that was
used. The ref column is NULL for this type.
range can be used
when a key column is compared to a constant using any of the =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, or IN() operators:
SELECT * FROM tbl_name
•
WHERE key_column = 10;
•
•
SELECT * FROM tbl_name
•
WHERE key_column BETWEEN 10 and 20;
•
•
SELECT * FROM tbl_name
•
WHERE key_column IN (10,20,30);
•
•
SELECT * FROM tbl_name
•
WHERE key_part1 = 10 AND key_part2 IN (10,20,30);
•
• index
The index join type is the same as ALL, except that the
index tree is scanned. This occurs two ways:
◦
If the index
is a covering index for the queries and can be used to satisfy all data
required from the table, only the index tree is scanned. In this case, the Extra column says Using index. An
index-only scan usually is faster than ALL because the size
of the index usually is smaller than the table data.
◦
A full table
scan is performed using reads from the index to look up data rows in index
order. Uses index does not appear in the Extra column.
•
MySQL can use this join type when the query uses only columns that are
part of a single index.
• ALL
A full table
scan is done for each combination of rows from the previous tables. This is
normally not good if the table is the first table not marked const, and usually very bad in all other cases. Normally, you can avoid ALL by adding
indexes that enable row retrieval from the table based on constant values or
column values from earlier tables.
EXPLAIN
Extra Information
The Extra column of EXPLAIN output
contains additional information about how MySQL resolves the query. The
following list explains the values that can appear in this column. If you want
to make your queries as fast as possible, look out for Extra values of Using filesort and Using temporary.
• Child of 'table' pushed join@1
This table is
referenced as the child of table in a join that can be pushed down to the
NDB kernel. Applies only in MySQL Cluster NDB 7.2 and later, when pushed-down
joins are enabled. See the description of the ndb_join_pushdown
server system variable for more information and examples.
• const row not found
For a query such as SELECT ... FROM tbl_name, the table was empty.
• Distinct
MySQL is looking for distinct values, so it stops searching for more rows
for the current row combination after it has found the first matching row.
• Full scan on NULL key
This occurs for subquery optimization as a fallback strategy when the
optimizer cannot use an index-lookup access method.
• Impossible HAVING
The HAVING clause is always false and cannot select any rows.
• Impossible WHERE
The WHERE clause is always false and cannot select any rows.
• Impossible WHERE noticed after reading const tables
MySQL has
read all const (and system) tables and
notice that the WHERE clause is always false.
• No matching min/max row
No row satisfies the condition for a query such as SELECT MIN(...) FROM ... WHERE condition.
• no matching row in const table
For a query with a join, there was an empty table or a
table with no rows satisfying a unique index condition.
• No tables used
The query has no FROM clause, or has a FROM DUAL clause.
•
Not exists
MySQL was able to do a LEFT JOIN optimization
on the query and does not examine more rows in this table for the previous row
combination after it finds one row that matches the LEFT JOIN criteria.
Here is an example of the type of query that can be optimized this way:
SELECT * FROM t1
LEFT JOIN t2 ON t1.id=t2.id
•
WHERE t2.id IS NULL;
•
Assume that t2.id is defined as NOT NULL. In this
case, MySQL scans t1 and looks up the rows in t2 using the values of t1.id. If MySQL finds a matching row in
t2, it knows
that t2.id can
never be NULL, and does not scan through the rest of the rows in t2 that have the same id value. In other words, for each
row in t1, MySQL needs to do only a single lookup in t2, regardless of how many rows actually
match in t2.
• Range checked for each record (index map: N)
MySQL found
no good index to use, but found that some of indexes might be used after column
values from preceding tables are known. For each row combination in the
preceding tables, MySQL checks whether it is possible to use a range or index_merge access
method to retrieve rows. This is not very fast, but is faster than performing a
join with no index at all. The applicability criteria are as described in Section 8.2.1.3,
“Range Optimization”, and Section 8.2.1.4,
“Index Merge Optimization”, with the exception that all column
values for the preceding table are known and considered to be constants.
Indexes are numbered beginning with 1, in the same order as shown by SHOW INDEX for the
table. The index map value N is a bitmask value that indicates which
indexes are candidates. For example, a value of 0x19 (binary 11001) means that indexes 1, 4,
and 5 will be considered.
• Scanned N databases
This
indicates how many directory scans the server performs when processing a query
for INFORMATION_SCHEMA tables, as described in Section 8.2.4,
“Optimizing INFORMATION_SCHEMA Queries”. The value of N can be 0, 1,
or all.
•
Select tables optimized away
The optimizer
determined 1) that at most one row should be returned, and 2) that to produce
this row, a deterministic set of rows must be read. When the rows to be read
can be read during the optimization phase (for example, by reading index rows),
there is no need to read any tables during query execution.
The first condition is fulfilled when the query is implicitly grouped
(contains an aggregate function but no GROUP BY clause). The second condition is fulfilled
when one row lookup is performed per index used. The number of indexes read
determines the number of rows to read.
Consider the
following implicitly grouped query:
SELECT MIN(c1), MIN(c2) FROM t1;
•
Suppose that MIN(c1) can be retrieved by reading one index row
and MIN(c2) can
be retrieved by reading one row from a different index. That is, for each
column c1 and c2, there exists an index where the column is the first column of the index.
In this case, one row is returned, produced by reading two deterministic rows.
This Extra value does not occur if the rows to read are not deterministic. Consider
this query:
SELECT MIN(c2) FROM t1 WHERE c1 <= 10;
•
Suppose that (c1, c2) is a covering index. Using this index, all
rows with c1 <= 10 must be scanned to find the minimum c2 value. By contrast, consider this
query:
SELECT
MIN(c2) FROM t1 WHERE c1 = 10;
•
In this case,
the first index row with c1 = 10 contains the minimum c2 value. Only one row must be read to
produce the returned row.
For storage engines that maintain
an exact row count per table (such as MyISAM, but not InnoDB), this Extra value can occur for COUNT(*) queries for
which the WHERE clause is missing or always true and there is no GROUP BY clause.
(This is an instance of an implicitly grouped query where the storage engine
influences whether a deterministic number of rows can be read.)
• Skip_open_table, Open_frm_only, Open_trigger_only, Open_full_table
These values indicate file-opening optimizations that apply to queries for
INFORMATION_SCHEMA tables, as described in Section 8.2.4,
“Optimizing INFORMATION_SCHEMA Queries”.
◦
Skip_open_table: Table files do not need to be opened. The
information has already become available within the query by scanning the
database directory.
◦
Open_frm_only: Only the table's .frm file need be opened.
◦
Open_trigger_only: Only the table's .TRG file need be opened.
◦
Open_full_table: The unoptimized information lookup. The .frm, .MYD, and .MYI files must be opened.
•
• unique row not found
For a query such as SELECT ... FROM tbl_name, no rows satisfy the condition for a UNIQUE index or PRIMARY KEY on the
table.
• Using filesort
MySQL must do an extra pass to find out how to retrieve the rows in sorted
order. The sort is done by going through all rows according to the join type
and storing the sort key and pointer to the row for all rows that match the WHERE clause. The keys then are sorted
and the rows are retrieved in sorted order. See Section 8.2.1.11,
“ORDER BY Optimization”.
• Using index
The column information is retrieved from the table using only information
in the index tree without having to do an additional seek to read the actual
row. This strategy can be used when the query uses only columns that are part
of a single index.
For InnoDB tables that have a user-defined clustered
index, that index can be used even when Using index is absent from the Extra column. This is the case if type is index and key is PRIMARY.
• Using index for group-by
Similar to the Using index table access method, Using index for
group-by indicates that MySQL found an
index that can be used to retrieve all columns of a GROUP BY or DISTINCT query
without any extra disk access to the actual table. Additionally, the index is
used in the most efficient way so that for each group, only a few index entries
are read. For details, see Section 8.2.1.12,
“GROUP BY Optimization”.
• Using join buffer
Tables from earlier joins are read in portions into the join buffer, and
then their rows are used from the buffer to perform the join with the current
table.
• Using sort_union(...), Using union(...), Using intersect(...)
These indicate how index scans are merged for the index_merge join
type. See Section 8.2.1.4,
“Index Merge Optimization”.
• Using temporary
To resolve the query, MySQL needs to create a temporary table to hold the
result. This typically happens if the query contains GROUP BY and ORDER BY clauses that
list columns differently.
• Using where
A WHERE clause is used to restrict which rows to match against the next table or
send to the client. Unless you specifically intend to fetch or examine all rows
from the table, you may have something wrong in your query if the Extra value is not Using where and the
table join type is ALL or index. Even if you
are using an index for all parts of a WHERE clause, you may see Using where if the
column can be NULL.
• Using where with pushed condition
This item applies to NDB tables only. It means that MySQL Cluster is using the Condition Pushdown optimization
to improve the efficiency of a direct comparison between a nonindexed column
and a constant. In such cases, the condition is “pushed down” to the cluster's
data nodes and is evaluated on all data nodes simultaneously. This eliminates
the need to send nonmatching rows over the network, and can speed up such
queries by a factor of 5 to 10 times over cases where Condition Pushdown could
be but is not used. For more information, see Section 8.2.1.5,
“Engine Condition Pushdown Optimization”.
EXPLAIN
Output Interpretation
You can get a
good indication of how good a join is by taking the product of the values in
the rows
column of the EXPLAIN output. This
should tell you roughly how many rows MySQL must examine to execute the query.
If you restrict queries with the max_join_size system
variable, this row product also is used to determine which multiple-table SELECT statements to
execute and which to abort. See Section 8.12.2,
“Tuning Server Parameters”.
The following
example shows how a multiple-table join can be optimized progressively based on
the information provided by EXPLAIN.
Suppose that
you have the SELECT statement
shown here and that you plan to examine it using EXPLAIN:
EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,
tt.ProjectReference,
tt.EstimatedShipDate,
tt.ActualShipDate, tt.ClientID,
tt.ServiceCodes,
tt.RepetitiveID,
tt.CurrentProcess,
tt.CurrentDPPerson,
tt.RecordVolume, tt.DPPrinted,
et.COUNTRY,
et_1.COUNTRY, do.CUSTNAME
FROM tt, et, et AS et_1, do
WHERE tt.SubmitTime IS NULL
AND tt.ActualPC = et.EMPLOYID
AND tt.AssignedPC = et_1.EMPLOYID
AND tt.ClientID = do.CUSTNMBR;
For this
example, make the following assumptions:
• The columns being compared have been
declared as follows.
•
Table
|
•
Column
|
•
Data Type
|
• tt
|
• ActualPC
|
• CHAR(10)
|
• tt
|
• AssignedPC
|
• CHAR(10)
|
• tt
|
• ClientID
|
• CHAR(10)
|
• et
|
• EMPLOYID
|
• CHAR(15)
|
• do
|
• CUSTNMBR
|
• CHAR(15)
|
•
• The tables have the following indexes.
•
Table
|
•
Index
|
• tt
|
• ActualPC
|
• tt
|
• AssignedPC
|
• tt
|
• ClientID
|
• et
|
• EMPLOYID (primary
key)
|
• do
|
• CUSTNMBR (primary
key)
|
•
• The tt.ActualPC values are not evenly distributed.
Initially,
before any optimizations have been performed, the EXPLAIN statement
produces the following information:
table type possible_keys key key_len ref
rows Extra
et
ALL PRIMARY NULL NULL NULL 74
do
ALL PRIMARY NULL NULL NULL 2135
et_1
ALL PRIMARY NULL NULL NULL 74
tt
ALL AssignedPC, NULL NULL
NULL 3872
ClientID,
ActualPC
Range checked for each record (index map: 0x23)
Because type is ALL for each table,
this output indicates that MySQL is generating a Cartesian product of all the
tables; that is, every combination of rows. This takes quite a long time,
because the product of the number of rows in each table must be examined. For
the case at hand, this product is 74 × 2135 × 74 × 3872 = 45,268,558,720 rows.
If the tables were bigger, you can only imagine how long it would take.
One problem
here is that MySQL can use indexes on columns more efficiently if they are
declared as the same type and size. In this context, VARCHAR and CHAR are considered
the same if they are declared as the same size. tt.ActualPC is declared as CHAR(10) and et.EMPLOYID is CHAR(15), so there is
a length mismatch.
To fix this
disparity between column lengths, use ALTER TABLE to
lengthen ActualPC from 10 characters to 15 characters:
mysql> ALTER TABLE tt MODIFY
ActualPC VARCHAR(15);
Now tt.ActualPC and et.EMPLOYID are both VARCHAR(15). Executing
the EXPLAIN statement
again produces this result:
table type
possible_keys key key_len
ref rows Extra
tt
ALL AssignedPC, NULL
NULL NULL 3872
Using
ClientID, where
ActualPC
do
ALL PRIMARY NULL
NULL NULL 2135
Range checked for each record (index map: 0x1)
et_1
ALL PRIMARY NULL
NULL NULL 74
Range checked for each record (index map: 0x1)
et
eq_ref PRIMARY PRIMARY
15 tt.ActualPC 1
This is not
perfect, but is much better: The product of the rows values is less by a factor of 74. This
version executes in a couple of seconds.
A second
alteration can be made to eliminate the column length mismatches for the tt.AssignedPC = et_1.EMPLOYID and tt.ClientID = do.CUSTNMBR comparisons:
mysql> ALTER TABLE tt MODIFY
AssignedPC VARCHAR(15),
-> MODIFY
ClientID VARCHAR(15);
After that
modification, EXPLAIN produces the
output shown here:
table type
possible_keys key key_len
ref rows Extra
et
ALL PRIMARY NULL
NULL NULL 74
tt
ref AssignedPC, ActualPC 15 et.EMPLOYID 52
Using
ClientID, where
ActualPC
et_1
eq_ref PRIMARY PRIMARY 15
tt.AssignedPC 1
do
eq_ref PRIMARY PRIMARY 15
tt.ClientID 1
At this
point, the query is optimized almost as well as possible. The remaining problem
is that, by default, MySQL assumes that values in the tt.ActualPC column are
evenly distributed, and that is not the case for the tt table. Fortunately, it is easy to
tell MySQL to analyze the key distribution:
mysql> ANALYZE TABLE tt;
With the
additional index information, the join is perfect and EXPLAIN produces
this result:
table type
possible_keys key key_len
ref rows Extra
tt
ALL AssignedPC NULL
NULL NULL 3872 Using
ClientID, where
ActualPC
et
eq_ref PRIMARY PRIMARY
15 tt.ActualPC 1
et_1
eq_ref PRIMARY PRIMARY
15 tt.AssignedPC 1
do
eq_ref PRIMARY PRIMARY
15 tt.ClientID 1
The rows column in the output from EXPLAIN is an
educated guess from the MySQL join optimizer. Check whether the numbers are
even close to the truth by comparing the rows product with the actual number of rows
that the query returns. If the numbers are quite different, you might get
better performance by using STRAIGHT_JOIN in your SELECT statement and
trying to list the tables in a different order in the FROM clause.
It is possible
in some cases to execute statements that modify data when EXPLAIN SELECT is
used with a subquery; for more information, see Section 13.2.10.8,
“Subqueries in the FROM Clause”.
Visual Explain Diagram Information
|
System Name
|
Color
|
Text on Visual Diagram
|
Tooltip related information
|
|
SYSTEM
|
Blue
|
Single row: system constant
|
Very low cost
|
|
CONST
|
Blue
|
Single row: constant
|
Very low cost
|
|
EQ_REF
|
Green
|
Unique Key Lookup
|
Low cost -- The optimizer is able to find an index that it can use to retrieve the required records. It is fast because the index search directly leads to the page with all the row data
|
|
REF
|
Green
|
Non-Unique Key Lookup
|
Low-medium -- Low if the number of matching rows is small; higher as the number of rows increases
|
|
FULLTEXT
|
Yellow
|
Fulltext Index Search
|
Specialized FULLTEXT search. Low -- for this specialized search requirement
|
|
REF_OR_NULL
|
Green
|
Key Lookup + Fetch NULL Values
|
Low-medium -- if the number of matching rows is small; higher as the number of rows increases
|
|
INDEX_MERGE
|
Green
|
Index Merge
|
Medium -- look for a better index selection in the query to improve performance
|
|
UNIQUE_SUBQUERY
|
Orange
|
Unique Key Lookup into table of subquery
|
Low -- Used for efficient Subquery processing
|
|
INDEX_SUBQUERY
|
Orange
|
Non-Unique Key Lookup into table of subquery
|
Low -- Used for efficient Subquery processing
|
|
RANGE
|
Orange
|
Index Range Scan
|
Medium -- partial index scan
|
|
INDEX
|
Red
|
Full Index Scan
|
High -- especially for large indexes
|
|
ALL
|
Red
|
Full Table Scan
|
Very High -- very costly for large tables, but less of an impact for small ones. No usable indexes were found for the table, which forces the optimizer to search every row. This could also mean that the search range is so broad that the index would be useless.
|
|
UNKNOWN
|
Black
|
unknown
|
Note: This is the default, in case a match cannot be determined
|
|
System Name
|
Color
|
Text on Visual Diagram
|
Tooltip related information
|
- id – a sequential identifier for each SELECT within the query (for when you have nested subqueries)
- select_type – the type of SELECT query. Possible values are:
- SIMPLE – the query is a simple SELECT query without any subqueries or UNIONs
- PRIMARY – the SELECT is in the outermost query in a JOIN
- DERIVED – the SELECT is part of a subquery within a FROM clause
- SUBQUERY – the first SELECT in a subquery
- DEPENDENT SUBQUERY – a subquery which is dependent upon on outer query
- UNCACHEABLE SUBQUERY – a subquery which is not cacheable (there are certain conditions for a query to be cacheable)
- UNION – the SELECT is the second or later statement of a UNION
- DEPENDENT UNION – the second or later SELECT of a UNION is dependent on an outer query
- UNION RESULT – the SELECT is a result of a UNION
- table – the table referred to by the row
- type – how MySQL joins the tables used. This is one of the most insightful fields in the output because it can indicate missing indexes or how the query is written should be reconsidered. Possible values are:
- system – the table has only zero or one row
- const – the table has only one matching row which is indexed. This is the fastest type of join because the table only has to be read once and the column’s value can be treated as a constant when joining other tables.
- eq_ref – all parts of an index are used by the join and the index is PRIMARY KEY or UNIQUE NOT NULL. This is the next best possible join type.
- ref – all of the matching rows of an indexed column are read for each combination of rows from the previous table. This type of join appears for indexed columns compared using = or <=> operators.
- fulltext – the join uses the table’s FULLTEXT index.
- ref_or_null – this is the same as ref but also contains rows with a null value for the column.
- index_merge – the join uses a list of indexes to produce the result set. The key column of EXPLAIN‘s output will contain the keys used.
- unique_subquery – an IN subquery returns only one result from the table and makes use of the primary key.
- index_subquery – the same as unique_subquery but returns more than one result row.
- range – an index is used to find matching rows in a specific range, typically when the key column is compared to a constant using operators like BETWEEN, IN, >, >=, etc.
- index – the entire index tree is scanned to find matching rows.
- all – the entire table is scanned to find matching rows for the join. This is the worst join type and usually indicates the lack of appropriate indexes on the table.
- possible_keys – shows the keys that can be used by MySQL to find rows from the table, though they may or may not be used in practice. In fact, this column can often help in optimizing queries since if the column is NULL, it indicates no relevant indexes could be found.
- key – indicates the actual index used by MySQL. This column may contain an index that is not listed in the possible_key column. MySQL optimizer always look for an optimal key that can be used for the query. While joining many tables, it may figure out some other keys which is not listed in possible_key but are more optimal.
- key_len – indicates the length of the index the Query Optimizer chose to use. For example, a key_len value of 4 means it requires memory to store four characters. Check out MySQL’s data type storage requirements to know more about this.
- ref – Shows the columns or constants that are compared to the index named in the key column. MySQL will either pick a constant value to be compared or a column itself based on the query execution plan. You can see this in the example given below.
- rows – lists the number of records that were examined to produce the output. This Is another important column worth focusing on optimizing queries, especially for queries that use JOIN and subqueries.
- Extra – contains additional information regarding the query execution plan. Values such as “Using temporary”, “Using filesort”, etc. in this column may indicate a troublesome query. For a complete list of possible values and their meaning, check out the MySQL documentation.
If you look at the above result, you can see all of the symptoms of a bad query. But even if I wrote a better query, the results would still be the same since there are no indexes. The join type is shown as “ALL” (which is the worst), which means MySQL was unable to identify any keys that can be used in the join and hence the possible_keys and key columns are null. Most importantly, the rows column shows MySQL scans all of the records of each table for query. That means for executing the query, it will scans 7 × 110 × 122 × 326 × 2996 = 91,750,822,240 records to find the four matching results. That’s really horrible, and it will only increase exponentially as the database grows.
Now lets add some obvious indexes, such as primary keys for each table, and execute the query once again. As a general rule of thumb, you can look at the columns used in the JOIN clauses of the query as good candidates for keys because MySQL will always scan those columns to find matching records.
댓글 없음:
댓글 쓰기