MySQL database - index (4) - SQL performance analysis - profile details, explain (profile view command, explain the meaning of each field in the execution plan)

Table of contents

SQL performance analysis

profile details

View instructions

explain

grammar

Explain the meaning of each field in the execution plan


SQL performance analysis (Part 2)

profile details

show profiles can help us understand where time is spent when optimizing SQL.

Through the have_profiling parameter, you can see whether the current MySQL supports profile operations:

SELECT @@have_profiling;

As you can see, MySQL currently supports profile operations, but the switch is turned off.

Profiling can be turned on at the session/global level through the set statement: 

SET profiling = 1;

After it is turned on, the SQL statements we execute will be recorded by MySQL, and the execution time will be recorded.

View instructions

-- 查看每一条SQL的耗时基本情况

show profiles;

-- 查看指定query_id的SQL语句各个阶段的耗时情况

show profile for query query_id;

-- 查看指定query_id的SQL语句CPU的使用情况

show profile cpu for query query_id;

explain

The EXPLAIN or DESC command obtains information about how MySQL executes the SELECT statement, including how tables are connected and the order in which they are connected during the execution of the SELECT statement.

grammar

-- 直接在select语句之前加上关键字 explain / desc

EXPLAIN SELECT 字段列表 FROM 表名 WHERE 条件 ;

Explain the meaning of each field in the execution plan

  • id

The sequence number of the select query, indicating the order in which select clauses or table operations are executed in the query.

(With the same ID, the execution order is from top to bottom; with different IDs, the larger the value, the earlier it is executed) .

  • select_type

Indicates the type of SELECT. Common values ​​include SIMPLE (simple table, that is, no table connection or subquery is used), PRIMARY (main query, that is, the outer query), UNION (the second or subsequent query statement in UNION) ), SUBQUERY (subquery is included after SELECT/WHERE), etc.

  • type

Indicates the connection type. The connection types from good to poor performance are NULL, system, const, eq_ref, ref, range, index, and all.

1. system : The table has only one row (=system table). This is a special case of the const join type.
2. const : The table has at most one matching row, which will be read at the beginning of the query. Because there is only one row, the column values ​​in this row can be treated as constants by the rest of the optimizer.
const is used when comparing all parts of a PRIMARY KEY or UNIQUE index with a constant value.
3. eq_ref : For each combination of rows from the previous table, read one row from the table. This is probably the best join type, besides const types. It is used when all parts of an index are used in the join and the index is UNIQUE or PRIMARY KEY. eq_ref can be used on indexed columns compared using the = operator. The comparison value can be a constant or an expression that uses a column from a table that was read before this table.
4. ref : For each combination of rows from the previous table, all rows with matching index values ​​will be read from this table. Use ref if the join uses only the leftmost prefix of the key, or if the key is not UNIQUE or PRIMARY KEY (in other words, if the join cannot select a single row based on the key). This join type is good if you are using keys that only match a small number of rows. ref can be used on indexed columns using the = or <=> operators.
5. range : Retrieve only rows in a given range, using an index to select rows. The key column shows which index was used. key_len contains the longest key element of the index used. The ref column is NULL in this type. When using =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN or IN operators to compare key columns with constants, you can use range 6
, index: This join type is the same as ALL, except that only the index tree is scanned. This is usually faster than ALL because index files are usually smaller than data files.
7. all : Perform a complete table scan for each combination of rows from the previous table. This is usually not good if the table is the first one not marked const, and is usually bad in that case. It is usually possible to add more indexes without using ALL so that rows can be retrieved based on constant values ​​or column values ​​in the previous table.

8. NULL : When no table is used in the query, the connection type is NULL.
————————————————

(The reproduced content has been modified)

Copyright statement: This article is an original article by CSDN blogger "FSW..." and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/FSWZYC/article/details/126655438

  •  possible_key

 Displays one or more indexes that may be applied to this table.

  • key

The actual index used. If NULL, no index is used.

  • key_len

Indicates the number of bytes used in the index. This value is the maximum possible length of the index field, not the actual length used. Without losing accuracy, the shorter the length, the better.

  • rows

The number of rows that MySQL considers necessary to execute the query in the innodb engine table is an estimate and may not always be accurate.

  • filtered

Indicates the number of rows returned as a percentage of the number of rows to be read. The larger the value of filtered, the better.

  • Extra

This column contains details of how MySQL resolved the query.


END


Learn from: Dark Horse Programmer - MySQL Database Course

Guess you like

Origin blog.csdn.net/li13437542099/article/details/133102189