5 minutes to see the SQL execution plan
I. Overview
If you want to optimize the SQL statement, then the SQL execution plan must be known, because only through the SQL execution plan, you can know how the SQL performs the query, and whether to use the index, what index to use, and whether to perform the query. information about how to sort and how to sort.
At the application level, it is actually added in front of the SQL statement Explain
. I have covered the use of this in the previous article, but I have not elaborated. Today, we will specifically talk about the meaning of the information output by this Explain.
Second, the column of the Explain output
List | significance |
---|---|
id | the SELECT identifier |
select_type | the SELECT type |
table | table of output rows |
partitions | matching partition |
type | join type |
possible_keys | possible indices to choose from |
key | actually selected index |
key_len | the length of the selected key |
ref | Column to compare with index |
rows | An estimate of the row to check |
filtered | Percentage of rows filtered by table condition |
Extra | extra information |
Example
Here is an example for everyone to understand:
In the second column select_type
, here SIMPLE
represents a simple query, that is to say, no UNION or subquery is used
The fourth column is type
very important, you can use this to judge the execution speed of SQL and whether to use an index and what index to use
The fifth column, as the possible_keys
name suggests, is the key that can be selected for this query
The sixth column key
is the actual index used, in this example the PRIMARY
primary key index is used
The seventh column key_len
is the length of the key used
The eighth column ref
indicates how the column compared with the index uses the index (because multiple indexes may be used in the where condition of a query)
3. Indexes used according to type analysis
type
The most common values for a column are as follows:
-
const
Use
PRIMARY KEY
orUNIQUE
Index , in layman's terms, is to use a unique index (a single row of data is obtained) , which is the fastest -
ref
const
It is slower than using a unique index (there may be multiple rows of data obtained) , such as only using the leftmost prefix key of the key or a normal indexexample:
You can see through row
this column that 2 pieces of data were queried
-
ref or null
Similar to ref, the difference is that MySQL will additionally search for rows with NULL values
example:
IS NULL
This condition is also added when using ordinary indexes
Note: This field of ordinary index is not set when designing the table structureIS NOT NULL
-
index
This means that the secondary index is only traversed, that is, there is no return table query, which is what the previous article said.
索引覆盖
Note: If a full table scan is performed using a secondary index
-
range
This is very simple, in fact, it is a range query
-
all
In fact, there is nothing to say about this. In fact, it is a full table scan, which is the slowest one.
4. Summary
-
range
This is very simple, in fact, it is a range query
-
all
In fact, there is nothing to say about this. In fact, it is a full table scan, which is the slowest one.
4. Summary
In fact, for us to optimize SQL, especially the query statement, the core is to use the index as much as possible, and to scan the full table as little as possible. In order to know whether the SQL uses the index, we need to check the execution plan, so This article will pay special attention type
to the content of the column output. By Explain
observing the SQL execution plan, you can design an index that suits your business needs.