Skip to content

Commit 40810ba

Browse files
montywivuvova
authored andcommitted
MDEV-33144 Implement the Percona variable slow_query_log_always_write_time
This task is inspired by the Percona implementation of slow_query_log_always_write_time. This task implements the variable log_slow_always_query_time (name matching other MariaDB variables using the slow query log). The default value for the variable is 31536000, which makes MariaDB compatible with older installations. For queries with execution time longer than log_slow_always_query_time the variables log_slow_rate_limit and log_slow_min_examined_row_limit will be ignored and the query will be written to the slow query log if there is no other limitations (like log_slow_filter etc). Other things: - long_query_time internal variable renamed to log_slow_query_time. - More descriptive information for "log_slow_query_time".
1 parent bf9662f commit 40810ba

11 files changed

+154
-31
lines changed

mysql-test/main/log_slow.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ select @@log_slow_verbosity;
1111
show variables like "log_slow%";
1212
Variable_name Value
1313
log_slow_admin_statements ON
14+
log_slow_always_query_time 31536000.000000
1415
log_slow_disabled_statements sp
1516
log_slow_filter admin,filesort,filesort_on_disk,filesort_priority_queue,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk
1617
log_slow_max_warnings 10
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
select @@session.log_slow_always_query_time;
2+
@@session.log_slow_always_query_time
3+
31536000.000000
4+
SET GLOBAL log_slow_always_query_time= 20.2;
5+
SET SESSION log_slow_always_query_time=20.2;
6+
select @@global.log_slow_always_query_time, @@session.log_slow_always_query_time;
7+
@@global.log_slow_always_query_time @@session.log_slow_always_query_time
8+
20.200000 20.200000
9+
set GLOBAL log_slow_always_query_time= default;
10+
SET log_slow_query_time=0.5;
11+
SET log_slow_rate_limit=999;
12+
SET log_slow_always_query_time=1.5;
13+
SET log_slow_min_examined_row_limit= 100;
14+
[slow_log_start.inc] log_slow_always_query_time-1
15+
SELECT sleep(2) as 'this-should-be-logged';
16+
this-should-be-logged
17+
0
18+
SELECT sleep(1) as 'this-should-not-be-logged';
19+
this-should-not-be-logged
20+
0
21+
[log_grep.inc] file: log_slow_always_query_time-1 pattern: this-should expected_matches: 1
22+
[log_grep.inc] lines: 1
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--source include/not_valgrind.inc
2+
# We cannot use ps2 protocol as the test is counting number of executions
3+
--disable_ps2_protocol
4+
# View protocol disabled as it remove the tag from the SELECT query
5+
--disable_view_protocol
6+
7+
#
8+
# Test log_slow_always_query_time variable usage
9+
#
10+
11+
--source include/log_slow_prepare.inc
12+
13+
--let $log_slow_prefix=log_slow_always_query_time
14+
--let $log_file=$log_slow_prefix-1
15+
16+
select @@session.log_slow_always_query_time;
17+
SET GLOBAL log_slow_always_query_time= 20.2;
18+
SET SESSION log_slow_always_query_time=20.2;
19+
select @@global.log_slow_always_query_time, @@session.log_slow_always_query_time;
20+
set GLOBAL log_slow_always_query_time= default;
21+
22+
SET log_slow_query_time=0.5;
23+
SET log_slow_rate_limit=999;
24+
SET log_slow_always_query_time=1.5;
25+
SET log_slow_min_examined_row_limit= 100;
26+
27+
--source include/log_slow_start.inc
28+
SELECT sleep(2) as 'this-should-be-logged';
29+
SELECT sleep(1) as 'this-should-not-be-logged';
30+
31+
--let grep_pattern=this-should
32+
--let $log_expected_matches=1
33+
--source include/log_grep.inc
34+
35+
--source include/log_slow_cleanup.inc
36+
--enable_ps2_protocol

mysql-test/main/mysqld--help.result

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,14 @@ The following specify which files/extra groups are read (specified before remain
545545
Deprecated, will be removed in a future release. Please
546546
use --log-slow-filter instead.
547547
(Defaults to on; use --skip-log-slow-admin-statements to disable.)
548+
--log-slow-always-query-time=#
549+
Queries slower than log_slow_always_query_time are not
550+
affected by log_slow_rate_limit or
551+
log_slow_min_examined_row_limit. Query will be logged if
552+
execution time of the query is longer than
553+
log_slow_query_time and log_slow_always_query_time. The
554+
argument will be treated as a decimal value with
555+
microsecond precision
548556
--log-slow-disabled-statements=name
549557
Don't log certain types of statements to slow log. Any
550558
combination of: admin, call, slave, sp, or ALL to set all
@@ -576,7 +584,8 @@ The following specify which files/extra groups are read (specified before remain
576584
Log all queries that have taken more than
577585
log_slow_query_time seconds to execute to the slow query
578586
log file. The argument will be treated as a decimal value
579-
with microsecond precision
587+
with microsecond precision. Affected by
588+
log_slow_rate_limit and log_slow_min_examined_row_limit
580589
--log-slow-rate-limit=#
581590
Write to slow log every #th slow query. Set to 1 to log
582591
everything. Increase it to reduce the size of the slow or
@@ -598,10 +607,7 @@ The following specify which files/extra groups are read (specified before remain
598607
Log some non-critical warnings to the error log. Value
599608
can be between 0 and 11. Higher values mean more
600609
verbosity
601-
--long-query-time=# Alias for log_slow_query_time. Log all queries that have
602-
taken more than long_query_time seconds to execute to the
603-
slow query log file. The argument will be treated as a
604-
decimal value with microsecond precision
610+
--long-query-time=# Alias for log_slow_query_time
605611
--low-priority-updates
606612
INSERT/DELETE/UPDATE has lower priority than selects
607613
--lower-case-table-names[=#]
@@ -1768,6 +1774,7 @@ log-queries-not-using-indexes FALSE
17681774
log-short-format FALSE
17691775
log-slave-updates FALSE
17701776
log-slow-admin-statements TRUE
1777+
log-slow-always-query-time 3.1536e+07
17711778
log-slow-disabled-statements sp
17721779
log-slow-filter admin,filesort,filesort_on_disk,filesort_priority_queue,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk
17731780
log-slow-max-warnings 10

mysql-test/suite/sys_vars/r/sysvars_server_embedded.result

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,16 @@ NUMERIC_BLOCK_SIZE NULL
17321732
ENUM_VALUE_LIST OFF,ON
17331733
READ_ONLY NO
17341734
COMMAND_LINE_ARGUMENT OPTIONAL
1735+
VARIABLE_NAME LOG_SLOW_ALWAYS_QUERY_TIME
1736+
VARIABLE_SCOPE SESSION
1737+
VARIABLE_TYPE DOUBLE
1738+
VARIABLE_COMMENT Queries slower than log_slow_always_query_time are not affected by log_slow_rate_limit or log_slow_min_examined_row_limit. Query will be logged if execution time of the query is longer than log_slow_query_time and log_slow_always_query_time. The argument will be treated as a decimal value with microsecond precision
1739+
NUMERIC_MIN_VALUE 0
1740+
NUMERIC_MAX_VALUE 31536000
1741+
NUMERIC_BLOCK_SIZE NULL
1742+
ENUM_VALUE_LIST NULL
1743+
READ_ONLY NO
1744+
COMMAND_LINE_ARGUMENT REQUIRED
17351745
VARIABLE_NAME LOG_SLOW_DISABLED_STATEMENTS
17361746
VARIABLE_SCOPE SESSION
17371747
VARIABLE_TYPE SET
@@ -1795,7 +1805,7 @@ COMMAND_LINE_ARGUMENT REQUIRED
17951805
VARIABLE_NAME LOG_SLOW_QUERY_TIME
17961806
VARIABLE_SCOPE SESSION
17971807
VARIABLE_TYPE DOUBLE
1798-
VARIABLE_COMMENT Log all queries that have taken more than log_slow_query_time seconds to execute to the slow query log file. The argument will be treated as a decimal value with microsecond precision
1808+
VARIABLE_COMMENT Log all queries that have taken more than log_slow_query_time seconds to execute to the slow query log file. The argument will be treated as a decimal value with microsecond precision. Affected by log_slow_rate_limit and log_slow_min_examined_row_limit
17991809
NUMERIC_MIN_VALUE 0
18001810
NUMERIC_MAX_VALUE 31536000
18011811
NUMERIC_BLOCK_SIZE NULL
@@ -1845,7 +1855,7 @@ COMMAND_LINE_ARGUMENT OPTIONAL
18451855
VARIABLE_NAME LONG_QUERY_TIME
18461856
VARIABLE_SCOPE SESSION
18471857
VARIABLE_TYPE DOUBLE
1848-
VARIABLE_COMMENT Alias for log_slow_query_time. Log all queries that have taken more than long_query_time seconds to execute to the slow query log file. The argument will be treated as a decimal value with microsecond precision
1858+
VARIABLE_COMMENT Alias for log_slow_query_time
18491859
NUMERIC_MIN_VALUE 0
18501860
NUMERIC_MAX_VALUE 31536000
18511861
NUMERIC_BLOCK_SIZE NULL

mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,16 @@ NUMERIC_BLOCK_SIZE NULL
19221922
ENUM_VALUE_LIST OFF,ON
19231923
READ_ONLY NO
19241924
COMMAND_LINE_ARGUMENT OPTIONAL
1925+
VARIABLE_NAME LOG_SLOW_ALWAYS_QUERY_TIME
1926+
VARIABLE_SCOPE SESSION
1927+
VARIABLE_TYPE DOUBLE
1928+
VARIABLE_COMMENT Queries slower than log_slow_always_query_time are not affected by log_slow_rate_limit or log_slow_min_examined_row_limit. Query will be logged if execution time of the query is longer than log_slow_query_time and log_slow_always_query_time. The argument will be treated as a decimal value with microsecond precision
1929+
NUMERIC_MIN_VALUE 0
1930+
NUMERIC_MAX_VALUE 31536000
1931+
NUMERIC_BLOCK_SIZE NULL
1932+
ENUM_VALUE_LIST NULL
1933+
READ_ONLY NO
1934+
COMMAND_LINE_ARGUMENT REQUIRED
19251935
VARIABLE_NAME LOG_SLOW_DISABLED_STATEMENTS
19261936
VARIABLE_SCOPE SESSION
19271937
VARIABLE_TYPE SET
@@ -1985,7 +1995,7 @@ COMMAND_LINE_ARGUMENT REQUIRED
19851995
VARIABLE_NAME LOG_SLOW_QUERY_TIME
19861996
VARIABLE_SCOPE SESSION
19871997
VARIABLE_TYPE DOUBLE
1988-
VARIABLE_COMMENT Log all queries that have taken more than log_slow_query_time seconds to execute to the slow query log file. The argument will be treated as a decimal value with microsecond precision
1998+
VARIABLE_COMMENT Log all queries that have taken more than log_slow_query_time seconds to execute to the slow query log file. The argument will be treated as a decimal value with microsecond precision. Affected by log_slow_rate_limit and log_slow_min_examined_row_limit
19891999
NUMERIC_MIN_VALUE 0
19902000
NUMERIC_MAX_VALUE 31536000
19912001
NUMERIC_BLOCK_SIZE NULL
@@ -2035,7 +2045,7 @@ COMMAND_LINE_ARGUMENT OPTIONAL
20352045
VARIABLE_NAME LONG_QUERY_TIME
20362046
VARIABLE_SCOPE SESSION
20372047
VARIABLE_TYPE DOUBLE
2038-
VARIABLE_COMMENT Alias for log_slow_query_time. Log all queries that have taken more than long_query_time seconds to execute to the slow query log file. The argument will be treated as a decimal value with microsecond precision
2048+
VARIABLE_COMMENT Alias for log_slow_query_time
20392049
NUMERIC_MIN_VALUE 0
20402050
NUMERIC_MAX_VALUE 31536000
20412051
NUMERIC_BLOCK_SIZE NULL

sql/mysqld.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8941,10 +8941,12 @@ static int get_options(int *argc_ptr, char ***argv_ptr)
89418941
if (my_assert_on_error)
89428942
debug_assert_if_crashed_table= 1;
89438943

8944-
global_system_variables.long_query_time= (ulonglong)
8945-
(global_system_variables.long_query_time_double * 1e6 + 0.1);
8944+
global_system_variables.log_slow_query_time= (ulonglong)
8945+
(global_system_variables.log_slow_query_time_double * 1e6 + 0.1);
89468946
global_system_variables.max_statement_time= (ulonglong)
89478947
(global_system_variables.max_statement_time_double * 1e6 + 0.1);
8948+
global_system_variables.log_slow_always_query_time= (ulonglong)
8949+
(global_system_variables.log_slow_always_query_time_double * 1e6 + 0.1);
89488950

89498951
if (opt_short_log_format)
89508952
opt_specialflag|= SPECIAL_SHORT_LOG_FORMAT;

sql/sql_class.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ typedef struct system_variables
714714
ulonglong max_heap_table_size;
715715
ulonglong tmp_memory_table_size;
716716
ulonglong tmp_disk_table_size;
717-
ulonglong long_query_time;
717+
ulonglong log_slow_query_time;
718+
ulonglong log_slow_always_query_time;
718719
ulonglong max_statement_time;
719720
ulonglong optimizer_switch;
720721
ulonglong optimizer_trace;
@@ -743,7 +744,8 @@ typedef struct system_variables
743744
ulonglong max_tmp_space_usage;
744745

745746
double optimizer_where_cost, optimizer_scan_setup_cost;
746-
double long_query_time_double, max_statement_time_double;
747+
double log_slow_query_time_double, max_statement_time_double;
748+
double log_slow_always_query_time_double;
747749
double sample_percentage;
748750

749751
ha_rows select_limit;
@@ -4438,9 +4440,14 @@ class THD: public THD_count, /* this must be first */
44384440
void update_server_status()
44394441
{
44404442
set_time_for_next_stage();
4441-
if (utime_after_query >= utime_after_lock + variables.long_query_time)
4443+
if (utime_after_query >= utime_after_lock + variables.log_slow_query_time)
44424444
server_status|= SERVER_QUERY_WAS_SLOW;
44434445
}
4446+
/* True if query took longer than log_slow_always_query_time */
4447+
bool log_slow_always_query_time()
4448+
{
4449+
return (utime_after_query >= utime_after_lock + variables.log_slow_always_query_time);
4450+
}
44444451
inline ulonglong found_rows(void)
44454452
{
44464453
return limit_found_rows;

sql/sql_parse.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,8 @@ void log_slow_statement(THD *thd)
25522552
}
25532553

25542554
if ((thd->server_status & SERVER_QUERY_WAS_SLOW) &&
2555-
thd->get_examined_row_count() >= thd->variables.min_examined_row_limit)
2555+
(thd->get_examined_row_count() >= thd->variables.min_examined_row_limit ||
2556+
thd->log_slow_always_query_time()))
25562557
{
25572558
thd->status_var.long_query_count++;
25582559

@@ -2572,6 +2573,7 @@ void log_slow_statement(THD *thd)
25722573
this query to the log or not.
25732574
*/
25742575
if (thd->variables.log_slow_rate_limit > 1 &&
2576+
!thd->log_slow_always_query_time() &&
25752577
(global_query_id % thd->variables.log_slow_rate_limit) != 0)
25762578
goto end;
25772579

sql/sql_prepare.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5097,7 +5097,7 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor)
50975097
50985098
E.g., lets consider the following statements
50995099
SET slow_query_log= 1;
5100-
SET @@long_query_time=0.01;
5100+
SET @@log_slow_query_time=0.01;
51015101
PREPARE stmt FROM 'set statement slow_query_log=0 for select sleep(0.1)';
51025102
EXECUTE stmt;
51035103

0 commit comments

Comments
 (0)