mysql 慢日志分析

mysql 调优首先需要找到那些有问题的SQL语句。

怎么找到这些语句呢?

mysql 提供了很方便的功能。

1.慢日志

在my.cnf 文件中,增加如下配置

log-error                      = /var/lib/mysql/mysql-error.log

#记录没有索引使用的查询。

log-queries-not-using-indexes  = 1

#记录慢查询

slow-query-log                 = 1

#慢查询日志

slow-query-log-file            = /var/lib/mysql/mysql-slow.log

slow-query-log :启用慢日志

log-queries-not-using-indexes :没有使用索引的查询

slow-query-log-file :日志路径

当服务器出现性能时,日志就会记录下来。

2.分析慢日志

有了慢日志,我们就可以使用这个日志进行分析,找出有问题的SQL语句。

分析工具可以使用:

https://www.percona.com/downloads/percona-toolkit/3.0.13/binary/redhat/7/x86_64/percona-toolkit-3.0.13-1.el7.x86_64.rpm

下载后安装

rpm -ivh percona-toolkit-3.0.13-1.el7.x86_64.rpm

安装后就可以进行使用了

pt-query-digest --limit 20  mysql-slow.log >result.log

这个命令的意思是分析慢日志,并且会将分析结果记录到 result.log 文件中。

# 35.4s user time, 90ms system time, 26.54M rss, 220.85M vsz
# Current date: Thu Apr 25 14:44:54 2019
# Hostname: localhost.localdomain
# Files: mysql-slow.log
# Overall: 85.73k total, 11 unique, 30.61 QPS, 0.00x concurrency _________
# Time range: 2019-04-25T06:18:19 to 2019-04-25T07:05:00
# Attribute          total     min     max     avg     95%  stddev  median
# ============     ======= ======= ======= ======= ======= ======= =======
# Exec time            10s    68us    24ms   116us   167us    95us   103us
# Lock time             4s    21us     4ms    44us    69us    32us    38us
# Rows sent         83.75k       0      24    1.00    0.99    0.08    0.99
# Rows examine     586.61k       0      48    7.01    6.98    0.54    6.98
# Query size         5.34M      46     435   65.32   80.10   12.29   54.21

# Profile
# Rank Query ID                           Response time Calls R/Call V/M  
# ==== ================================== ============= ===== ====== =====
#    1 0xB717B245D9B4D6CF8F79C4C320FFEC0C  6.2929 62.9% 57228 0.0001  0.00 SELECT os_rel_type
#    2 0x5F211D31CFFA376C13F39992DAE2394A  3.6963 36.9% 28453 0.0001  0.00 SELECT os_rel_type
#    3 0xE0F7C7EEB5446ECBF9C66FF0A1B4E8D6  0.0056  0.1%    15 0.0004  0.00 SELECT BPM_NODE_SET
#    4 0x7B35F1202DF2724043EFB10A443E25A1  0.0043  0.0%    15 0.0003  0.00 SELECT bpm_form_view
#    5 0x19E0ECF6F83257951B93561BB55257C7  0.0025  0.0%     5 0.0005  0.00 SELECT SYS_PROPERTIES
#    6 0x83A81E68FE6CCB29329B3D8C6F2DD7B9  0.0022  0.0%     1 0.0022  0.00 SELECT information_schema.PARTITIONS
#    7 0x7598123F1F2AFD056EBD09352AA28815  0.0021  0.0%     4 0.0005  0.00 SELECT sys_datasource_def
#    8 0x6C4E656EF7596EB3232AC868B24BD543  0.0017  0.0%     6 0.0003  0.00 SELECT SYS_INST
#    9 0x2A9FB8408CDA668DDD37078754092B47  0.0012  0.0%     4 0.0003  0.00 SELECT MI_DB_ID
#   10 0x951BF5C783033884B41893D8832E1E73  0.0007  0.0%     1 0.0007  0.00 SELECT information_schema.columns
#   11 0x7B8A1218880310FF773B6C94685844F4  0.0003  0.0%     1 0.0003  0.00 SELECT information_schema.triggers

我们在这里影响性能的就是 OS_REL_TYPE表。

select * from os_rel_type where KEY_='GROUP-USER-BELONG'G

# Query 2: 10.16 QPS, 0.00x concurrency, ID 0x5F211D31CFFA376C13F39992DAE2394A at byte 21216778
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: 2019-04-25T06:18:20 to 2019-04-25T07:05:00
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 33 28453
# Exec time 36 4s 84us 3ms 129us 176us 44us 119us
# Lock time 39 2s 31us 3ms 53us 76us 32us 47us
# Rows sent 33 27.79k 1 1 1 1 0 1
# Rows examine 33 194.50k 7 7 7 7 0 7
# Query size 42 2.28M 84 84 84 84 0 84

解释

Exec time :执行时间

Lock time :锁定时间

Rows sent :返回的记录数

Rows examine :扫描过的记录数,我们可以看到

这返回的数据和扫描的记录差的很大,所以我们可以通过创建索引。

Query size:查询语句的字符数

如果返回的记录数和扫描的记录数相差很远,那么就需要想办法创建索引了。

原文地址:https://www.cnblogs.com/yg_zhang/p/10771673.html