0

0

MySQL 日志之--慢查询日志(slow-query-log)_MySQL

php中文网

php中文网

发布时间:2016-05-27 13:46:31

|

1361人浏览过

|

来源于php中文网

原创

慢查询日志:

 

SnapEdit
SnapEdit

AI移除图片中的任何物体

下载

MySQL慢查询日志记录下所有执行超过long_query_time时间的SQL语句,帮你找到执行慢的SQL,方便我们对这些SQL进行优化。  

 

慢查询日志的配置:

 

默认情况下,mysql没有启用慢查询日志。

 

[root@rh64 ~]# mysql -u root -p

 

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.6.25-73.1 Percona Server (GPL), Release 73.1, Revision 07b797f

Copyright (c) 2009-2015 Percona LLC and/or its affiliates

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners。

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%slow%';

+------------------------------------+------------------------------+
| Variable_name                      | Value                        |
+------------------------------------+------------------------------+
| log_slow_admin_statements          | OFF                          |
| log_slow_filter                    |                              |
| log_slow_rate_limit                | 1                            |
| log_slow_rate_type                 | session                      |
| log_slow_slave_statements          | OFF                          |
| log_slow_sp_statements             | ON                           |
| log_slow_verbosity                 |                              |
| max_slowlog_files                  | 0                            |
| max_slowlog_size                   | 0                            |
| slow_launch_time                   | 2                            |
| slow_query_log                     | OFF                          |
| slow_query_log_always_write_time   | 10.000000                    |
| slow_query_log_file                | /var/lib/mysql/rh64-slow.log |
| slow_query_log_timestamp_always    | OFF                          |
| slow_query_log_timestamp_precision | second                       |
| slow_query_log_use_global_control  |                              |
+------------------------------------+------------------------------+
16 rows in set (0.01 sec)

 

 

1、可以配置my.cnf文件,服务启动时自动配置

[root@rh64 ~]# cat /etc/my.cnf

 

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend

sql_mode=STRICT_TRANS_TABLES ,NO_ENGINE_SUBSTITUTION

 

slow_query_log=true

slow_query_log_file = "/var/lib/mysql/rh64-slow.log"

long_query_time=1

log-queries-not-using-indexes=true

 

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

explicit_defaults_for_timestamp=true

innodb_buffer_pool_size = 128M

join_buffer_size = 128M

sort_buffer_size = 2M

read_rnd_buffer_size = 2M

 

重启server后,查看:

mysql> show variables like '%slow%';

 

+------------------------------------+------------------------------+
| Variable_name                      | Value                        |
+------------------------------------+------------------------------+
| log_slow_admin_statements          | OFF                          |
| log_slow_filter                    |                              |
| log_slow_rate_limit                | 1                            |
| log_slow_rate_type                 | session                      |
| log_slow_slave_statements          | OFF                          |
| log_slow_sp_statements             | ON                           |
| log_slow_verbosity                 |                              |
| max_slowlog_files                  | 0                            |
| max_slowlog_size                   | 0                            |
|
 slow_launch_time                   | 2                            |
| slow_query_log                     | ON                           |
| slow_query_log_always_write_time   | 10.000000                    |
| slow_query_log_file                | /var/lib/mysql/rh64-slow.log |
| slow_query_log_timestamp_always    | OFF                          |
| slow_query_log_timestamp_precision | second                       |
| slow_query_log_use_global_control  |                              |
+------------------------------------+------------------------------+
16 rows in set (0.00 sec)

 

2、在系统中配置slow-query-log

mysql> set @@global.slow_query_log = on;

mysql> show variables like '%slow%';

 

+------------------------------------+------------------------------+
| Variable_name                      | Value                        |
+------------------------------------+------------------------------+
| log_slow_admin_statements          | OFF                          |
| log_slow_filter                    |                              |
| log_slow_rate_limit                | 1                            |
| log_slow_rate_type                 | session                      |
| log_slow_slave_statements          | OFF                          |
| log_slow_sp_statements             | ON                           |
| log_slow_verbosity                 |                              |
| max_slowlog_files                  | 0                            |
| max_slowlog_size                   | 0                            |
| slow_launch_time                   | 2                            |
| slow_query_log                     | ON                           |
| slow_query_log_always_write_time   | 10.000000                    |
| slow_query_log_file                | /var/lib/mysql/rh64-slow.log |
| slow_query_log_timestamp_always    | OFF                          |
| slow_query_log_timestamp_precision | second                       |
| slow_query_log_use_global_control  |                              |
+------------------------------------+------------------------------+
16 rows in set (0.00 sec)

 

3、查看慢查询日志信息

[root@rh64 mysql]# tail rh64-slow.log 

 

use prod;
SET timestamp=1449476453;
insert into emp1 select * from emp1;
# Time: 151207 16:21:11
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 16.748949  Lock_time: 0.000137  Rows_sent: 0  Rows_examined: 1236992  Rows_affected: 618496
# Bytes_sent: 58
SET timestamp=1449476471;
insert into emp1 select * from emp1;
[root@rh64 mysql]# tail -f rh64-slow.log 
use prod;
SET timestamp=1449476453;
insert into emp1 select * from emp1;
# Time: 151207 16:21:11
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 16.748949  Lock_time: 0.000137  Rows_sent: 0  Rows_examined: 1236992  Rows_affected: 618496
# Bytes_sent: 58
SET timestamp=1449476471;
insert into emp1 select * from emp1;
# Time: 151207 16:22:54
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 44.036039  Lock_time: 0.000083  Rows_sent: 0  Rows_examined: 2473984  Rows_affected: 1236992
# Bytes_sent: 59
SET timestamp=1449476574;
insert into emp1 select * from emp1;
# Time: 151207 16:26:46
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 106.674422  Lock_time: 0.000148  Rows_sent: 0  Rows_examined: 4947968  Rows_affected: 2473984
# Bytes_sent: 59
SET timestamp=1449476806;
insert into emp1 select * from emp1;

 

记录没有使用索引的语句:

mysql> set @@global.log_queries_not_using_indexes=on;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show variables like '%index%';
+----------------------------------------+-------+
| Variable_name                          | Value |
+----------------------------------------+-------+
| eq_range_index_dive_limit              | 10    |
| expand_fast_index_creation             | OFF   |
| innodb_adaptive_hash_index             | ON    |
| innodb_adaptive_hash_index_partitions  | 1     |
| innodb_cmp_per_index_enabled           | OFF   |
| log_bin_index                          |       |
| log_queries_not_using_indexes          | ON    |
| log_throttle_queries_not_using_indexes | 0     |
| relay_log_index                        |       |
+----------------------------------------+-------+
9 rows in set (0.00 sec)

 

测试:

mysql> select count(*) from emp1 where empno=7788;

+----------+

| count(*) |

+----------+

|   688128 |

+----------+

1 row in set (4.03 sec)

 

[root@rh64 mysql]# tail rh64-slow.log 

 

SET timestamp=1449476453;
insert into emp1 select * from emp1;
# Time: 151207 16:21:11
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 16.748949  Lock_time: 0.000137  Rows_sent: 0  Rows_examined: 1236992  Rows_affected: 618496
# Bytes_sent: 58

SET timestamp=1449476471;
insert into emp1 select * from emp1;
# Time: 151207 16:22:54
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 44.036039  Lock_time: 0.000083  Rows_sent: 0  Rows_examined: 2473984  Rows_affected: 1236992
# Bytes_sent: 59

SET timestamp=1449476574;
insert into emp1 select * from emp1;
# Time: 151207 16:26:46
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 106.674422  Lock_time: 0.000148  Rows_sent: 0  Rows_examined: 4947968  Rows_affected: 2473984
# Bytes_sent: 59

SET timestamp=1449476806;
insert into emp1 select * from emp1;
# Time: 151207 16:30:44
# User@Host: root[root] @ localhost []  Id:     2
# Schema: prod  Last_errno: 0  Killed: 0
# Query_time: 4.025612  Lock_time: 0.000098  Rows_sent: 1  Rows_examined: 4947968  Rows_affected: 0
# Bytes_sent: 68

SET timestamp=1449477044;
select count(*) from emp1 where empno=7788;

 

 

4、通过mysqldumpslow工具查看慢查询日志

[root@rh64 mysql]# mysqldumpslow

Can't determine basedir from 'my_print_defaults mysqld' output: --datadir=/var/lib/mysql
--socket=/var/lib/mysql/mysql.sock
--user=mysql
--symbolic-links=0
--innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend
--sql_mode=STRICT_TRANS_TABLES ,NO_ENGINE_SUBSTITUTION

 

[root@rh64 mysql]# mysqldumpslow --help

 

Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
  --verbose    verbose
  --debug      debug
  --help       write this text to standard output
  -v           verbose
  -d           debug
  -s ORDER     what to sort by (al, at, ar, c, l, r, t), 'at' is default
                al: average lock time
                ar: average rows sent
                at: average query time
                 c: count
                 l: lock time
                 r: rows sent
                 t: query time  
  -r           reverse the sort order (largest last instead of first)
  -t NUM       just show the top n queries
  -a           don't abstract all numbers to N and strings to 'S'
  -n NUM       abstract numbers with at least n digits within names
  -g PATTERN   grep: only consider stmts that include this string
  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
  -i NAME      name of server instance (if using mysql.server startup script)
  -l           don't subtract lock time from total time

 

  [root@rh64 mysql]# mysqldumpslow rh64-slow.log 

 

Reading mysql slow query log from rh64-slow.log
Count: 3  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  # Schema: prod  Last_errno: N  Killed: N
  # Query_time: N.N  Lock_time: N.N  Rows_sent: N  Rows_examined: N  Rows_affected: N
  # Bytes_sent: N
  SET timestamp=N;
  insert into emp1 select * from emp1
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  # Schema: prod  Last_errno: N  Killed: N
  # Query_time: N.N  Lock_time: N.N  Rows_sent: N  Rows_examined: N  Rows_affected: N
  # Bytes_sent: N
  use prod;
  SET timestamp=N;
  insert into emp1 select * from emp1
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  # Schema: prod  Last_errno: N  Killed: N
  # Query_time: N.N  Lock_time: N.N  Rows_sent: N  Rows_examined: N  Rows_affected: N
  # Bytes_sent: N
  SET timestamp=N;
  select count(*) from emp1 where empno=N

 

按照平均锁定时间进行排序,查找前10名:

  [root@rh64 mysql]# mysqldumpslow -s al -n 10 rh64-slow.log 

Reading mysql slow query log from rh64-slow.log

 

Count: 3  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  # Schema: prod  Last_errno: N  Killed: N

  # Query_time: N.N  Lock_time: N.N  Rows_sent: N  Rows_examined: N  Rows_affected: N

  # Bytes_sent: N

  SET timestamp=N;

  insert into emp1 select * from emp1

 

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  # Schema: prod  Last_errno: N  Killed: N

  # Query_time: N.N  Lock_time: N.N  Rows_sent: N  Rows_examined: N  Rows_affected: N

  # Bytes_sent: N

  use prod;

  SET timestamp=N;

  insert into emp1 select * from emp1

 

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  # Schema: prod  Last_errno: N  Killed: N

  # Query_time: N.N  Lock_time: N.N  Rows_sent: N  Rows_examined: N  Rows_affected: N

  # Bytes_sent: N

  SET timestamp=N;

  select count(*) from emp1 where empno=N

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
AO3官网入口与中文阅读设置 AO3网页版使用与访问
AO3官网入口与中文阅读设置 AO3网页版使用与访问

本专题围绕 Archive of Our Own(AO3)官网入口展开,系统整理 AO3 最新可用官网地址、网页版访问方式、正确打开链接的方法,并详细讲解 AO3 中文界面设置、阅读语言切换及基础使用流程,帮助用户稳定访问 AO3 官网,高效完成中文阅读与作品浏览。

89

2026.02.02

主流快递单号查询入口 实时物流进度一站式追踪专题
主流快递单号查询入口 实时物流进度一站式追踪专题

本专题聚合极兔快递、京东快递、中通快递、圆通快递、韵达快递等主流物流平台的单号查询与运单追踪内容,重点解决单号查询、手机号查物流、官网入口直达、包裹进度实时追踪等高频问题,帮助用户快速获取最新物流状态,提升查件效率与使用体验。

24

2026.02.02

Golang WebAssembly(WASM)开发入门
Golang WebAssembly(WASM)开发入门

本专题系统讲解 Golang 在 WebAssembly(WASM)开发中的实践方法,涵盖 WASM 基础原理、Go 编译到 WASM 的流程、与 JavaScript 的交互方式、性能与体积优化,以及典型应用场景(如前端计算、跨平台模块)。帮助开发者掌握 Go 在新一代 Web 技术栈中的应用能力。

11

2026.02.02

PHP Swoole 高性能服务开发
PHP Swoole 高性能服务开发

本专题聚焦 PHP Swoole 扩展在高性能服务端开发中的应用,系统讲解协程模型、异步IO、TCP/HTTP/WebSocket服务器、进程与任务管理、常驻内存架构设计。通过实战案例,帮助开发者掌握 使用 PHP 构建高并发、低延迟服务端应用的工程化能力。

4

2026.02.02

Java JNI 与本地代码交互实战
Java JNI 与本地代码交互实战

本专题系统讲解 Java 通过 JNI 调用 C/C++ 本地代码的核心机制,涵盖 JNI 基本原理、数据类型映射、内存管理、异常处理、性能优化策略以及典型应用场景(如高性能计算、底层库封装)。通过实战示例,帮助开发者掌握 Java 与本地代码混合开发的完整流程。

5

2026.02.02

go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

62

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

55

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

27

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号