(编辑:jimmy 日期: 2024/12/25 浏览:2)
MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中。long_query_time的默认值为10,意思是运行10S以上的语句。默认情况下,Mysql数据库并不启动慢查询日志,需要我们手动来设置这个参数,当然,如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响。慢查询日志支持将日志记录写入文件,也支持将日志记录写入数据库表。
官方文档,关于慢查询的日志介绍如下(部分资料,具体参考官方相关链接):
The slow query log consists of SQL statements that took more than long_query_time seconds to execute and required at least min_examined_row_limit rows to be examined. The minimum and default values of long_query_time are 0 and 10, respectively. The value can be specified to a resolution of microseconds. For logging to a file, times are written including the microseconds part. For logging to tables, only integer times are written; the microseconds part is ignored.
By default, administrative statements are not logged, nor are queries that do not use indexes for lookups. This behavior can be changed usinglog_slow_admin_statements and log_queries_not_using_indexes, as described later.
MySQL 慢查询的相关参数解释:
慢查询日志会把查询耗时超过规定时间的SQL语句记录下来,利用慢查询日志,可以定位分析性能的瓶颈
查看慢查询日志功能是否开启,以及慢查询日志文件存放目录
SHOW VARIABLES LIKE 'slow_query%'
在MySQL配置文件 /etc/my.cnf 中,设置
slow_query_log=ON long_query_time=1
开启慢查询日志,记录查询超过1秒的sql语句,重启MySQL后生效。
可以使用下面sql测试以下
SELECT SLEEP(2);
慢查询日志记录文件
Tcp port: 0 Unix socket: (null) Time Id Command Argument # Time: 210125 6:30:14 # User@Host: reptile[reptile] @ [192.168.10.254] Id: 1 # Query_time: 2.000380 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 SET timestamp=1611556214; SELECT SLEEP(2);