Mysql--慢查询日志之一
1.当查询超过一定时间没有返回结果的时候,才会记录进慢查询日志。
2.慢查询日志可以帮助DBA找出执行效率缓慢的SQL语句,为数据库优化工作提供帮助。
3.慢查询日志默认是不开启的,也没必要一直开启。(如果开启必定会消耗系统的性能)
4.当需要进行采样分析时手工开启。
慢查询日志相关参数
mysql.cnf中修改以下配置后,需要重新启动数据库服务使之生效。
slow_query_log=on|off---是否开启慢查询日志
slow_query_log_file=filename---指定慢查询日志保存路径及文件名。不设置则使用默认值。默认存放位置为数据库文件所在目录下,名称为hostname-slow.log
long_query_time=2---指定多少秒未返回结果的查询语句属于慢查询。
long-queries-not-using-indexes---记录所有没有使用到索引的查询语句。
min_examined_row_limit=1000---记录那些由于查找了多余1000次而引发的慢查询。
log-slow-admin-statements---记录那些慢的OPTIMIZE TABLE, ANALYZE TABLE和ALTER TABLE语句。
log-slow-slave-statements---记录由slave所产生的慢查询。
不重启修改慢查询配置
SET @@global.slow_query_log=1 ---1开启 0关闭
SET @@global.long_query_time=3
或者
SET global slow_query_log=1
SET global long_query_time=3
其他参数可通过以下命令查阅:
show variables like ‘%slow%‘;
Model开启慢查询日志:
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.22 sec)
mysql> set global long_query_time=2;
Query OK, 0 rows affected (0.04 sec)
[root@gusha ~]# cd /var/lib/mysql/
[root@gusha mysql]# pwd
/var/lib/mysql
[root@gusha mysql]# ls ---目录下会多出一个主机名-slow.log的文件
auto.cnf gusha.wyzc.com.err ib_logfile1 RPM_UPGRADE_HISTORY
gusha gusha.wyzc.com.pid mysql RPM_UPGRADE_MARKER-LAST
gusha.log ibdata1 mysql.sock t1.ibd
gusha-slow.log ib_logfile0 performance_schema worldb
mysql> select sleep(3);
+----------+
| sleep(3) |
+----------+
| 0 |
+----------+
1 row in set (3.05 sec)
mysql> select variables like ‘long%‘;
ERROR 1054 (42S22): Unknown column ‘variables‘ in ‘field list‘
mysql> show variables like ‘long%‘;
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 2.000000 |
+-----------------+----------+
1 row in set (0.25 sec)
[root@gusha mysql]# tail -f gusha-slow.log ---查看日记记录的相关信息
/usr/sbin/mysqld, Version: 5.6.16 (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 150204 3:13:06
# User@Host: root[root] @ localhost [] Id: 23
# Query_time: 3.022983 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1422990786;
select sleep(3);
常见的慢查询分析工具
1.mysqldumpslow---mysql自带的工具,展示效果不友好
2.mysqlsla
hackmysql.com出品的一款分析工具。可以在其网站下载到,或者在系统提示符下输入以下命令下载:
#wget http://hackmysql.com/scripts/mysqlsla
3.percona-toolkit中的pt-query-digest---安装时需要注意依赖包
A-mysqldumpslow
[root@gusha mysql]# mysqldumpslow gusha-slow.log ---慢日志文件名字
Reading mysql slow query log from gusha-slow.log
Count: 1 Time=3.02s (3s) Lock=0.00s (0s) Rows=1.0 (1), root[root]@localhost
select sleep(N)
B-mysqlsla
[root@gusha ~]# cd /var/mysql-7/
[root@gusha mysql-7]# ls
MySQL-5.6.17-1.el6.src.rpm mysqlsla-2.03
mysql-5.6.17.tar.gz percona-toolkit-2.2.7-1.noarch.rpm
[root@gusha mysql-7]# chmod +x mysqlsla-2.03
[root@gusha mysql-7]# ./mysqlsla-2.03 /var/lib/mysql/gusha-slow.log
Auto-detected logs as slow logs
Report for slow logs: /var/lib/mysql/gusha-slow.log
1 queries total, 1 unique
Sorted by ‘t_sum‘
Grand Totals: Time 3 s, Lock 0 s, Rows sent 1, Rows Examined 0
______________________________________________________________________ 001 ___
Count : 1 (100.00%)
Time : 3.022983 s total, 3.022983 s avg, 3.022983 s to 3.022983 s max (100.00%)
Lock Time (s) : 0 total, 0 avg, 0 to 0 max (0.00%)
Rows sent : 1 avg, 1 to 1 max (100.00%)
Rows examined : 0 avg, 0 to 0 max (0.00%)
Database :
Users :
root@localhost : 100.00% (1) of query, 100.00% (1) of all users
Query abstract:
SET timestamp=N; SELECT sleep(N);
Query sample:
SET timestamp=1422990786;
select sleep(3);
C-percona-toolkit
[root@gusha mysql-7]# yum install percona-toolkit-2.2.7-1.noarch.rpm
.......
[root@gusha mysql-7]# pt-query-digest /var/lib/mysql/gusha-slow.log
# 170ms user time, 20ms system time, 23.98M rss, 202.45M vsz
# Current date: Wed Feb 4 03:33:40 2015
# Hostname: gusha.wyzc.com
# Files: /var/lib/mysql/gusha-slow.log
# Overall: 1 total, 1 unique, 0 QPS, 0x concurrency ______________________
# Time range: all events occurred at 2015-02-04 03:13:06
# Attribute total min max avg 95% stddev median
# ============ ======= ======= ======= ======= ======= ======= =======
# Exec time 3s 3s 3s 3s 3s 0 3s
# Lock time 0 0 0 0 0 0 0
# Rows sent 1 1 1 1 1 0 1
# Rows examine 0 0 0 0 0 0 0
# Query size 15 15 15 15 15 0 15
# Profile
# Rank Query ID Response time Calls R/Call V/M Item
# ==== ================== ============= ===== ====== ===== ======
# 1 0xF9A57DD5A41825CA 3.0230 100.0% 1 3.0230 0.00 SELECT
# Query 1: 0 QPS, 0x concurrency, ID 0xF9A57DD5A41825CA at byte 0 ________
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2015-02-04 03:13:06
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 100 1
# Exec time 100 3s 3s 3s 3s 3s 0 3s
# Lock time 0 0 0 0 0 0 0 0
# Rows sent 100 1 1 1 1 1 0 1
# Rows examine 0 0 0 0 0 0 0 0
# Query size 100 15 15 15 15 15 0 15
# String:
# Hosts localhost
# Users root
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms
# 100ms
# 1s ################################################################
# 10s+
# EXPLAIN /*!50100 PARTITIONS*/
select sleep(3)\G
删除慢查询日志
慢查询日志会不断的増长。因此往往仅仅在做例如采样分析这样的特别工作时才开启,其他的时候则关闭。无用的慢日志要记得及时删除。
删除慢查询日志有两种方法:
1.操作系统命令直接删除。
2.在MySQL提示符下使用flush logs命令;或在系统提示符下使用mysqladmin flush-logs命令。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。