简体   繁体   English

mysql 5.7:为什么慢查询日志中缺少38%的查询?

[英]mysql 5.7: Why are 38% of queries missing from the slow query log?

Sporadically we're seeing thousands of queries per second reported by show global status like "Queries" but don't see them in the slow query log with everything enabled, so we can't see what these queries are. 偶尔,我们会看到每秒show global status like "Queries"成千上万的查询,这些查询是通过show global status like "Queries"报告的,但是在启用了所有功能的慢速查询日志中看不到它们,因此我们看不到这些查询是什么。

Here are (what I think are) the relevant global variables if I want to see everything in the slow query log: 如果想在慢查询日志中查看所有内容 ,则以下是(我认为是)相关的全局变量:

log_queries_not_using_indexes   ON
log_slow_admin_statements   ON
log_slow_slave_statements   ON
log_throttle_queries_not_using_indexes  0
long_query_time 0.000000
min_examined_row_limit  0
slow_launch_time    2
slow_query_log  ON
slow_query_log_file /tmp/slow.log

Over a 5min period, mysql global status variable Questions reports 90433 queries, but there are only 56479 queries in the slow log. 在5分钟内,mysql全局状态变量Questions报告90433个查询,但慢日志中只有56479个查询。 Where are the missing 33954 (38%) queries? 缺少的33954(38%)查询在哪里?

I enable the log with a enableLog.pl script that sets up the above variables for 5 minutes, then reverts them to their original values: 我使用enableLog.pl脚本启用了日志,该脚本将上述变量设置了5分钟,然后将其还原为原始值:

# mysql --login-path=cm -Ee 'show  global status like "Queries"' && rm -rf /tmp/slow.log && ./enableLog.pl --duration 5m /tmp/slow.log && mysql --login-path=cm -Ee 'show  global status like "Queries"'
*************************** 1. row ***************************
Variable_name: Queries
        Value: 73570440
11:41:51: logging for 00:05:00 until 11:46:51
... ending
*************************** 1. row ***************************
Variable_name: Queries
        Value: 73660873

# perl -E 'say 73660873-73570440'
90433

# grep '^# Time: ' /tmp/slow.log | wc -l
56479

# perl -E 'say 90433-56479'
33954

# perl -E 'say +(90433-56479)/90433'
0.375460285515243

While enableLog.pl is running here are the (relevant) global variables: 当enableLog.pl运行时,以下是(相关的)全局变量:

# mysql --login-path=cm -e 'show global variables;' | egrep '^(slow_|log_|min_)'
log_bin OFF
log_bin_basename    
log_bin_index   
log_bin_trust_function_creators OFF
log_bin_use_v1_row_events   OFF
log_builtin_as_identified_by_password   OFF
log_error   /var/log/mysql/error.log
log_error_verbosity 3
log_output  FILE
log_queries_not_using_indexes   ON
log_slave_updates   OFF
log_slow_admin_statements   ON
log_slow_slave_statements   ON
log_syslog  OFF
log_syslog_facility daemon
log_syslog_include_pid  ON
log_syslog_tag  
log_throttle_queries_not_using_indexes  0
log_timestamps  UTC
log_warnings    2
long_query_time 0.000000
min_examined_row_limit  0
slow_launch_time    2
slow_query_log  ON
slow_query_log_file /tmp/slow.log

# mysql --version
mysql  Ver 14.14 Distrib 5.7.9, for Linux (x86_64) using  EditLine wrapper

Also, they aren't query cache hits: 而且,它们不是查询缓存命中:

# mysql --login-path=cm -e 'SHOW GLOBAL STATUS LIKE "Qcache_hits";'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Qcache_hits   | 0     |
+---------------+-------+

Note that changes to global variables are not inherited by existing sessions. 请注意,对全局变量的更改不会被现有会话继承。

Sessions make a copy of global variables when the session starts, and the session does not re-copy global variables during the session's lifetime. 会话在会话启动时会创建全局变量的副本,并且该会话在会话的生存期内不会重新复制全局变量。 So if sessions are relatively long-lived, some won't have "heard" that they should log their slow queries. 因此,如果会话的寿命相对较长,则有些会话不会“听到”它们应该记录其缓慢的查询。 The only way to ensure that all sessions heed the new global settings is to make them re-connect and start new sessions. 确保所有会话都注意新的全局设置的唯一方法是使它们重新连接并开始新的会话。

Some other answers have suggested enabling the general query log, but this won't help if sessions are long-lived and don't know about changes to the global config options. 其他一些答案建议启用常规查询日志,但是如果会话寿命很长并且不知道全局配置选项的更改,这将无济于事。 The general query log won't be enabled for those sessions either. 这些会话也不会启用常规查询日志。

Percona Server implemented a config option slow_query_log_use_global_control to make sessions use the global option for certain query-log options. Percona Server实现了配置选项slow_query_log_use_global_control,以使会话将全局选项用于某些查询日志选项。 But this feature is not in stock MySQL. 但是此功能在MySQL中不存在。

I implemented a script similar to your enableLog.pl. 我实现了一个类似于您的enableLog.pl的脚本。 It's here: 它在这里:

From the manual : 手册

The server does not write queries handled by the query cache to the slow query log, nor queries that would not benefit from the presence of an index because the table has zero rows or one row. 服务器不会将查询缓存处理的查询写入慢速查询日志,也不会因为表有零行或一行而无法从索引的存在中受益。

Check how many queries were served by the query cache with 检查查询缓存为多少查询提供服务

SHOW STATUS LIKE 'Qcache%';

Look for Qcache_hits. 寻找Qcache_hits。 You can also check the GLOBAL value. 您还可以检查GLOBAL值。

  • read more about the query cache here 在此处阅读有关查询缓存的更多信息

When you want to know, turn on BOTH the slow query log, as described AND the General Log in the same script. 如果您想知道,请同时打开慢速查询日志(如所述)和同一脚本中的“常规日志”。 It will be real easy to compare side by side to see what the General Log is doing that is 'missing' in the slow query log. 并排比较以查看慢速查询日志中“丢失”的通用日志正在做的事情将是真正容易的。

Remember turn OFF the General Log at the end of your session to avoid filling your storage device. 请记住在会话结束时关闭常规日志,以免填满存储设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM