mysql服务器有一项功能,能够检测到哪条sql语句查询得比较慢,就是慢查询slowlog,如今介绍如何开启。
在[mysqld]下面增长以下代码:java
long_query_time = 1 log-slow-queries = /usr/local/mysql/data/slow.log log-queries-not-using-indexes
long_query_time = 1 #定义超过1秒的查询计数到变量Slow_queries。
log-slow-queries = /usr/local/mysql/data/slow.log #定义慢查询日志路径。
log-queries-not-using-indexes #未使用索引的查询也被记录到慢查询日志中(可选)。
mysql自带了一个查看慢日志的工具mysqldumpslow。
执行mysqldumpslow –h能够查看帮助信息。
主要介绍两个参数-s和-t
-s 这个是排序参数,可选的有:
al: 平均锁定时间
ar: 平均返回记录数
at: 平均查询时间
c: 计数
l: 锁定时间
r: 返回记录
t: 查询时间mysql
-t n 显示头n条记录。
实例:
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语句是性能的瓶颈,进行优化,好比加索引,该应用的实现方式等。git
附帮助:sql
[root@yzdb1 sqlpro]# 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 (al, at, ar, c, l, r, t), 'at' is default al: average lock time ar: average rows sent at: average query time c: count l: lock time r: rows sent t: query time -r reverse the sort order (largest last instead of first) -t NUM just show the top n queries -a don't abstract all numbers to N and strings to 'S' -n NUM abstract numbers with at least n digits within names -g PATTERN grep: only consider stmts that include this string -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard), 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