Skip to content

Commit 56ce9e7

Browse files
MDEV-35353: write rows_examined for union_all queries
For all "UNION ALL" queries, the field rows_examined in the slow query log is always being set to 0. However, this is not the case with UNION queries although both UNION, and UNION ALL share the same code. The problem is that for UNION ALL queries, rows_examined field in the thd object is not updated to the actual processed rows, although they are being tracked in the sql_union.cc Later, when the thd object is used to dump the examined rows to the slow query log, it was only writing 0, as thd object was never updated with the processed rows count. This PR addresses the concern to write rows_examined field correctly for UNION_ALL queries to the slow query log.
1 parent 96045fb commit 56ce9e7

File tree

3 files changed

+158
-2
lines changed

3 files changed

+158
-2
lines changed

mysql-test/main/log_state.result

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,86 @@ disconnect con2;
313313
disconnect con1;
314314
connection default;
315315
drop procedure p1;
316+
#
317+
# MDEV-35353 Rows_examined is always 0 in the slow query log
318+
# for union all queries
319+
#
320+
SET GLOBAL log_output = "TABLE";
321+
SET GLOBAL slow_query_log = ON;
322+
SET GLOBAL long_query_time = 0.0;
323+
TRUNCATE TABLE mysql.slow_log;
324+
create table t1(a int, b int);
325+
insert into t1 select seq, seq from seq_1_to_20;
326+
connect con2,localhost,root,,;
327+
select sum(a) from t1
328+
union all
329+
select sum(b) from t1;
330+
sum(a)
331+
210
332+
210
333+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%union all%';
334+
rows_examined sql_text
335+
40 select sum(a) from t1
336+
union all
337+
select sum(b) from t1
338+
TRUNCATE TABLE mysql.slow_log;
339+
select sum(a) from t1
340+
union
341+
select sum(b) from t1;
342+
sum(a)
343+
210
344+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%union%';
345+
rows_examined sql_text
346+
41 select sum(a) from t1
347+
union
348+
select sum(b) from t1
349+
TRUNCATE TABLE mysql.slow_log;
350+
select sum(a) from t1
351+
except all
352+
select sum(b) from t1;
353+
sum(a)
354+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%except all%';
355+
rows_examined sql_text
356+
40 select sum(a) from t1
357+
except all
358+
select sum(b) from t1
359+
TRUNCATE TABLE mysql.slow_log;
360+
select sum(a) from t1
361+
except
362+
select sum(b) from t1;
363+
sum(a)
364+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%except%';
365+
rows_examined sql_text
366+
40 select sum(a) from t1
367+
except
368+
select sum(b) from t1
369+
TRUNCATE TABLE mysql.slow_log;
370+
select sum(a) from t1
371+
intersect all
372+
select sum(b) from t1;
373+
sum(a)
374+
210
375+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%intersect all%';
376+
rows_examined sql_text
377+
41 select sum(a) from t1
378+
intersect all
379+
select sum(b) from t1
380+
TRUNCATE TABLE mysql.slow_log;
381+
select sum(a) from t1
382+
intersect
383+
select sum(b) from t1;
384+
sum(a)
385+
210
386+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%intersect%';
387+
rows_examined sql_text
388+
41 select sum(a) from t1
389+
intersect
390+
select sum(b) from t1
391+
disconnect con2;
392+
connection default;
393+
DROP TABLE t1;
394+
TRUNCATE TABLE mysql.slow_log;
395+
End of 10.11 tests
316396
SET GLOBAL long_query_time = @save_long_query_time;
317397
SET GLOBAL log_output = @old_log_output;
318398
SET global general_log = @old_general_log;

mysql-test/main/log_state.test

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
--source include/not_embedded.inc
55
--source include/have_csv.inc
6+
--source include/have_sequence.inc
67

78
call mtr.add_suppression("options .* --log_slow_queries is not set");
89

@@ -370,6 +371,79 @@ disconnect con1;
370371
connection default;
371372
drop procedure p1;
372373

374+
###########################################################################
375+
376+
--echo #
377+
--echo # MDEV-35353 Rows_examined is always 0 in the slow query log
378+
--echo # for union all queries
379+
--echo #
380+
381+
SET GLOBAL log_output = "TABLE";
382+
SET GLOBAL slow_query_log = ON;
383+
SET GLOBAL long_query_time = 0.0;
384+
385+
# clear slow_log of any residual slow queries
386+
TRUNCATE TABLE mysql.slow_log;
387+
create table t1(a int, b int);
388+
insert into t1 select seq, seq from seq_1_to_20;
389+
390+
connect (con2,localhost,root,,);
391+
--disable_ps_protocol
392+
select sum(a) from t1
393+
union all
394+
select sum(b) from t1;
395+
396+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%union all%';
397+
398+
TRUNCATE TABLE mysql.slow_log;
399+
400+
select sum(a) from t1
401+
union
402+
select sum(b) from t1;
403+
404+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%union%';
405+
406+
TRUNCATE TABLE mysql.slow_log;
407+
408+
select sum(a) from t1
409+
except all
410+
select sum(b) from t1;
411+
412+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%except all%';
413+
414+
TRUNCATE TABLE mysql.slow_log;
415+
416+
select sum(a) from t1
417+
except
418+
select sum(b) from t1;
419+
420+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%except%';
421+
422+
TRUNCATE TABLE mysql.slow_log;
423+
424+
select sum(a) from t1
425+
intersect all
426+
select sum(b) from t1;
427+
428+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%intersect all%';
429+
430+
TRUNCATE TABLE mysql.slow_log;
431+
432+
select sum(a) from t1
433+
intersect
434+
select sum(b) from t1;
435+
436+
SELECT rows_examined,sql_text FROM mysql.slow_log where sql_text LIKE '%intersect%';
437+
438+
disconnect con2;
439+
connection default;
440+
DROP TABLE t1;
441+
442+
TRUNCATE TABLE mysql.slow_log;
443+
444+
--echo End of 10.11 tests
445+
###########################################################################
446+
373447
# Reset global system variables to initial values if forgotten somewhere above.
374448
SET GLOBAL long_query_time = @save_long_query_time;
375449
SET GLOBAL log_output = @old_log_output;

sql/sql_union.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,8 +2436,8 @@ bool st_select_lex_unit::exec()
24362436
fake_select_lex->table_list.empty();
24372437
if (likely(!saved_error))
24382438
{
2439-
thd->limit_found_rows = (ulonglong)table->file->stats.records + add_rows;
2440-
thd->inc_examined_row_count(examined_rows);
2439+
thd->limit_found_rows=
2440+
(ulonglong) table->file->stats.records + add_rows;
24412441
}
24422442
/*
24432443
Mark for slow query log if any of the union parts didn't use
@@ -2448,6 +2448,8 @@ bool st_select_lex_unit::exec()
24482448
thd->lex->current_select= lex_select_save;
24492449
err:
24502450
thd->lex->set_limit_rows_examined();
2451+
if (likely(!saved_error))
2452+
thd->inc_examined_row_count(examined_rows);
24512453
DBUG_RETURN(saved_error);
24522454
}
24532455

0 commit comments

Comments
 (0)