slow log 设置:
cat my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
#skip-grant-tables
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
log_bin=mysql-bin
slow_query_log=on
long_query_time=1
#long_query_time = 1
slow_query_log = 1
slow_query_log_file = slow.log
************************************************************
mysql查看:
mysql> show variables like "%query%";
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| long_query_time | 1.000000 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | slow.log |
+------------------------------+----------+
13 rows in set (0.00 sec)
pt-query-digest:分析
常用选项: —create-review-table 当使用—review参数,把分析结果输出到表中EXPLAIN:SQL语句
pt-query-digest安装:
yum -y install perl perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes
*********
rpm包安装:
rpm -ivh percona-toolkit-2.1.7-1.noarch.rpm
warning: percona-toolkit-2.1.7-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing... ########################################### [100%]
1:percona-toolkit ########################################### [100%]
使用pt-query-digest分析满查询输出结果:
pt-query-digest slow.log
# 450ms user time, 60ms system time, 23.30M rss, 197.64M vsz
该工具执行日志分析的用户时间,系统时间,物理内存占用大小,虚拟内存占用大小
# Current date: Sat Aug 2 17:33:05 2014
工具执行时间
# Hostname: cj149
运行分析工具的主机名
# Files: slow.log
被分析的文件名
# Overall: 6 total, 1 unique, 0.06 QPS, 0.24x concurrency ________________
语句总数量,唯一的语句数量,QPS,并发数
# Time range: 2014-08-02 17:05:48 to 17:07:30
日志记录的时间范围
# Attribute total min max avg 95% stddev median
属性 总计 最小 最大 平均 95% 标准 中等
# ============ ======= ======= ======= ======= ======= ======= =======
# Exec time 24s 3s 6s 4s 6s 1s 4s
语句执行时间
# Lock time 0 0 0 0 0 0 0
锁占用时间
# Rows sent 6 1 1 1 1 0 1
发送到客户端的行数
# Rows examine 0 0 0 0 0 0 0
select语句扫描行数
# Query size 90 15 15 15 15 0 15
查询的字符数
# Profile
# Rank Query ID Response time Calls R/Call Apdx V/M Item
# ==== ================== ============== ===== ====== ==== ===== ======
# 1 0xF9A57DD5A41825CA 24.0022 100.0% 6 4.0004 0.33 0.31 SELECT
Rank:所有语句的排名,默认按查询时间降序排列,通过--order-by指定
Query ID :语句的ID,(去掉多余空格和文本字符,计算hash值)
Response time:响应时间,占所有响应时间的百分比
Calls:查询执行次数
R/Call:平均响应时间
V/M:响应时间Variance-to-mean的比率
Item:查询语句一部分