Commit db1db8fa authored by Igor Babaev's avatar Igor Babaev

Fixed LP bug #1002146 (bug mdev-645).

If the setting of system variables does not allow to use join buffer
for a join query with GROUP BY <f1,...> / ORDER BY <f1,...> then
filesort is not needed if the first joined table is scanned in
the order compatible with order specified by the list <f1,...>.
parent ecf04668
...@@ -2111,4 +2111,45 @@ FROM t2 ...@@ -2111,4 +2111,45 @@ FROM t2
GROUP BY 1; GROUP BY 1;
a a
DROP TABLE t1, t2; DROP TABLE t1, t2;
#
# Bug #1002146: Unneeded filesort if usage of join buffer is not allowed
# (bug mdev-645)
#
CREATE TABLE t1 (pk int PRIMARY KEY, a int, INDEX idx(a));
INSERT INTO t1 VALUES (3,2), (2,3), (5,3), (6,4);
CREATE TABLE t2 (pk int PRIMARY KEY, a int, INDEX idx(a));
INSERT INTO t2 VALUES (9,0), (10,3), (6,4), (1,6), (3,100), (5,200);
set join_cache_level=0;
EXPLAIN
SELECT t2.a FROM t2 STRAIGHT_JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 range idx idx 5 NULL 5 Using where; Using index
1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using index
SELECT t2.a FROM t2 STRAIGHT_JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
a
3
4
100
200
set join_cache_level=default;
set @save_optimizer_switch=@@optimizer_switch;
set optimizer_switch='outer_join_with_cache=off';
EXPLAIN
SELECT t2.a FROM t2 LEFT JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 range idx idx 5 NULL 5 Using where; Using index
1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using where; Using index
SELECT t2.a FROM t2 LEFT JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
a
0
3
4
100
200
set optimizer_switch=@save_optimizer_switch;
DROP TABLE t1,t2;
# End of 5.3 tests # End of 5.3 tests
...@@ -2978,7 +2978,7 @@ EXPLAIN ...@@ -2978,7 +2978,7 @@ EXPLAIN
SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a) SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a)
GROUP BY a HAVING a != 'z'; GROUP BY a HAVING a != 'z';
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 t index idx_a idx_a 4 NULL 3 Using index; Using temporary; Using filesort 1 PRIMARY t index idx_a idx_a 4 NULL 1 Using index
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where
2 MATERIALIZED t1 ref idx_a idx_a 4 test.t2.b 2 Using index 2 MATERIALIZED t1 ref idx_a idx_a 4 test.t2.b 2 Using index
...@@ -2992,7 +2992,7 @@ EXPLAIN ...@@ -2992,7 +2992,7 @@ EXPLAIN
SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a) SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a)
GROUP BY a HAVING a != 'z'; GROUP BY a HAVING a != 'z';
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 t index idx_a idx_a 4 NULL 3 Using index; Using temporary; Using filesort 1 PRIMARY t index idx_a idx_a 4 NULL 1 Using index
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where
2 MATERIALIZED t1 ref idx_a idx_a 4 test.t2.b 2 Using index 2 MATERIALIZED t1 ref idx_a idx_a 4 test.t2.b 2 Using index
......
...@@ -1472,4 +1472,40 @@ WHERE a = ( ...@@ -1472,4 +1472,40 @@ WHERE a = (
GROUP BY 1; GROUP BY 1;
DROP TABLE t1, t2; DROP TABLE t1, t2;
--echo #
--echo # Bug #1002146: Unneeded filesort if usage of join buffer is not allowed
--echo # (bug mdev-645)
--echo #
CREATE TABLE t1 (pk int PRIMARY KEY, a int, INDEX idx(a));
INSERT INTO t1 VALUES (3,2), (2,3), (5,3), (6,4);
CREATE TABLE t2 (pk int PRIMARY KEY, a int, INDEX idx(a));
INSERT INTO t2 VALUES (9,0), (10,3), (6,4), (1,6), (3,100), (5,200);
set join_cache_level=0;
EXPLAIN
SELECT t2.a FROM t2 STRAIGHT_JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
SELECT t2.a FROM t2 STRAIGHT_JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
set join_cache_level=default;
set @save_optimizer_switch=@@optimizer_switch;
set optimizer_switch='outer_join_with_cache=off';
EXPLAIN
SELECT t2.a FROM t2 LEFT JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
SELECT t2.a FROM t2 LEFT JOIN t1 ON t2.a <> 0 WHERE t2.a <> 6
GROUP BY t2.a;
set optimizer_switch=@save_optimizer_switch;
DROP TABLE t1,t2;
--echo # End of 5.3 tests --echo # End of 5.3 tests
...@@ -7422,8 +7422,9 @@ get_best_combination(JOIN *join) ...@@ -7422,8 +7422,9 @@ get_best_combination(JOIN *join)
if ( !(keyuse= join->best_positions[tablenr].key)) if ( !(keyuse= join->best_positions[tablenr].key))
{ {
j->type=JT_ALL; j->type=JT_ALL;
if (tablenr != join->const_tables) if (join->best_positions[tablenr].use_join_buffer &&
join->full_join=1; tablenr != join->const_tables)
join->full_join= 1;
} }
/*if (join->best_positions[tablenr].sj_strategy == SJ_OPT_LOOSE_SCAN) /*if (join->best_positions[tablenr].sj_strategy == SJ_OPT_LOOSE_SCAN)
...@@ -8436,7 +8437,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -8436,7 +8437,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
We will use join cache here : prevent sorting of the first We will use join cache here : prevent sorting of the first
table only and sort at the end. table only and sort at the end.
*/ */
if (i != join->const_tables && join->table_count > join->const_tables + 1) if (i != join->const_tables &&
join->table_count > join->const_tables + 1 &&
join->best_positions[i].use_join_buffer)
join->full_join= 1; join->full_join= 1;
} }
......
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