IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    MySQL系列之MySQL Response Time详解

    Vlix_Liu发表于 2015-12-06 16:03:50
    love 0

    MySQL database response time

    MySQL数据库运维过程中,我们经常通过一些系统指标,DB指标来check数据库的运行状态。响应时间就是一个很重要的参数。但是响应时间一般都比较难抓取。特别是MySQL数据库。统计信息这块做的还是有很大的进步空间。不像Oracle可以通过统计信息来计算响应时间。

    今天大致整理了一下,MySQL数据库的响应时间的收集方法。目前业界主要有两种方式:

    1. 通过tcpdump 抓网络包,来进行分析。对应的工具 tcprstat
    2. 原理和1类似,只不过是集成到了数据库中【Percona Server & MariaDB】
    

    下面分别介绍一下两种方法

    如何使用tcprstat ?

    安装:

    从 http://github.com/downloads/Lowercases/tcprstat/tcprstat-static.v0.3.1.x86_64 下载
    放到 PATH 路径下 , 比如 : /usr/bin
    重命名为 tcprstat
    添加可执行权限: chmod +x
    

    运行:

    [root@xxx ~]# /home/mysql/bin/tcprstat -p 3306 -t 1 -n 0 -l `/sbin/ifconfig | grep 'addr:[^ ]\+' -o | cut -f 2 -d : | xargs echo | sed -e 's/ /,/g'` 
    timestamp       count   max     min     avg     med     stddev  95_max  95_avg  95_std  99_max  99_avg  99_std
    1420614369      728     84075   76      868     244     6273    420     211     105     14159   247     551
    1420614370      724     63779   77      701     248     4770    403     218     106     3308    232     165
    1420614371      724     44319   73      469     248     2843    407     217     112     653     227     122
    1420614372      682     87216   75      864     242     6189    430     218     117     15382   280     826
    1420614373      650     73711   78      793     256     5483    427     219     116     13176   253     535
    1420614374      617     85264   77      978     254     6715    408     218     115     24093   293     1128
    1420614375      730     96018   75      905     244     6737    418     214     113     13574   248     527
    1420614376      686     106426  78      1094    242     7984    415     212     111     34149   331     1681
    1420614377      683     83807   74      886     245     6270    410     209     112     23873   279     1061
    1420614378      727     93795   77      925     239     6682    409     208     112     23836   269     1018
    1420614379      730     84769   77      805     241     5772    422     209     115     13808   243     535
    1420614380      748     93895   75      977     234     7214    397     202     109     24140   266     1031
    1420614381      753     87112   78      825     238     6027    404     205     109     14669   240     562
    1420614382      727     878833  75      2105    241     33216   417     210     118     24386   277     1052
    1420614383      671     88954   74      841     237     6269    408     207     115     13867   241     558 
    

    注意: 一般参考 95_avg 的值

    输出格式:

    Format Code Header  Default?    Meaning
    %n      count       y   Count of requests that completed during this iteration
    %a      avg         y   Average response time
    %s      sum         y   Sum of response times
    %x      sqs             Sum of squares of response times
    %m      min         y   Minimum response time
    %M      max         y   Maximum response time
    %h      med         y   Median response time
    %S      stddev      y   Standard deviation of response times
    %v      var             Variance of response times
    %I      iter#           Iteration number
    %t      elapsed         Seconds elapsed since the first iteration
    %T      timestamp   y   Unix timestamp
    %%                       A literal %
    \t                      A tab character
    \n                      A newline character
    95,99  Adds a prefix    y   A percentile indicator;  
    

    how percona server does?

    percona server 实现了一个叫做 响应时间区间 的特性 ,5.6 之后开始以Plugin的方式提供。需要单独安装。

    安装插件

    mysql> INSTALL PLUGIN QUERY_RESPONSE_TIME_AUDIT SONAME 'query_response_time.so';
    mysql> INSTALL PLUGIN QUERY_RESPONSE_TIME SONAME 'query_response_time.so';
    

    检查

    SHOW PLUGINS;
    ....
    | partition                   | ACTIVE   | STORAGE ENGINE     | NULL                   | GPL     |
    | QUERY_RESPONSE_TIME_AUDIT   | ACTIVE   | AUDIT              | query_response_time.so | GPL     |
    | QUERY_RESPONSE_TIME         | ACTIVE   | INFORMATION SCHEMA | query_response_time.so | GPL     |
    +-----------------------------+----------+--------------------+------------------------+---------+
    

    设置参数

    SET GLOBAL query_response_time_stats = 1 ; # start stat collection
    SET GLOBAL query_response_time_flush='ON';  
    

    SELECT * from INFORMATION_SCHEMA.QUERY_RESPONSE_TIME ;

    dbadmin@(none) 04:45:06> SELECT * from INFORMATION_SCHEMA.QUERY_RESPONSE_TIME ;
    +----------------+-------+----------------+
    | TIME           | COUNT | TOTAL          |
    +----------------+-------+----------------+
    |       0.000001 |     0 |       0.000000 |
    |       0.000010 |    92 |       0.000524 |
    |       0.000100 |    66 |       0.005463 |
    |       0.001000 |   112 |       0.064937 |
    |       0.010000 |    12 |       0.013653 |
    |       0.100000 |     0 |       0.000000 |
    |       1.000000 |     0 |       0.000000 |
    |      10.000000 |     0 |       0.000000 |
    |     100.000000 |     0 |       0.000000 |
    |    1000.000000 |     0 |       0.000000 |
    |   10000.000000 |     0 |       0.000000 |
    |  100000.000000 |     0 |       0.000000 |
    | 1000000.000000 |     0 |       0.000000 |
    | TOO LONG       |     0 | TOO LONG       |
    +----------------+-------+----------------+
    

    set GLOBAL query_response_time_flush = on :

    Clears the collected times from the QUERY_RESPONSE_TIME table ; 
    Reads the value of query_response_time_range_base and uses it to set the range base for the table 
    

    总结

    一般数据库监控项中,db响应时间对我们诊断问题,还是有很大的帮助的。我们可以参考以上两种方式来对mysql数据库的响应时间收集起来,可以绘制曲线。



沪ICP备19023445号-2号
友情链接