首页 > 文章详情 > MySQL慢查询 - 开启慢查询

MySQL慢查询 - 开启慢查询

原创 YuanDong 2019-10-04 浏览量(104)
文章简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能

一、简介

开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能。

二、参数说明

slow_query_log 慢查询开启状态
slow_query_log_file 慢查询日志存放的位置(这个目录需要MySQL的运行帐号的可写权限,一般设置为MySQL的数据存放目录)
long_query_time 查询超过多少秒才记录

三、设置步骤

1.查看慢查询相关参数

mysql> show variables like 'slow_query_log%';
+---------------------------+----------------------------------+
| Variable_name             | Value                            |
+---------------------------+----------------------------------+
| slow_query_log            | OFF                              |
| slow_query_log_file       | /mysql/data/localhost-slow.log   |
+---------------------------+----------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+

2.设置方法
方法一:全局变量设置
将 slow_query_log 全局变量设置为“ON”状态

mysql> set global slow_query_log='ON';

设置慢查询日志存放的位置

mysql> set global slow_query_log_file='/usr/local/mysql/data/slow-query-log.log';

查询超过1秒就记录

mysql> set global long_query_time=1;

方法二:配置文件设置
修改配置文件my.cnf,在[mysqld]下的下方加入

[mysqld]
slow_query_log = ON          #开启慢查询
slow_query_log_file = /usr/local/mysql/data/slow-query-log.log     #慢查询日志路径
long_query_time = 1           #慢查询时间阈值 单位时间秒

3.重启MySQL服务

service mysql restart

4.查看设置后的参数

mysql> show variables like 'slow_query%';
+---------------------+-------------------------------------------+
| Variable_name       | Value                                     |
+---------------------+-------------------------------------------+
| slow_query_log      | ON                                        |
| slow_query_log_file | /usr/local/mysql/data/slow-query-log.log  |
+---------------------+-------------------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+

四、测试

1.执行一条慢查询SQL语句

mysql> select sleep(2);

2.查看是否生成慢查询日志

ls /usr/local/mysql/data/slow.log

如果日志存在,MySQL开启慢查询设置成功

五、慢查询日志分析(分析工具)

分析工具干什么事的呢,其实就是把mysql-slow.log里面记录下来的数据,分析一下显示出来。其实自己写一个shell脚本也是可以把要的信息取出来的。我们来看一下mysql-slow.log里面到底是什么东西。

[root@BlackGhost mysql]# cat mysql-slow.log     //查看命令  
/usr/local/mysql/libexec/mysqld, Version: 5.1.26-rc-log (Source distribution). started with:  
Tcp port: 3306  Unix socket: /tmp/mysql.sock  
Time                 Id Command    Argument  
# Time: 100814 13:28:30  
# User@Host: root[root] @ localhost []  
# Query_time: 10.096500  Lock_time: 0.045791 Rows_sent: 1  Rows_examined: 2374192  
SET timestamp=1281763710;  
select count(distinct ad_code) as x from ad_visit_history where ad_code in (select ad_code from ad_list where media_id=15);  
# Time: 100814 13:37:02  
# User@Host: root[root] @ localhost []  
# Query_time: 10.394134  Lock_time: 0.000091 Rows_sent: 1  Rows_examined: 2374192  
SET timestamp=1281764222;  
select count(distinct ad_code) as x from ad_visit_history where ad_code in (select ad_code from ad_list where media_id=15);  
# Time: 100814 13:37:16  
# User@Host: root[root] @ localhost []  
# Query_time: 4.608920  Lock_time: 0.000078 Rows_sent: 1  Rows_examined: 1260544  
SET timestamp=1281764236;  
select count(*) as cou  from ad_visit_history where ad_code in (select ad_code from ad_list where id=41) order by id desc;

看到了,就是记录一下sql语句的执行情况,包括执行时间,锁定时间等,所以要不要分析工具看个人情况,分析工具很多,在这儿只说一下mysql自带的慢查询分析工具mysqldumpslow的使用方法。

[root@BlackGhost bin]# mysqldumpslow -h  
Option h requires an argument  
ERROR: bad option  

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 (t, at, l, al, r, ar etc), 'at' is default     //排序方式query次数,时间,lock的时间和返回的记录数来排序  
 -r           reverse the sort order (largest last instead of first)       //倒排序  
 -t NUM       just show the top n queries                                       //显示前N多个  
 -a           don't abstract all numbers to N and strings to 'S' 
 -n NUM       abstract numbers with at least n digits within names   //抽象的数字,至 少有n位内的名称 
 -g PATTERN   grep: only consider stmts that include this string      //配置模式 
 -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),     //mysql所以机器名或者IP 
 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@VM_0_12_centos bin]# ./mysqldumpslow -s r -t 20 /usr/local/mysql/mysql-slow.log
[root@VM_0_12_centos bin]# ./mysqldumpslow -s r -t 20 -g 'count' /usr/local/mysql/mysql-slow.log

热门评论 (0)

网友评论 0 条评论 / 0 人参与