[MySQL database | Part 20] explain execution plan

Table of contents

 Foreword:

explain: 

grammar:

Summarize:


 Foreword:

        In the previous article, we introduced three tools for analyzing the execution efficiency of MySQL statements from a time perspective: SQL execution frequency, slow log query, and profile. However, these three methods only roughly check the efficiency of SQL statements from the perspective of time. If we want to see the real performance of a statement, we need to use explain to check the pros and cons of SQL statements.

explain: 

In SQL, EXPLAIN obtains information about how MySQL executes a SELECT statement, including how tables are joined and the order in which they are joined during the execution of the SELECT statement.

The EXPLAIN command simulates the query execution process without executing the query itself, thereby explaining the execution plan of the query and the indexes used, which helps to check whether the query uses an effective index and which parts need to be optimized.

Specifically, EXPLAIN will generate a table that contains the execution plan corresponding to each part of the query statement, including query type, table scanning method, index usage, and so on. Each row in this table corresponds to a step in the query process, and each column describes that step or other relevant information about the query statement.

By using the EXPLAIN command, developers can better understand the use of the query optimizer, identify performance problems in queries, and try to fix them by tuning query statements, indexes, and so on.

grammar:

#直接在SELECT语句之前加上关键字explain
EXPLAIN SELECT 字段列表 FROM 表名 WHERE 条件;

We use this statement in the self-built table:

EXPLAIN SELECT * FROM emp WHERE age BETWEEN 18 AND 30;

You can see that the execution result is:

 Let's go through what each of these represents:

1. id : the serial number of the slesct query, indicating that the select clause or the sequence table operation is executed in the query (the id is the same, the execution order is from top to bottom, the id is different, the larger the value, the earlier the execution)

2. select_type :   This field is used to indicate the type of query executed by MySQL

  • SIMPLE: Simple query, does not contain UNION queries or subqueries, etc.
  • PRIMARY: Indicates the outermost query in the query statement.
  • DEPENDENT SUBQUERY: depends on the results in the outer query.
  • UNION: The second or subsequent SELECT statement in a UNION.
  • UNION RESULT: Selects rows from the results of a UNION query.
  • SUBQUERY: A subquery in the WHERE clause or HAVING clause.
  • DERIVED: A temporary table is derived for the table or subquery in the FROM clause, and the result is returned using this table.
  • MATERIALIZED: A derived temporary table already exists and the query needs to retrieve its results.

In the given execution plan, the value of `select_type` is `SIMPLE`, which means that the query is a simple query without using complex features such as UNION queries or subqueries.

3. type : Point out the name of the table involved in the query statement and the access method used.

All access methods:

  • system : A table with only one row in the system table (such as `dual`, etc.), which is a special case of the `const` type, and generally does not need to consider this access method.
  • const : Indicates that the query uses constants to match, and only one row of data meets the condition. This situation generally occurs in the case of queries using primary keys or unique indexes.
  • eq_ref : Use a unique index or primary key to query
  • ref : The query uses a non-unique index , and the returned result set will process a part of the index, which needs to be returned to the data table to match the data of the query condition.
  • range : Use a {@link https://dev.mysql.com/doc/refman/8.0/en/index-merge-optimization.html | Index Range Lookup}.
  • index : Indicates that the query will scan the entire index, and does not need to return to the data table for data query .
  • ALL : Indicates a full table scan, which is a relatively inefficient query method for large tables.

These access methods will affect the query efficiency of the MySQL database, so you should choose the appropriate query method flexibly according to the specific situation during development. For example, for large tables, indexes should be used for queries as much as possible, and types such as `ALL` should be avoided.

The performance of these types from high to low are: NULL, system, const, eq_ref, ref, range, index, all.

But in fact, we will not have the NULL access method in the query, because NULL does not mean that no table is used in this query. In practice, no matter how we optimize it, it is impossible to optimize NULL.

4. possible_keys : Indicates the indexes that can be used in this query.

5. key : The index actually used.

6. key_len : Indicates the length of the index used by MySQL.

7. ref : Indicates the association condition between the index and the table used by MySQL.

8. rows : Indicates the number of rows that MySQL scans when executing a query.

9. filtered : Indicates the ratio between the number of rows in the result set and the number of rows scanned.

10. Extra : Indicates additional execution plan details. Using where is used in this example, which means that the query uses the WHERE condition.

Here, because the above demonstration uses a single-table query, there is only one select statement, and the effect of the id cannot be seen, so we call a multi-table query here

  explain select e.*, d.name from emp e left join dept d on e.dept_id = d.id;

operation result:

 We can find here: id is not self-increasing, which is what we mentioned before

id is the serial number of the slesct query, indicating that the select clause or the sequence table operation is executed in the query (the id is the same, the execution order is from top to bottom, the id is different, the larger the value, the earlier the execution)

What if the id is different?

 explain  select * from emp where dept_id = (select id from dept where name = '销售部');

operation result:

 We can see that in this case, the id reflects the execution order. We can know that in this multi-table query, we first execute the select statement in the demp table, and then execute the select statement in the emp table.

Summarize:

This article introduces explain, a more practical efficiency viewing tool, and its various usages. You must master the pros and cons of the four statements we have introduced so far, so that you can play with MySQL optimization.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

 

 

Guess you like

Origin blog.csdn.net/fckbb/article/details/131353298