博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysqldumpslow日志分析
阅读量:2428 次
发布时间:2019-05-10

本文共 3370 字,大约阅读时间需要 11 分钟。

[zz]用mysqldumpslow分析mysql的slow query log
2009-06-18 16:43

一、mysqldumpslow

官方文档:

shell> mysqldumpslow [options] [log_file ...]

Table 4.12. mysqldumpslow Option Reference

Format Config File Description Introduction Deprecated Removed
Do not abstract all numbers to N and strings to S
Abstract numbers with at least the specified digits
Write debugging information
Only consider statements that match the pattern
Display help message and exit
Host name of the server in the log file name
Name of the server instance
Do not subtract lock time from total time
Reverse the sort order
How to sort output
Display only first num queries
Verbose mode

mysql有一个功能就是可以log下来运行的比较慢的sql语句,默认是没有这个log的,为了开启这个功能,要修改my.cnf或者在mysql启动的时候加入一些参数。如果在my.cnf里面修改,需增加如下几行

long_query_time = 1

log-slow-queries = /var/youpath/slow.log
log-queries-not-using-indexes

long_query_time 是指执行超过多久的sql会被log下来,这里是1秒。

log-slow-queries 设置把日志写在那里,可以为空,系统会给一个缺省的文件host_name-slow.log,我生成的log就在mysql的data目录
log-queries-not-using-indexes 就是字面意思,log下来没有使用索引的query。

把上述参数打开,运行一段时间,就可以关掉了,省得影响生产环境。

接下来就是分析了,我这里的文件名字叫host-slow.log。

mysqldumpslow –help以下,俺主要用的是
-s ORDER what to sort by (t, at, l, al, r, ar etc), ‘at’ is default
-t NUM just show the top n queries
-g PATTERN grep: only consider stmts that include this string

-s,是order的顺序,说明写的不够详细,俺用下来,包括看了代码,主要有

c,t,l,r和ac,at,al,ar,分别是按照query次数,时间,lock的时间和返回的记录数来排序,前面加了a的时倒叙
-t,是top n的意思,即为返回前面多少条的数据
-g,后边可以写一个正则匹配模式,大小写不敏感的

mysqldumpslow -s c -t 20 host-slow.log

mysqldumpslow -s r -t 20 host-slow.log

上述命令可以看出访问次数最多的20个sql语句和返回记录集最多的20个sql。

mysqldumpslow -t 10 -s t -g “left join” host-slow.log
这个是按照时间返回前10条里面含有左连接的sql语句。

用了这个工具就可以查询出来那些sql语句是性能的瓶颈,进行优化,比如加索引,该应用的实现方式等。

二、mysqldumpslow

MySQL 自带 slow log 的分析工具 mysqldumpslow ,但是没有说明。本文通过分析该脚本,介绍了其用法。

slow log 是 MySQL 根据 SQL 语句的执行时间设定,写入的一个文件,用于分析执行较慢的语句。
只要在 my.cnf 文件中配置好:
log-slow-queries = [slow_query_log_filename]
即可记录超过默认的 10s 执行时间的 SQL 语句。
如果要修改默认设置,可以添加:
long_query_time = 5
设定为 5s 。
如果要记录所有 SQL 语句,可以写入:
log-long-format
# t=time, l=lock time, r=rows
# at, al, 以及 ar 是对应的平均值
mysqldumpslow 可以接受的参数有:
'v+', # verbose
'd+', # debug
's=s', # 排序 (t, at, l, al, r, ar etc)
'r!', # 倒排序 (largest last instead of first)
't=i', # 显示最高的 n 个查询
'a!', # 不把所有的数字以 N ,字符串以 'S' 显示
'n=i', # abstract numbers with at least n digits within names
'g=s', # grep: only consider stmts that include this string
'h=s', # hostname of db server for *-slow.log filename (can be wildcard)
'i=s', # name of server instance (if using mysql.server startup script)
'l!', # don't subtract lock time from total time

'verbose|v+',# verbose

'help+', # write usage info
'debug|d+', # debug
's=s', # what to sort by (t, at, l, al, r, ar etc)
'r!', # reverse the sort order (largest last instead of first)
't=i', # just show the top n queries
'a!', # don't abstract all numbers to N and strings to 'S'
'n=i', # abstract numbers with at least n digits within names
'g=s', # grep: only consider stmts that include this string
'h=s', # hostname of db server for *-slow.log filename (can be wildcard)
'i=s', # name of server instance (if using mysql.server startup script)
'l!', # don't subtract lock time from total time

以时间倒序显示前10个慢查询日志:mysqldumpslow -s at -t 10 /var/db/mysql/db-slow.log

来源:

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7916042/viewspace-1026768/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7916042/viewspace-1026768/

你可能感兴趣的文章
决战第三屏:移动互联网时代的商业与营销新规则
查看>>
如何清理ibdata1
查看>>
Think HY 读《观止-微软》一书有感
查看>>
方刚先生谈《胜于言传——网站内容制胜宝典》
查看>>
【Frank.Xu】.net深入学习笔记(3):垃圾回收与内存管理
查看>>
2006JAVA类图书读者投票排行榜
查看>>
《编程之美》舞动’08年IT图书销售奇迹
查看>>
MySQL查询的性能优化
查看>>
微博的MySQL数据库优化实践经验
查看>>
php以图搜图
查看>>
php怎么实现根据图片搜索图片功能
查看>>
三种保证URL地址可信的加密方式
查看>>
memcached 并发原语CAS与GETS操作
查看>>
memcached(六)调优经验
查看>>
赶集mysql军规
查看>>
nginx/tengine限制流量如何配置
查看>>
cron和crontab命令详解 crontab 每分钟、每小时、每天、每周、每月、每年定时执行 crontab每5分钟执行一次
查看>>
mysql使用索引优化order排序
查看>>
mysql复合索引、普通索引总结
查看>>
mysql explain中的using filesort
查看>>