MySQL Advanced Road (20) - 5 minutes to understand the SQL execution plan

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 SELECTidentifier
select_type the SELECTtype
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:

Please add image description

In the second column select_type, here SIMPLErepresents a simple query, that is to say, no UNION or subquery is used

The fourth column is typevery 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_keysname suggests, is the key that can be selected for this query

The sixth column keyis the actual index used, in this example the PRIMARYprimary key index is used

The seventh column key_lenis the length of the key used

The eighth column refindicates 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

typeThe most common values ​​for a column are as follows:

  • const

    Use PRIMARY KEYor UNIQUEIndex , in layman's terms, is to use a unique index (a single row of data is obtained) , which is the fastest

  • ref

    constIt 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 index

    example:

Please add image description

You can see through rowthis 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:

Please add image description

IS NULLThis 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.索引覆盖

Please add image description

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 typeto the content of the column output. By Explainobserving the SQL execution plan, you can design an index that suits your business needs.

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/121426403