Bug #91496 Regression: Slow_queries status variable isn't incremented if slow query is off
Submitted: 29 Jun 2018 11:40 Modified: 7 Jun 2022 15:43
Reporter: Mikiya Okuno Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Logging Severity:S3 (Non-critical)
Version:5.7.3 OS:Any
Assigned to: CPU Architecture:Any

[29 Jun 2018 11:40] Mikiya Okuno
Description:
https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html#statvar_Slow_queries

The manual says "The number of queries that have taken more than long_query_time seconds. This counter increments regardless of whether the slow query log is enabled."

Actually, the counter isn't incremented when slow query is not enabled. This problem happenes after 5.7.3 due to fix for bug #69420 (Oracle bug #16924125).

How to repeat:
* Disable slow query log.
* Execute a query which takes longer than long_query_time.
* See "SHOW GLOBAL STATUS LIKE 'Slow_queries'" output.

Suggested fix:
diff --git a/sql/log.cc b/sql/log.cc
index e9b43a6..d812c74 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -1711,7 +1711,6 @@ bool log_slow_applicable(THD *thd)
 void log_slow_do(THD *thd)
 {
   THD_STAGE_INFO(thd, stage_logging_slow_query);
-  thd->status_var.long_query_count++;
 
   if (thd->rewritten_query.length())
     query_logger.slow_log_write(thd,
@@ -1724,6 +1723,7 @@ void log_slow_do(THD *thd)
 
 void log_slow_statement(THD *thd)
 {
+  thd->status_var.long_query_count++;
   if (log_slow_applicable(thd))
     log_slow_do(thd);
 }
[29 Jun 2018 12:05] MySQL Verification Team
The suggest fix was wrong. The following patch is the true fix.

diff --git a/sql/log.cc b/sql/log.cc
index e9b43a6..1a0669a 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -1711,7 +1711,6 @@ bool log_slow_applicable(THD *thd)
 void log_slow_do(THD *thd)
 {
   THD_STAGE_INFO(thd, stage_logging_slow_query);
-  thd->status_var.long_query_count++;
 
   if (thd->rewritten_query.length())
     query_logger.slow_log_write(thd,
@@ -1724,6 +1723,10 @@ void log_slow_do(THD *thd)
 
 void log_slow_statement(THD *thd)
 {
+  if (thd->server_status & SERVER_QUERY_WAS_SLOW)
+  {
+    thd->status_var.long_query_count++;
+  }
   if (log_slow_applicable(thd))
     log_slow_do(thd);
 }
[26 Aug 2019 15:53] Paul DuBois
Posted by developer:
 
Fixed in 8.0.19.

For slow query logging, the Slow_queries was not implemented unless
the slow query log was enabled, contrary to the documentation.
[27 Aug 2021 5:40] MySQL Verification Team
Bug #104240 marked as duplicate of this one.
[7 Jun 2022 15:43] Daniel Price
Posted by developer:
 
Fixed as of the upcoming 8.0.30 release:

For slow query logging, the Slow_queries status variable was not
implemented unless the slow query log was enabled, contrary to the
documentation.