Commit 680f732f authored by Oleg Smirnov's avatar Oleg Smirnov

MDEV-32475: Skip sorting if we will read one row

test_if_skip_sort_order() should catch the join types JT_EQ_REF,
JT_CONST and JT_SYSTEM and skip sort order for these.

Such join types imply retrieving of a single row of data, and sorting
of a single row can always be skipped.
parent fefea242
...@@ -3446,7 +3446,7 @@ CREATE TABLE t2 SELECT * FROM t1; ...@@ -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; 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 id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 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; SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.b ORDER BY t1.b LIMIT 1) AS c FROM t2;
c c
1 1
...@@ -3706,4 +3706,23 @@ drop table t1,t2,t3,t4; ...@@ -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 ) ; SELECT 1.000000 two UNION SELECT 1 ORDER BY ( SELECT two LIMIT 1 OFFSET 1 ) ;
two two
1.000000 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 # End of 10.4 tests
...@@ -2452,4 +2452,20 @@ drop table t1,t2,t3,t4; ...@@ -2452,4 +2452,20 @@ drop table t1,t2,t3,t4;
--echo # --echo #
SELECT 1.000000 two UNION SELECT 1 ORDER BY ( SELECT two LIMIT 1 OFFSET 1 ) ; 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 --echo # End of 10.4 tests
...@@ -23613,6 +23613,14 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -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 */ /* Check that we are always called with first non-const table */
DBUG_ASSERT(tab == tab->join->join_tab + tab->join->const_tables); 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 Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
been taken into account. been taken into account.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment