Mysql uses Explain to view the execution plan - optimize slow sql

Official address:
MySQL :: MySQL 5.7 Reference Manual :: 8.8.2 EXPLAIN Output Format

Explain is used in mysql to view the execution plan of sql. The programmer optimizes the sql statement according to the execution result. Add indexes or rewrite sql statements.

EXPLAIN SELECT * FROM example_table

The execution results are as follows:
insert image description here

  • select_type

    The most common query type is SIMPLE, which means that our query has no subqueries and UNION queries are not used. It
    indicates the type of query, and the values ​​​​used are as follows

    select_type describe
    SIMPLE Indicates that the query statement does not contain subqueries or UNION
    PRIMARY Indicates that this query is the outermost query
    UNION Identifies that this query is the second or subsequent query of a UNION
    DEPENDENT UNION The second or subsequent query statement in the UNION, using the results of the outer query
    UNION RESULT Result of UNION
    SUBQUERY SELECT subquery statement
    DEPENDENT SUBQUERY Outer query result from SELECT subquery statement
  • type

    Indicates the method used by the storage engine to query data. A more important attribute, through which you can determine whether the query is a full table scan or an index-based partial scan. The commonly used attribute values ​​are as follows, and the efficiency increases sequentially from top to bottom.

    type describe
    ALL Indicates a full table scan with the worst performance
    index Represents an index-based full table scan, first scan the index before the full table scan
    range Indicates the use of indexed range queries. Use >, >=, <, <=, in, etc.
    ref Represents a single value query using a non-unique index
    eq_ref In general, there is a multi-table join query, which means that each record in the previous table can only match one row of results in the latter table.
    const Indicates that the primary key or unique index is used for equivalent query and constant query.
    NULL Indicates that there is no need to access the table, and the speed is the fastest
  • rows

    The MySQL query optimizer will estimate how many rows of records SQL needs to scan to find the results based on statistical information. In principle,
    the fewer rows, the higher the efficiency, and you can intuitively understand the SQL efficiency.

  • possible-keys

    Indicates the index that can be used when querying. Note that it is not necessarily used in practice, and the index name is displayed

  • key

    Indicates the index actually used when querying, and the index name is displayed

  • Extra

    Extra indicates a lot of extra information. Various operations will prompt related information in Extra. The common types are as follows:

    Extra describe
    Using where Indicates that the query needs to query data through the return table
    Using index Indicates that the query needs to pass the index, and the index can satisfy the required data
    Using filesort Indicates that the queried calculations need to be sorted additionally, sort in memory if the data volume is small, and sort on disk if the data volume is large. Therefore, Using filesort suggests optimization
    Using temprorary The query uses a temporary table, which generally occurs in operations such as deduplication and grouping
  • Indexes can improve query speed and affect where queries and order by sorting. MySQL index types are as follows:

    • Divided from the index storage structure: B Tree index, Hash index, FULLTEXT full-text index, R Tree index
    • Divided from the application level: ordinary index, unique index, primary key index, composite index
    • Divided from the index key value type: primary key index, auxiliary index (secondary index)
    • From data storage and index key-value logic division: clustered index (clustered index), non-clustered index (non-clustered index)

Guess you like

Origin blog.csdn.net/u013795102/article/details/117355437
Recommended