Questions and applications of slow query

The question is raised: When you work, your boss may ask you, my database system has been running for a year, and the system query data has slowed down, how to optimize it? Then you must say that indexing optimization is done. Is there any problem with all SQL? Of course not, I want to optimize specific SQL statements, find out the slowest query, and find out the most frequent query, I will calculate it, and then perform targeted optimization. then what should we do? We need to use the slow query log tool. Let's talk about how to use slow query in detail below.

Ordinary development programmers in the company have no access to these, because they don't have the authority to operate the database, they are usually handed over to the operation and maintenance personnel, and the operation and maintenance personnel are generally allowed to open the slow query log. Intercept one day's data, or query data for a period of time, specifically find out the slowest query, and find out the SQL statement with the most queries for advantages.

What is slow query

  • The slow query log of MySQL is a log record provided by MySQL. It is used to record the statements whose response time exceeds the threshold in MySQL. Specifically long_query_time, the SQL whose running time exceeds the value will be recorded in the slow query log.
  • Specifically long_query_time, the SQL whose running time exceeds the value will be recorded in the slow query log. long_query_timeThe default value is 10, which means to run the statement for more than 10 seconds.
  • It is up to him to check which SQL exceeds our maximum endurance time value. For example, if a SQL executes for more than 5 seconds, we even if it is slow SQL, we hope to collect more than 5 seconds of SQL explainfor comprehensive analysis.
    (Note: The slow query log is not always on. After the slow query log is turned on, whenever SQL is slow, it must be written to the log. Writing the log will cause IO, which will make the system slower.)

How to use slow query log

By default, MySQL database does not enable slow query log, we need to manually set this parameter.
Of course, if it is not required for tuning, it is generally not recommended to enable this parameter, because turning on the slow query log will more or less bring about a certain performance impact. Slow query log supports writing log records to files

Check whether to enable slow query log

Check whether slow query is enabled

SHOW VARIABLES LIKE '%slow_query_log%';

Insert a picture description here. Insert picture description here
By default, the value of slow_query_log is OFF, indicating that the slow query log is disabled.

Can be opened by the set values slow_query_log
use

set global slow_query_log=1;

The slow query log is enabled only for the current database.
Insert picture description here

If MySQL is restarted, it will fail.

What kind of sql will be recorded in the slow query log

SHOW VARIABLES LIKE '%slow_query_log%';

Insert picture description here

This is controlled by the parameter long_query_time. By default, the value of long_query_time is 10 seconds, which means that queries over ten seconds will be recorded in the slow query log. This time is too long, we need to modify the default value.

You can use the command to modify it, or you can modify it in the my.cnf parameter.

If the running time is exactly equal to long_query_time, it will not be recorded. In other words,
in the mysql source code, the judgment is greater than long_query_time, not greater than or equal.

Set the threshold to 0.1s

set long_query_time=0.1

Insert picture description here
Without adding global, it is only valid in the current window, and it becomes invalid once you reconnect to the database

set global long_query_time=0.1

Add global, MySQL will fail after restarting.

SHOW VARIABLES LIKE '%slow_query_log%';

Insert picture description here
Run a few sql statements, open /var/lib/mysql/cocoon-slow.log to view the slow query log, and
Insert picture description here
Insert picture description here
query how many slow query records are in the current system.

show global status like '%Slow_queries%';

Insert picture description here

Log analysis tool mysqldumpslow

In a production environment, if you want to manually analyze logs, find and analyze SQL, it is obviously an individual effort. MySQL provides a log analysis tool mysqldumpslow.

mysqldumpslow --help

Insert picture description here
Insert picture description here

#得到返回记录集最多的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

Guess you like

Origin blog.csdn.net/qq_43925089/article/details/109436007