Mysql advanced - performance analysis tool (1)

Performance Analysis Tools(1)

1. Optimization steps of database server

The part of the letter S represents observation (the corresponding analysis tools will be used), and the part of the letter A represents action (corresponding to the actions that can be taken by the analysis).

Insert image description here

Insert image description here

2. View system performance parameters

In MySQL, you can use the SHOW STATUS statement to query the performance parameters and execution frequency of some MySQL database servers.
The syntax of the SHOW STATUS statement is as follows:

SHOW [GLOBAL|SESSION] STATUS LIKE '参数';

Some commonly used performance parameters are as follows:

• Connections: The number of times to connect to the MySQL server.

• Uptime: The online time of the MySQL server.

• Slow_queries: Number of slow queries.

• Innodb_rows_read: Number of rows returned by Select query

• Innodb_rows_inserted: Number of rows inserted by performing INSERT operation

• Innodb_rows_updated: Number of rows updated by UPDATE operation

• Innodb_rows_deleted: Number of rows deleted by DELETE operation

• Com_select: The number of query operations.

• Com_insert: Number of insert operations. For batch insert INSERT operations, only one accumulation is performed.

• Com_update: Number of update operations.

• Com_delete: Number of delete operations.

3. Statistics of SQL query cost: last_query_cost

CREATE TABLE `student_info` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `student_id` INT NOT NULL ,
    `name` VARCHAR(20) DEFAULT NULL,
    `course_id` INT NOT NULL ,
    `class_id` INT(11) DEFAULT NULL,
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

If we want to query the record with id=900001, and then look at the query cost, we can directly search on the clustered index:

SELECT student_id, class_id, NAME, create_time FROM student_info
WHERE id = 900001;

Running results (1 record, running time 0.02s)
Then look at the cost of the query optimizer. In fact, we only need to retrieve one page:

mysql> SHOW STATUS LIKE 'last_query_cost';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| Last_query_cost | 1.000000 |
+-----------------+----------+

What if we want to query student records with IDs between 900001 and 9000100?

SELECT student_id, class_id, NAME, create_time FROM student_info
WHERE id BETWEEN 900001 AND 900100;

Running results (100 records, running time is 0.046s):
Then look at the cost of the query optimizer. At this time we need to query about 20 pages.

mysql> SHOW STATUS LIKE 'last_query_cost';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| Last_query_cost | 21.134453 |
+-----------------+-----------+

You can see that the number of pages is 20 times that of before, but the efficiency of the query has not changed significantly. In fact, the time of the two SQL queries is basically the same, because the page is loaded in one go using sequential reading. into the buffer pool and then search again. Although the number of pages (last_query_cost) has increased a lot , the query time has not increased much through the buffer pool mechanism .

Usage scenarios: It is very useful for comparing costs, especially when we have several query methods to choose from.

4. Locate SQL that executes slowly: slow query log

4.1 Enable slow query log parameters

  1. Turn on slow_query_log
mysql > set global slow_query_log='ON';

Then let’s check whether the slow query log is turned on and the location of the slow query log file:

show VARIABLES like '%slow_query_log%'
  1. Modify long_query_time threshold

To check the time threshold setting of slow query, use the following command:

mysql > show variables like '%long_query_time%';

If we want to shorten the time, for example, set it to 1 second, we can set it like this:

#测试发现:设置global的方式对当前session的long_query_time失效。对新连接的客户端有效。所以可以一并
执行下述语句
mysql > set global long_query_time = 1;
mysql> show global variables like '%long_query_time%';
mysql> set long_query_time=1;
mysql> show variables like '%long_query_time%';

4.2 Check the number of slow queries

Query how many slow query records there are in the current system

SHOW GLOBAL STATUS LIKE '%Slow_queries%';

4.3 Slow query log analysis tool: mysqldumpslow

In a production environment, if you want to manually analyze logs, find and analyze SQL, it is obviously a laborious task. MySQL provides the log analysis tool
mysqldumpslow.
View the help information of mysqldumpslow

The specific parameters of the mysqldumpslow command are as follows:

-a: Do not abstract numbers into N, and abstract strings into S.
-s: Indicates how to sort:
c: Number of visits
l: Lock time
r: Return record
t: Query time
al: Average lock time
ar: Average Number of records returned
at: average query time (default mode)
ac: average number of queries
-t: that is, how many previous pieces of data are returned;

-g: followed by a regular matching pattern, case-insensitive;

Example: We want to sort by query time and view the first five SQL statements, so we can write:

mysqldumpslow -s t -t 5 /var/lib/mysql/atguigu01-slow.log

Common references for work:

#得到返回记录集最多的10个SQL
mysqldumpslow -s r -t 10 /var/lib/mysql/atguigu-slow.log
#得到访问次数最多的10个SQL
mysqldumpslow -s c -t 10 /var/lib/mysql/atguigu-slow.log
#得到按照时间排序的前10条里面含有左连接的查询语句
mysqldumpslow -s t -t 10 -g "left join" /var/lib/mysql/atguigu-slow.log
#另外建议在使用这些命令时结合 | 和more 使用 ,否则有可能出现爆屏情况
mysqldumpslow -s r -t 10 /var/lib/mysql/atguigu-slow.log | more

4.4 Close slow query log

There are two ways to stop the slow query log function of the MySQL server:

Method 1: Permanent method

[mysqld]
slow_query_log=OFF

Or, comment out or delete the slow_query_log item

[mysqld]
#slow_query_log =OFF

Restart the MySQL service and execute the following statement to query the slow log function.

SHOW VARIABLES LIKE '%slow%'; #查询慢查询日志所在目录
SHOW VARIABLES LIKE '%long_query_time%'; #查询超时时长

Method 2: Temporary method

Use the SET statement to set. (1) Stop the MySQL slow query log function. The specific SQL statements are as follows.

SET GLOBAL slow_query_log=off;

(2) Restart the MySQL service and use the SHOW statement to query the slow query log function information. The specific SQL statement is as follows

SHOW VARIABLES LIKE '%slow%';
#以及
SHOW VARIABLES LIKE '%long_query_time%';

4.5 Delete slow query logs

5. View SQL execution cost: SHOW PROFILE

mysql > show variables like 'profiling';

Turn on show profile by setting profiling='ON':

mysql > set profiling = 'ON';

Then execute the relevant query statements. Then to see what profiles the current session has, use the following command:

mysql > show profiles;

You can see that there are a total of 2 queries in the current session. If we want to see the cost of the most recent query, we can use:

mysql > show profile;
mysql> show profile cpu,block io for query 2;

Common query parameters for show profile:
① ALL: Display all overhead information. ② BLOCK IO: Displays block IO overhead. ③ CONTEXT SWITCHES: Context switching overhead
. ④ CPU: Displays CPU overhead information. ⑤ IPC: Displays sending and receiving overhead information. ⑥ MEMORY: Display memory overhead information
. ⑦ PAGE FAULTS: Display page error overhead information. ⑧ SOURCE: Displays
overhead information related to Source_function, Source_file, Source_line. ⑨ SWAPS: Displays the cost information of the number of swaps.

Guess you like

Origin blog.csdn.net/qq_51495235/article/details/133013929
Recommended