diff --git a/mysql-test/main/order_by.result b/mysql-test/main/order_by.result index 2aab272eca2..9d83805c9c3 100644 --- a/mysql-test/main/order_by.result +++ b/mysql-test/main/order_by.result @@ -3446,7 +3446,7 @@ CREATE TABLE t2 SELECT * FROM t1; EXPLAIN SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.b ORDER BY t1.b LIMIT 1) AS c FROM t2; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 -2 DEPENDENT SUBQUERY t1 index PRIMARY b 5 NULL 1 Using where +2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using where SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.b ORDER BY t1.b LIMIT 1) AS c FROM t2; c 1 @@ -3706,4 +3706,23 @@ drop table t1,t2,t3,t4; SELECT 1.000000 two UNION SELECT 1 ORDER BY ( SELECT two LIMIT 1 OFFSET 1 ) ; two 1.000000 +# MDEV-32475: test_if_skip_sort_order() should catch the join types +# JT_EQ_REF, JT_CONST and JT_SYSTEM and skip sort order for these +# +CREATE TABLE t1 (a INT PRIMARY KEY, b INT, KEY(b)); +CREATE TABLE t2 (a INT, b INT); +INSERT INTO t1 SELECT seq, seq+1 FROM seq_1_to_100; +INSERT INTO t2 VALUES (0, 1),(1, 2); +ANALYZE TABLE t1, t2 PERSISTENT FOR ALL; +Table Op Msg_type Msg_text +test.t1 analyze status Engine-independent statistics collected +test.t1 analyze status Table is already up to date +test.t2 analyze status Engine-independent statistics collected +test.t2 analyze status OK +# Table t1 must use eq_ref, not index below: +EXPLAIN SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.b ORDER BY t1.b LIMIT 1) AS c FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 2 +2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using where +DROP TABLE t1,t2; # End of 10.4 tests diff --git a/mysql-test/main/order_by.test b/mysql-test/main/order_by.test index 9b63cbf64f0..ec10375e57c 100644 --- a/mysql-test/main/order_by.test +++ b/mysql-test/main/order_by.test @@ -2452,4 +2452,20 @@ drop table t1,t2,t3,t4; --echo # SELECT 1.000000 two UNION SELECT 1 ORDER BY ( SELECT two LIMIT 1 OFFSET 1 ) ; +--echo # MDEV-32475: test_if_skip_sort_order() should catch the join types +--echo # JT_EQ_REF, JT_CONST and JT_SYSTEM and skip sort order for these +--echo # + +CREATE TABLE t1 (a INT PRIMARY KEY, b INT, KEY(b)); +CREATE TABLE t2 (a INT, b INT); +INSERT INTO t1 SELECT seq, seq+1 FROM seq_1_to_100; +INSERT INTO t2 VALUES (0, 1),(1, 2); + +ANALYZE TABLE t1, t2 PERSISTENT FOR ALL; + +--echo # Table t1 must use eq_ref, not index below: +EXPLAIN SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.b ORDER BY t1.b LIMIT 1) AS c FROM t2; + +DROP TABLE t1,t2; + --echo # End of 10.4 tests diff --git a/sql/sql_select.cc b/sql/sql_select.cc index d7badbc59a5..f3ebf830301 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -23613,6 +23613,14 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, /* Check that we are always called with first non-const table */ DBUG_ASSERT(tab == tab->join->join_tab + tab->join->const_tables); + /* Sorting a single row can always be skipped */ + if (tab->type == JT_EQ_REF || + tab->type == JT_CONST || + tab->type == JT_SYSTEM) + { + DBUG_RETURN(1); + } + /* Keys disabled by ALTER TABLE ... DISABLE KEYS should have already been taken into account.