MySQL数据库的响应时间的收集方法

  • A+
所属分类:数据库技术

MySQL database response time

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

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

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

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

如何使用tcprstat ?

安装:

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

运行:

  1. [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'`
  2. timestamp       count   max     min     avg     med     stddev  95_max  95_avg  95_std  99_max  99_avg  99_std
  3. 1420614369      728     84075   76      868     244     6273    420     211     105     14159   247     551
  4. 1420614370      724     63779   77      701     248     4770    403     218     106     3308    232     165
  5. 1420614371      724     44319   73      469     248     2843    407     217     112     653     227     122
  6. 1420614372      682     87216   75      864     242     6189    430     218     117     15382   280     826
  7. 1420614373      650     73711   78      793     256     5483    427     219     116     13176   253     535
  8. 1420614374      617     85264   77      978     254     6715    408     218     115     24093   293     1128
  9. 1420614375      730     96018   75      905     244     6737    418     214     113     13574   248     527
  10. 1420614376      686     106426  78      1094    242     7984    415     212     111     34149   331     1681
  11. 1420614377      683     83807   74      886     245     6270    410     209     112     23873   279     1061
  12. 1420614378      727     93795   77      925     239     6682    409     208     112     23836   269     1018
  13. 1420614379      730     84769   77      805     241     5772    422     209     115     13808   243     535
  14. 1420614380      748     93895   75      977     234     7214    397     202     109     24140   266     1031
  15. 1420614381      753     87112   78      825     238     6027    404     205     109     14669   240     562
  16. 1420614382      727     878833  75      2105    241     33216   417     210     118     24386   277     1052
  17. 1420614383      671     88954   74      841     237     6269    408     207     115     13867   241     558

注意: 一般参考 95_avg 的值

输出格式:

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

how percona server does?

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

安装插件

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

检查

  1. SHOW PLUGINS;
  2. ....
  3. | partition                   | ACTIVE   | STORAGE ENGINE     | NULL                   | GPL     |
  4. | QUERY_RESPONSE_TIME_AUDIT   | ACTIVE   | AUDIT              | query_response_time.so | GPL     |
  5. | QUERY_RESPONSE_TIME         | ACTIVE   | INFORMATION SCHEMA | query_response_time.so | GPL     |
  6. +-----------------------------+----------+--------------------+------------------------+---------+

设置参数

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

SELECT * from INFORMATION_SCHEMA.QUERY_RESPONSE_TIME ;

  1. dbadmin@(none) 04:45:06> SELECT * from INFORMATION_SCHEMA.QUERY_RESPONSE_TIME ;
  2. +----------------+-------+----------------+
  3. | TIME           | COUNT | TOTAL          |
  4. +----------------+-------+----------------+
  5. |       0.000001 |     0 |       0.000000 |
  6. |       0.000010 |    92 |       0.000524 |
  7. |       0.000100 |    66 |       0.005463 |
  8. |       0.001000 |   112 |       0.064937 |
  9. |       0.010000 |    12 |       0.013653 |
  10. |       0.100000 |     0 |       0.000000 |
  11. |       1.000000 |     0 |       0.000000 |
  12. |      10.000000 |     0 |       0.000000 |
  13. |     100.000000 |     0 |       0.000000 |
  14. |    1000.000000 |     0 |       0.000000 |
  15. |   10000.000000 |     0 |       0.000000 |
  16. |  100000.000000 |     0 |       0.000000 |
  17. 1000000.000000 |     0 |       0.000000 |
  18. | TOO LONG       |     0 | TOO LONG       |
  19. +----------------+-------+----------------+

set GLOBAL query_response_time_flush = on :

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

总结

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