Home > Database > Mysql Tutorial > See how to enable slow query in mysql? A brief discussion on the principle of slow query log

See how to enable slow query in mysql? A brief discussion on the principle of slow query log

php是最好的语言
Release: 2019-02-25 16:31:24
Original
2089 people have browsed it

mysql slow query log is a log record provided by mysql. It is used to record statements in mysql whose corresponding time exceeds the threshold. It means that SQL whose running time exceeds the long_query_time value will be recorded. in the slow query log. The default value of long_query_time is 10, which means running statements for more than 10 seconds.

Recommended mysql video tutorials: "mysql tutorial"

Slow query log settings

1 , check whether the slow query log is turned on Command:

show variables like '%slow_query_log%'
Copy after login

2. Set the command to enable slow query

set global slow_query_log=1
Copy after login

Note:
slow_query_log ON means to turn on, OFF means to turn off
slow_query_log_file is the storage address of the slow query log

3. Query and modify the time of slow query definition

show variables like 'long_query_time%'
Copy after login
set global long_query_time=4
Copy after login

4. Queries that do not use indexes are recorded in the slow query log. If tuning, it is recommended to enable this option. If this parameter is enabled, the SQL of full index scan will also be recorded in the slow query log.

show variables like 'log_queries_not_using_indexes'
Copy after login
set global log_queries_not_using_indexes=1
Copy after login

5. Query how many slow query records there are

show global status like '%Slow_queries%';
Copy after login

mysqldumpslow slow log analysis tool
Command:

-s 按照那种方式排序    c:访问计数
    l:锁定时间
    r:返回记录
    al:平均锁定时间
    ar:平均访问记录数
    at:平均查询时间
-t 是top n的意思,返回多少条数据。-g 可以跟上正则匹配模式,大小写不敏感。
Copy after login

Get the 20 sql with the most returned records

mysqldumpslow -s r -t 20 sqlslow.log
Copy after login

Get the 20 sql with the highest average number of visits

mysqldumpslow -s ar -t 20 sqlslow.log
Copy after login

Get the 20 sql with the highest average number of visits and containing ttt characters

mysqldumpslow -s ar -t 20 -g "ttt" sqldlow.log
Copy after login

Note:
1. If -bash: mysqldumpslow: command not found error occurs, please execute

ln -s /usr/local/mysql/bin/mysqldumpslow /usr/bin
Copy after login

2. If the following error occurs, Died at /usr/bin/mysqldumpslow line 161, <> chunk 405659. It means you want to analyze The sql log is too large, please split it and then analyze it

The splitting command is:

tail -100000 mysql-slow.log>mysql-slow.20180725.log
Copy after login

Related articles:

Enable mysql slow query log and Use the mysqldumpslow command to view

Use mysqldumpslow and mysqlsla to analyze the mysql slow query log

Related videos:

MySQL data management Backup and recovery case analysis video tutorial

The above is the detailed content of See how to enable slow query in mysql? A brief discussion on the principle of slow query log. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template