Skip to content

Commit 677f1ef

Browse files
committed
MDEV-25682 Explain shows an execution plan different from actually executed
If a select query contained an ORDER BY clause that followed a LIMIT clause or an ORDER BY clause or ORDER BY with LIMIT the EXPLAIN output for the query showed an execution plan different from that was actually executed. Approved by Roman Nozdrin <roman.nozdrin@mariadb.com>
1 parent e607f33 commit 677f1ef

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

mysql-test/r/order_by.result

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,4 +3460,29 @@ SET max_length_for_sort_data=@save_max_length_for_sort_data;
34603460
SET max_sort_length= @save_max_sort_length;
34613461
SET sql_select_limit= @save_sql_select_limit;
34623462
DROP TABLE t1;
3463+
#
3464+
# MDEV-25682: EXPLAIN for SELECT with ORDER BY after [ORDER BY] LIMIT
3465+
#
3466+
create table t1 (a int);
3467+
insert into t1 values (3), (7), (1);
3468+
explain (select a from t1 limit 2) order by a desc;
3469+
id select_type table type possible_keys key key_len ref rows Extra
3470+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3
3471+
NULL UNION RESULT <union1> ALL NULL NULL NULL NULL NULL Using filesort
3472+
(select a from t1 limit 2) order by a desc;
3473+
a
3474+
7
3475+
3
3476+
create table t2 (a int, b int);
3477+
insert into t2 values (3,70), (7,10), (1,40), (4,30);
3478+
explain (select b,a from t2 order by a limit 3) order by b desc;
3479+
id select_type table type possible_keys key key_len ref rows Extra
3480+
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using filesort
3481+
NULL UNION RESULT <union1> ALL NULL NULL NULL NULL NULL Using filesort
3482+
(select b,a from t2 order by a limit 3) order by b desc;
3483+
b a
3484+
70 3
3485+
40 1
3486+
30 4
3487+
drop table t1,t2;
34633488
# End of 10.2 tests

mysql-test/t/order_by.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,4 +2293,20 @@ SET max_sort_length= @save_max_sort_length;
22932293
SET sql_select_limit= @save_sql_select_limit;
22942294
DROP TABLE t1;
22952295

2296+
--echo #
2297+
--echo # MDEV-25682: EXPLAIN for SELECT with ORDER BY after [ORDER BY] LIMIT
2298+
--echo #
2299+
2300+
create table t1 (a int);
2301+
insert into t1 values (3), (7), (1);
2302+
explain (select a from t1 limit 2) order by a desc;
2303+
(select a from t1 limit 2) order by a desc;
2304+
2305+
create table t2 (a int, b int);
2306+
insert into t2 values (3,70), (7,10), (1,40), (4,30);
2307+
explain (select b,a from t2 order by a limit 3) order by b desc;
2308+
(select b,a from t2 order by a limit 3) order by b desc;
2309+
2310+
drop table t1,t2;
2311+
22962312
--echo # End of 10.2 tests

sql/sql_select.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25332,7 +25332,7 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
2533225332
sl->options|= SELECT_DESCRIBE;
2533325333
}
2533425334

25335-
if (unit->is_union())
25335+
if (unit->is_union() || unit->fake_select_lex)
2533625336
{
2533725337
if (unit->union_needs_tmp_table() && unit->fake_select_lex)
2533825338
{

0 commit comments

Comments
 (0)