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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

慢查询日志:

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

慢查询日志的配置:


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


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

1
2
3
4
5
6
7
8
9
10
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[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%';

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+------------------------------------+------------------------------+
| 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%';

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
+------------------------------------+------------------------------+
| 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
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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










本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1720789,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4天前
|
C++
JNI Log 日志输出
JNI Log 日志输出
13 1
|
4天前
|
存储 运维 大数据
聊聊日志硬扫描,阿里 Log Scan 的设计与实践
泛日志(Log/Trace/Metric)是大数据的重要组成,伴随着每一年业务峰值的新脉冲,日志数据量在快速增长。同时,业务数字化运营、软件可观测性等浪潮又在对日志的存储、计算提出更高的要求。
|
5天前
|
关系型数据库 MySQL 数据管理
MySQL通过 bin-log 恢复从备份点到灾难点之间数据
MySQL通过 bin-log 恢复从备份点到灾难点之间数据
|
11天前
|
XML Java Maven
Springboot整合与使用log4j2日志框架【详解版】
该文介绍了如何在Spring Boot中切换默认的LogBack日志系统至Log4j2。首先,需要在Maven依赖中排除`spring-boot-starter-logging`并引入`spring-boot-starter-log4j2`。其次,创建`log4j2-spring.xml`配置文件放在`src/main/resources`下,配置包括控制台和文件的日志输出、日志格式和文件切分策略。此外,可通过在不同环境的`application.yml`中指定不同的log4j2配置文件。最后,文章提到通过示例代码解释了日志格式中的各种占位符含义。
|
11天前
|
运维 监控 Go
Golang深入浅出之-Go语言中的日志记录:log与logrus库
【4月更文挑战第27天】本文比较了Go语言中标准库`log`与第三方库`logrus`的日志功能。`log`简单但不支持日志级别配置和多样化格式,而`logrus`提供更丰富的功能,如日志级别控制、自定义格式和钩子。文章指出了使用`logrus`时可能遇到的问题,如全局logger滥用、日志级别设置不当和过度依赖字段,并给出了避免错误的建议,强调理解日志级别、合理利用结构化日志、模块化日志管理和定期审查日志配置的重要性。通过这些实践,开发者能提高应用监控和故障排查能力。
87 1
|
11天前
|
DataWorks 关系型数据库 MySQL
DataWorks产品使用合集之在DataWorks中,如何通过PolarDB for MySQL来查看binlog日志
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
28 1
|
12天前
|
SQL 监控 关系型数据库
【MySQL学习】MySQL的慢查询日志和错误日志
【MySQL学习】MySQL的慢查询日志和错误日志
|
12天前
|
弹性计算 运维 Shell
|
18天前
|
SQL 存储 关系型数据库
MySQL慢日志的介绍以及如何使用问题
MySQL慢日志的介绍以及如何使用问题
19 0
|
1天前
|
关系型数据库 MySQL 数据库
docker MySQL删除数据库时的错误(errno: 39)
docker MySQL删除数据库时的错误(errno: 39)