亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于MySQL的慢日志分析工具

標簽:
MySQL


今天我们看看关于MySQL慢日志的阅读。

我们知道,如果我们的语句不够优化,那么首先MySQL的慢日志是进一步优化的离线证据,虽然里面有好 多“伪慢语句”!

先不说怎么优化,如果你的日志有一条语句赌住了,那么会有不计其数的慢语句填充到MySQL的满日志里面。那么首先提炼出这些语 句就非常头疼。

今天主要介绍两种工具:

1,mysqldumpslow。(咱们 MySQL自带的简单而又实用的工具)

我们先来看下mysqldumpslow的结果。

[root@localhost ~]# mysqldumpslow -r localhost-slow.log

 

Reading mysql slow query log from localhost-slow.log

Count: 2  Time=7.00s (14s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  select * from t_page_sample order by id desc limit N,N

Count: 1  Time=11.00s (11s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@localhost

  select count(*) from t_page_sample

Count: 1  Time=1418.00s (1418s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  insert ignore into t_page_sample select ceil(rand()*N), ceil(rand()*N), date_sub(now(),interval floor(rand()*N) day), now() from t_page_sample

比如要查找排序的慢语句:

[root@localhost ~]# mysqldumpslow -r -g "order by " localhost-slow.log

 

Reading mysql slow query log from localhost-slow.log

Count: 2  Time=7.00s (14s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  select * from t_page_sample order by id desc limit N,N

 

自带的mysqldumpslow简单 实用,作为我个人的首选。关于具体的参数含义,可以参见它自身的HELP。

2,mk-query-digest。(网上著名的开源脚本家族Maatkit中一员)

手 册地址:http://www.maatkit.org/doc/mk-query-digest.html

下载方法:wget http://www.maatkit.org/get/mk-query-digest

完了赋给它可执行权限就OK。

mk-query-digest 功能太多,我今天只是试了下它对MySQL慢日志的分析功能。

以下是我觉得比较实用的功能。

1)分析慢日志并且把找出来的语句写到规定的 表里。

[root@localhost ~]# ./mk-query-digest --limit 2 --select Query_time,Lock_time,Rows_sent,Rows_examined,ts --create-review-table --review D=t_girl,t=query_review localhost-slow.log

# 280ms user time, 80ms system time, 11.56M rss, 16.65M vsz

# Current date: Sat May  8 02:47:39 2010

# Files: localhost-slow.log

# Overall: 4 total, 3 unique, 0.01 QPS, 1.96x concurrency ________________

#                    total     min     max     avg     95%  stddev  median

# Exec time          1443s      5s   1418s    361s   1357s    584s    684s

# Lock time              0       0       0       0       0       0       0

# Rows sent              1       0       1    0.25    0.99    0.43       0

# Rows exam          6.98M   1.69M   1.89M   1.74M   1.86M  76.62k   1.69M

# Time range        2010-05-08 00:28:42 to 2010-05-08 00:40:58

# Profile

# Rank Query ID           Response time    Calls R/Call    Item

# ==== ================== ================ ===== ========= ===============

#    1 0x2A94F91D8C3B4B26  1418.0000 99.0%     1 1418.0000 INSERT SELECT t_page_sample

#    2 0x06754F1BD3C8D697    14.0000  1.0%     2    7.0000 SELECT t_page_sample

# Query 1: 0 QPS, 0x concurrency, ID 0x2A94F91D8C3B4B26 at byte 0 ________

# This item is included in the report because it matches --limit.

#              pct   total     min     max     avg     95%  stddev  median

# Count         25       1

# Exec time     98   1418s   1418s   1418s   1418s   1418s       0   1418s

# Lock time      0       0       0       0       0       0       0       0

# Rows sent      0       0       0       0       0       0       0       0

# Rows exam     27   1.89M   1.89M   1.89M   1.89M   1.89M       0   1.89M

# Time range 2010-05-08 00:28:42 to 2010-05-08 00:28:42

# Query_time distribution

#   1us

#  10us

# 100us

#   1ms

#  10ms

# 100ms

#    1s

#  10s+  ################################################################

# Review information

#    first_seen: 2010-05-08 00:28:42

#     last_seen: 2010-05-08 00:28:42

#   reviewed_by: 

#   reviewed_on: 

#      comments: 

# Tables

#    SHOW TABLE STATUS FROM `t_girl` LIKE 't_page_sample'\G

#    SHOW CREATE TABLE `t_girl`.`t_page_sample`\G

insert ignore into t_page_sample select ceil(rand()*10000000), ceil(rand()*9), date_sub(now(),interval floor(rand()*20) day), now() from t_page_sample\G

# Query 2: 0.07 QPS, 0.47x concurrency, ID 0x06754F1BD3C8D697 at byte 1499

# This item is included in the report because it matches --limit.

#              pct   total     min     max     avg     95%  stddev  median

# Count         50       2

# Exec time      0     14s      5s      9s      7s      9s      3s      7s

# Lock time      0       0       0       0       0       0       0       0

# Rows sent      0       0       0       0       0       0       0       0

# Rows exam     48   3.39M   1.69M   1.69M   1.69M   1.69M       0   1.69M

# Time range 2010-05-08 00:40:28 to 2010-05-08 00:40:58

# Query_time distribution

#   1us

#  10us

# 100us

#   1ms

#  10ms

# 100ms

#    1s  ################################################################

#  10s+

# Review information

#    first_seen: 2010-05-08 00:40:28

#     last_seen: 2010-05-08 00:40:58

#   reviewed_by: 

#   reviewed_on: 

#      comments: 

# Tables

#    SHOW TABLE STATUS FROM `t_girl` LIKE 't_page_sample'\G

#    SHOW CREATE TABLE `t_girl`.`t_page_sample`\G

# EXPLAIN

select * from t_page_sample order by id desc limit 4400000,2\G

 

[root@localhost ~]#

 

因为慢日志里面会有写和读语句,所以当以后想要分析某类语句时,只需要简单的SELECT即可出来。

mysql> select * from query_review where fingerprint like 'select%'\G

*************************** 1. row ***************************

   checksum: 465365117438580375

fingerprint: select * from t_page_sample order by id desc limit ?

     sample: select * from t_page_sample order by id desc limit 4400000,2

 first_seen: 2010-05-08 00:40:28

  last_seen: 2010-05-08 00:40:58

reviewed_by: NULL

reviewed_on: NULL

   comments: NULL

1 rows in set (0.00 sec)

2. 分析当前运行的SQL语句。

以 前我都是自己写脚本,配合CRONTAB来定时抓取信息到固定的文件里以备分析。不过现在可以用它来打印出比较详细的报告来。

[root@localhost ~]# ./mk-query-digest --select Query_time,Lock_time,Rows_sent,Rows_examined  --processlist h=localhost,u=root

# Caught SIGINT.

# 690ms user time, 2.4s system time, 11.27M rss, 16.41M vsz

# Current date: Sat May  8 03:17:39 2010

# Files: STDIN

# Overall: 1 total, 1 unique, 0 QPS, 0x concurrency ______________________

#                    total     min     max     avg     95%  stddev  median

# Exec time        1273313855s 1273313855s 1273313855s 1273313855s 1273313855s       0 1273313855s

# Lock time              0       0       0       0       0       0       0

# Profile

# Rank Query ID           Response time          Calls R/Call          Ite

# ==== ================== ====================== ===== =============== ===

#    1 0xB52E1970DE36E57F 1273313854.8595 100.0%     1 1273313854.8595 SELECT t_page_sample

# Query 1: 0 QPS, 0x concurrency, ID 0xB52E1970DE36E57F at byte 0 ________

# This item is included in the report because it matches --limit.

#              pct   total     min     max     avg     95%  stddev  median

# Count        100       1

# Exec time    100 1273313855s 1273313855s 1273313855s 1273313855s 1273313855s       0 1273313855s

# Lock time      0       0       0       0       0       0       0       0

# Query_time distribution

#   1us

#  10us

# 100us

#   1ms

#  10ms

# 100ms

#    1s

#  10s+  ################################################################

# Tables

#    SHOW TABLE STATUS FROM `t_girl` LIKE 't_page_sample'\G

#    SHOW CREATE TABLE `t_girl`.`t_page_sample`\G

# EXPLAIN

select count(*) from t_page_sample\G

[root@localhost ~]#

当 想停止截取当前语句时,按住CTRL+C就OK。

 

不过我还是喜欢我自己的那个小脚本。哈哈。

©著作权归作者所有:来自51CTO博客作者david_yeung的原创作品,如需转载,请注明出处,否则将追究法律责任

MySQL数据库 优化MySQL常用工具


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消