MySQL----SQL performance analysis


SQL Performance Analysis

1 SQL execution frequency

After the MySQL client connects successfully, pass

 show [session|global] status

-- session 是查看当前会话 ;
-- global 是查询全局数据 ;

Commands can provide server status information.

You can view the access frequency of INSERT, UPDATE, DELETE, and SELECT of the current database by using the following commands:

show global status like 'com_______';

Please add image description

Com_delete: delete times
Com_insert: insert times
Com_select: query times
Com_update: update times

Through the above commands, we can check whether the current database is mainly based on query, or whether it is mainly based on additions and deletions, so as to provide a reference for database optimization.

If it is based on additions and deletions, we can consider optimizing without indexing.
If it is mainly based on query, then it is necessary to consider optimizing the index of the database.

2 Slow query log

The slow query log records all SQL statements whose execution time exceeds the specified parameter (long_query_time, unit: seconds, default 10 seconds).

2.1 Check whether the slow log is enabled

MySQL's slow query log is not enabled by default, we can check the system variable slow_query_log.

show variables like 'slow_query_log';

Please add image description
Enable the MySQL slow log query switch:

set global slow_query_log = 1;
show variables like 'slow_query_log';

Please add image description

2.2 The time to query the slow log

Default is 10s

show variables like 'long_query_time';

Please add image description

2.3 View the information recorded in the slow log file

Find the my.ini file and open it (under the mysql installation directory folder)
Please add image description
to find the directory corresponding to datadir, and the log file is placed in this directory.
Please add image description

3 profile details

show profiles can help us understand where time is spent when doing SQL optimization. Through the have_profiling parameter, you can see whether the current MySQL supports profile operations:

3.1 Query whether profile is supported

SELECT @@have_profiling ;

Please add image description

3.2 Querying whether the profile is enabled

select @@profiling;

Please add image description

3.3 Open profile

Profiling can be enabled at the session/global level via the set statement:

set @@profiling=1;
select @@profiling;

Please add image description
The switch has been turned on. Next, the SQL statements we execute will be recorded by MySQL, and the execution time will be recorded.

3.4 View the basic time-consuming situation of each SQL

show profiles;

Please add image description

3.5 View the time-consuming situation of each stage of the SQL statement with the specified query_id

show profile for query query_id;
show profile for query 107;

Please add image description

3.6 View the CPU usage of the SQL statement with the specified query_id

show profile cpu for query query_id;
show profile cpu for query 150;

Please add image description

4 explain

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

4.1 Syntax

Add the keywords explain/desc directly before the select statement

EXPLAIN SELECT 字段列表 FROM 表名 WHERE 条件 ;
explain select d.* from dept d where id=1;

Please add image description

4.2 Explain the meaning of each field in the execution plan

field meaning
id The serial number of the select query, indicating the order in which the select clause or operation table is executed in the query (the same id, the execution order is from top to bottom; the id is different, the larger the value is, the earlier it is executed).
select_type Indicates the type of SELECT. Common values ​​include SIMPLE (simple table, that is, no table join or sub-query), 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 bad are NULL, system, const, eq_ref, ref, range, index, all.
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. The shorter the length, the better without losing accuracy.
rows The number of rows that MySQL considers necessary to execute the query, in the tables of the innodb engine, is an estimate and may not always be accurate.
filtered Indicates the percentage of the number of rows returned in the result to the number of rows to be read. The larger the value of filtered, the better.
link type illustrate
system The table has only one row, the MyISAM engine.
const Constant connection, the table has only one row matching at most, it is generally used for primary key or unique index comparison
eq_ref Each time a row is merged with the previous table, only one row is read in the table. This is the best one except system and const. It is characterized by the use of = , and all parts of the index participate in the join and the index is the primary key or non- Indexes on Null Unique Keys
ref If only a few rows are matched at a time, it is a better one, use = or <=>, can be a left covering index or a non-primary key or a non-unique key
range range of constant values
index Index tree scan. a. When the query is covered by the index, that is, when all data can be obtained from the index tree (Using Index in Extra); b. Full table scan to find data rows from the index in index order (no Using Index); c .If the Using Index and Using Where in Extra appear at the same time, it means to use the index to find the key value; d. If it appears alone, the read index is used instead of the read row, but it is not used for search
all full table scan

Guess you like

Origin blog.csdn.net/m0_53022813/article/details/124372805