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参数,把分析结果输出到表中
—create-history-table 但是用—history参数,把分析结果输出到表中
—filter 对输入的慢查询按指定的字符串进行匹配过滤后,在进行分析
—limit 限制输出结果百分比或数量,默认值是20,即将最慢的20条语句输出
—host HostName
—user 用户名
—password 密码
—history 将分析结果保存到表中,分析结果比较详细
—review 将分析结果保存到表中
—output 分析结果输出类型
—since 从什么时间开始分析,值为字符串
—until 截止时间,配合since一起分析
分析结果:
其中:
Overall:总共有多少条查询
unique:唯一查询数量
Time range:查询执行的时间范围
total:总计
min:最小
max:最大
avg:平均
95%:95%的查询时间,重点分析
median:中位数,把所有值从小到大排列,位置位于中间那个数
详细的分析结果:
Response:总的响应时间
time:该查询在本次分析中占用的时间比
Calls:执行次数
R/Call:平均每次执行的响应时间
Item:查询对象
每一条查询的详细分析结果:
ID:查询的ID号,和上图的Query ID对应
Databases: 数据库名
Users:执行的用户
Query_time distribution:查询的时间分布(此图说明,查询几乎都在1ms-10ms之间)
Tables:查询涉及到的表
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:查询语句一部分
每条查询语句的分析:
# Query 1: 0.06 QPS, 0.24x concurrency, ID 0xF9A57DD5A41825CA at byte 1151
# This item is included in the report because it matches --limit.
# Scores: Apdex = 0.33 [1.0]*, V/M = 0.31
# Query_time sparkline: | ^ |
# Time range: 2014-08-02 17:05:48 to 17:07:30
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 100 6
# Exec time 100 24s 3s 6s 4s 6s 1s 4s
# Lock time 0 0 0 0 0 0 0 0
# Rows sent 100 6 1 1 1 1 0 1
# Rows examine 0 0 0 0 0 0 0 0
# Query size 100 90 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(6)G