mysql有一个功能就是可以log下来运行的比较慢的sql语句,默认是没有这个log的,为了开启这个功能,要修改my.cnf或者在mysql启动的时候加入一些参数。 如果在my.cnf里面修改,需增加如下几行 long_query_time = 10 log-slow-queries = long_query_time 是指执行超过多久的sql会被log下来,这里是10秒。 这是一个有用的日志。它对于性能的影响不大(假设所有查询都很快),并且强调了那些最需要注意的查询(丢失了索引或索引没有得到最佳应用) # Time: 070927 8:08:52 # User@Host: root[root] @ [192.168.0.20] # Query_time: 372 Lock_time: 136 Rows_sent: 152 Rows_examined: 263630 如果日志内容很多,用眼睛一条一条去看会累死,mysql自带了分析的工具,使用方法如下: 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 (t, at, l, al, r, ar etc), ‘at’ is default -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 scrīpt) -l don’t subtract lock time from total time -s,是order的顺序,说明写的不够详细,俺用下来,包括看了代码,主要有 mysqldumpslow -s c -t 20 host-slow.log 上述命令可以看出访问次数最多的20个sql语句和返回记录集最多的20个sql。 |
mysql慢查询日志分析
m