Commit 66350180 authored by Igor Babaev's avatar Igor Babaev

Fixed LP bug #664508.

When join buffers are employed no index scan for the first
table with grouping columns can be used.



mysql-test/r/join_cache.result:
  Added a test case for bug #664508.
  Sorted results for some other test cases.
mysql-test/t/join_cache.test:
  Added a test case for bug #664508.
  Sorted results for some other test cases.
parent 18eb0bf1
...@@ -4035,52 +4035,52 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -4035,52 +4035,52 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT City.Name, Country.Name FROM City,Country SELECT City.Name, Country.Name FROM City,Country
WHERE City.Country=Country.Code AND City.Population > 3000000; WHERE City.Country=Country.Code AND City.Population > 3000000;
Name Name Name Name
Sydney Australia
Dhaka Bangladesh
Rio de Janeiro Brazil
São Paulo Brazil
London United Kingdom
Santiago de Chile Chile
Alexandria Egypt Alexandria Egypt
Ankara Turkey
Baghdad Iraq
Bangkok Thailand
Berlin Germany
Cairo Egypt Cairo Egypt
Jakarta Indonesia
Delhi India
Calcutta [Kolkata] India Calcutta [Kolkata] India
Chengdu China
Chennai (Madras) India Chennai (Madras) India
Mumbai (Bombay) India
Baghdad Iraq
Teheran Iran
Tokyo Japan
Jokohama [Yokohama] Japan
Peking China
Chongqing China Chongqing China
Tianjin China Ciudad de México Mexico
Wuhan China Delhi India
Dhaka Bangladesh
Harbin China Harbin China
Shenyang China Ho Chi Minh City Vietnam
Shanghai China Istanbul Turkey
Chengdu China Jakarta Indonesia
Jokohama [Yokohama] Japan
Kanton [Guangzhou] China Kanton [Guangzhou] China
Santafé de Bogotá Colombia Karachi Pakistan
Kinshasa Congo, The Democratic Republic of the Kinshasa Congo, The Democratic Republic of the
Pusan South Korea
Seoul South Korea
Ciudad de México Mexico
Rangoon (Yangon) Myanmar
Lahore Pakistan Lahore Pakistan
Karachi Pakistan
Lima Peru Lima Peru
Berlin Germany London United Kingdom
Los Angeles United States
Moscow Russian Federation
Mumbai (Bombay) India
New York United States
Peking China
Pusan South Korea
Rangoon (Yangon) Myanmar
Rio de Janeiro Brazil
Riyadh Saudi Arabia Riyadh Saudi Arabia
Santafé de Bogotá Colombia
Santiago de Chile Chile
Seoul South Korea
Shanghai China
Shenyang China
Singapore Singapore Singapore Singapore
Bangkok Thailand
Istanbul Turkey
Ankara Turkey
St Petersburg Russian Federation St Petersburg Russian Federation
Moscow Russian Federation Sydney Australia
Ho Chi Minh City Vietnam São Paulo Brazil
Los Angeles United States Teheran Iran
New York United States Tianjin China
Tokyo Japan
Wuhan China
set join_buffer_size=default; set join_buffer_size=default;
set join_cache_level=6; set join_cache_level=6;
ALTER TABLE Country MODIFY Name varchar(52) NOT NULL default ''; ALTER TABLE Country MODIFY Name varchar(52) NOT NULL default '';
...@@ -5411,55 +5411,135 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -5411,55 +5411,135 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT t1.i, t1.d, t1.v, t2.i, t2.d, t2.t, t2.v FROM t1,t2,t3 SELECT t1.i, t1.d, t1.v, t2.i, t2.d, t2.t, t2.v FROM t1,t2,t3
WHERE t3.u <='a' AND t2.j < 5 AND t3.v = t2.u; WHERE t3.u <='a' AND t2.j < 5 AND t3.v = t2.u;
i d v i d t v i d v i d t v
3 2008-12-04 v 5 1900-01-01 22:45:53 a 0 2008-06-04 p 5 1900-01-01 22:45:53 a
3 2009-03-28 f 5 1900-01-01 22:45:53 a 0 2008-06-04 p 5 1900-01-01 22:45:53 a
3 1900-01-01 v 5 1900-01-01 22:45:53 a 0 2008-06-04 p 5 1900-01-01 22:45:53 a
1 1900-01-01 a 5 1900-01-01 22:45:53 a
1 1900-01-01 a 5 1900-01-01 22:45:53 a
1 1900-01-01 a 5 1900-01-01 22:45:53 a
1 2002-07-13 v 5 1900-01-01 22:45:53 a
1 2002-07-13 v 5 1900-01-01 22:45:53 a
1 2002-07-13 v 5 1900-01-01 22:45:53 a
1 2006-04-02 z 5 1900-01-01 22:45:53 a
1 2006-04-02 z 5 1900-01-01 22:45:53 a
1 2006-04-02 z 5 1900-01-01 22:45:53 a
2 1900-01-01 v 5 1900-01-01 22:45:53 a
2 1900-01-01 v 5 1900-01-01 22:45:53 a
2 1900-01-01 v 5 1900-01-01 22:45:53 a
2 2009-10-02 s 5 1900-01-01 22:45:53 a 2 2009-10-02 s 5 1900-01-01 22:45:53 a
3 2008-12-04 v 5 1900-01-01 22:45:53 a
3 2009-03-28 f 5 1900-01-01 22:45:53 a
3 1900-01-01 v 5 1900-01-01 22:45:53 a
2 2009-10-02 s 5 1900-01-01 22:45:53 a 2 2009-10-02 s 5 1900-01-01 22:45:53 a
2 2009-10-02 s 5 1900-01-01 22:45:53 a
3 1900-01-01 v 5 1900-01-01 22:45:53 a
3 1900-01-01 v 5 1900-01-01 22:45:53 a
3 1900-01-01 v 5 1900-01-01 22:45:53 a
3 2006-10-19 n 5 1900-01-01 22:45:53 a
3 2006-10-19 n 5 1900-01-01 22:45:53 a
3 2006-10-19 n 5 1900-01-01 22:45:53 a
3 2008-12-04 v 5 1900-01-01 22:45:53 a
3 2008-12-04 v 5 1900-01-01 22:45:53 a
3 2008-12-04 v 5 1900-01-01 22:45:53 a 3 2008-12-04 v 5 1900-01-01 22:45:53 a
3 2009-03-28 f 5 1900-01-01 22:45:53 a 3 2009-03-28 f 5 1900-01-01 22:45:53 a
3 1900-01-01 v 5 1900-01-01 22:45:53 a 3 2009-03-28 f 5 1900-01-01 22:45:53 a
2 2009-10-02 s 5 1900-01-01 22:45:53 a 3 2009-03-28 f 5 1900-01-01 22:45:53 a
1 1900-01-01 a 5 1900-01-01 22:45:53 a 4 2006-10-14 v 5 1900-01-01 22:45:53 a
0 2008-06-04 p 5 1900-01-01 22:45:53 a 4 2006-10-14 v 5 1900-01-01 22:45:53 a
8 2009-01-13 z 5 1900-01-01 22:45:53 a 4 2006-10-14 v 5 1900-01-01 22:45:53 a
5 1900-01-01 a 5 1900-01-01 22:45:53 a 5 1900-01-01 a 5 1900-01-01 22:45:53 a
1 1900-01-01 a 5 1900-01-01 22:45:53 a
0 2008-06-04 p 5 1900-01-01 22:45:53 a
8 2009-01-13 z 5 1900-01-01 22:45:53 a
5 1900-01-01 a 5 1900-01-01 22:45:53 a 5 1900-01-01 a 5 1900-01-01 22:45:53 a
1 1900-01-01 a 5 1900-01-01 22:45:53 a
0 2008-06-04 p 5 1900-01-01 22:45:53 a
8 2009-01-13 z 5 1900-01-01 22:45:53 a
5 1900-01-01 a 5 1900-01-01 22:45:53 a 5 1900-01-01 a 5 1900-01-01 22:45:53 a
5 2004-09-18 h 5 1900-01-01 22:45:53 a 5 2004-09-18 h 5 1900-01-01 22:45:53 a
4 2006-10-14 v 5 1900-01-01 22:45:53 a
2 1900-01-01 v 5 1900-01-01 22:45:53 a
5 2008-04-04 y 5 1900-01-01 22:45:53 a
5 2004-09-18 h 5 1900-01-01 22:45:53 a 5 2004-09-18 h 5 1900-01-01 22:45:53 a
4 2006-10-14 v 5 1900-01-01 22:45:53 a
2 1900-01-01 v 5 1900-01-01 22:45:53 a
5 2008-04-04 y 5 1900-01-01 22:45:53 a
5 2004-09-18 h 5 1900-01-01 22:45:53 a 5 2004-09-18 h 5 1900-01-01 22:45:53 a
4 2006-10-14 v 5 1900-01-01 22:45:53 a
2 1900-01-01 v 5 1900-01-01 22:45:53 a
5 2008-04-04 y 5 1900-01-01 22:45:53 a 5 2008-04-04 y 5 1900-01-01 22:45:53 a
1 2002-07-13 v 5 1900-01-01 22:45:53 a 5 2008-04-04 y 5 1900-01-01 22:45:53 a
1 2006-04-02 z 5 1900-01-01 22:45:53 a 5 2008-04-04 y 5 1900-01-01 22:45:53 a
3 2006-10-19 n 5 1900-01-01 22:45:53 a
8 2005-06-08 d 5 1900-01-01 22:45:53 a 8 2005-06-08 d 5 1900-01-01 22:45:53 a
1 2002-07-13 v 5 1900-01-01 22:45:53 a
1 2006-04-02 z 5 1900-01-01 22:45:53 a
3 2006-10-19 n 5 1900-01-01 22:45:53 a
8 2005-06-08 d 5 1900-01-01 22:45:53 a 8 2005-06-08 d 5 1900-01-01 22:45:53 a
1 2002-07-13 v 5 1900-01-01 22:45:53 a
1 2006-04-02 z 5 1900-01-01 22:45:53 a
3 2006-10-19 n 5 1900-01-01 22:45:53 a
8 2005-06-08 d 5 1900-01-01 22:45:53 a 8 2005-06-08 d 5 1900-01-01 22:45:53 a
8 2009-01-13 z 5 1900-01-01 22:45:53 a
8 2009-01-13 z 5 1900-01-01 22:45:53 a
8 2009-01-13 z 5 1900-01-01 22:45:53 a
DROP TABLE t1,t2,t3; DROP TABLE t1,t2,t3;
SET SESSION join_cache_level=DEFAULT; SET SESSION join_cache_level=DEFAULT;
SET SESSION join_buffer_size=DEFAULT; SET SESSION join_buffer_size=DEFAULT;
#
# Bug #664508: 'Simple' GROUP BY + ORDER BY
# when join buffers are used
#
CREATE TABLE t1 (
pk int NOT NULL, i int NOT NULL, v varchar(1) NOT NULL,
PRIMARY KEY (pk), INDEX idx1(i), INDEX idx2 (v,i)
);
INSERT INTO t1 VALUES
(10,8,'v'), (11,8,'f'), (12,5,'v'), (13,8,'s'), (14,8,'a'),
(15,6,'p'), (16,7,'z'), (17,2,'a'), (18,5,'h'), (19,7,'h'),
(25,3,'m'), (26,5,'z'), (27,9,'n'), (28,1,'d'), (29,107,'a');
CREATE TABLE t2 (
pk int NOT NULL, i int NOT NULL, v varchar(1) NOT NULL,
PRIMARY KEY (pk), INDEX idx1(i), INDEX idx2(v,i)
);
INSERT INTO t2 VALUES
(10,8,'v'), (11,8,'f'), (12,5,'v'), (13,8,'s'), (14,8,'a'),
(15,6,'p'), (16,7,'z'), (17,2,'a'), (18,5,'h'), (19,7,'h'),
(20,2,'v'), (21,9,'v'), (22,142,'b'), (23,3,'y'), (24,0,'v'),
(25,3,'m'), (26,5,'z'), (27,9,'n'), (28,1,'d'), (29,107,'a');
CREATE TABLE t3 (
pk int NOT NULL, i int(11) NOT NULL, v varchar(1) NOT NULL,
PRIMARY KEY (pk), INDEX idx1(i), INDEX idx2(v,i)
);
INSERT INTO t3 VALUES
(1,9,'x'), (2,5,'g'), (3,1,'o'), (4,0,'g'), (5,1,'v'),
(6,190,'m'), (7,6,'x'), (8,3,'c'), (9,4,'z'), (10,3,'i'),
(11,186,'x'), (12,1,'g'), (13,8,'q'), (14,226,'m'), (15,133,'p'),
(16,6,'e'), (17,3,'t'), (18,8,'j'), (19,5,'h'), (20,7,'w');
SET SESSION join_cache_level=1;
EXPLAIN
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index idx1 idx2 7 NULL 10 Using index; Using temporary; Using filesort
1 SIMPLE t3 eq_ref PRIMARY,idx2 PRIMARY 4 test.t2.i 1 Using where
1 SIMPLE t1 ref idx2 idx2 3 test.t3.v 2
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
v
h
z
p
n
v
SET SESSION join_cache_level=6;
EXPLAIN
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL idx1 NULL NULL NULL 20 Using temporary; Using filesort
1 SIMPLE t3 eq_ref PRIMARY,idx2 PRIMARY 4 test.t2.i 1 Using where; Using join buffer (flat, BKA join)
1 SIMPLE t1 ref idx2 idx2 3 test.t3.v 2 Using join buffer (incremental, BKA join)
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
v
h
z
n
v
p
SET SESSION join_cache_level=4;
EXPLAIN
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL idx1 NULL NULL NULL 20 Using temporary; Using filesort
1 SIMPLE t3 eq_ref PRIMARY,idx2 PRIMARY 4 test.t2.i 1 Using where; Using join buffer (flat, BNLH join)
1 SIMPLE t1 ref idx2 idx2 3 test.t3.v 2 Using join buffer (incremental, BNLH join)
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
v
h
z
n
v
p
DROP TABLE t1,t2,t3;
SET SESSION join_cache_level=DEFAULT;
set @@optimizer_switch=@save_optimizer_switch; set @@optimizer_switch=@save_optimizer_switch;
...@@ -699,6 +699,7 @@ EXPLAIN ...@@ -699,6 +699,7 @@ EXPLAIN
SELECT City.Name, Country.Name FROM City,Country SELECT City.Name, Country.Name FROM City,Country
WHERE City.Country=Country.Code AND City.Population > 3000000; WHERE City.Country=Country.Code AND City.Population > 3000000;
--sorted_result
SELECT City.Name, Country.Name FROM City,Country SELECT City.Name, Country.Name FROM City,Country
WHERE City.Country=Country.Code AND City.Population > 3000000; WHERE City.Country=Country.Code AND City.Population > 3000000;
...@@ -2173,6 +2174,7 @@ EXPLAIN ...@@ -2173,6 +2174,7 @@ EXPLAIN
SELECT t1.i, t1.d, t1.v, t2.i, t2.d, t2.t, t2.v FROM t1,t2,t3 SELECT t1.i, t1.d, t1.v, t2.i, t2.d, t2.t, t2.v FROM t1,t2,t3
WHERE t3.u <='a' AND t2.j < 5 AND t3.v = t2.u; WHERE t3.u <='a' AND t2.j < 5 AND t3.v = t2.u;
--sorted_result
SELECT t1.i, t1.d, t1.v, t2.i, t2.d, t2.t, t2.v FROM t1,t2,t3 SELECT t1.i, t1.d, t1.v, t2.i, t2.d, t2.t, t2.v FROM t1,t2,t3
WHERE t3.u <='a' AND t2.j < 5 AND t3.v = t2.u; WHERE t3.u <='a' AND t2.j < 5 AND t3.v = t2.u;
...@@ -2181,5 +2183,65 @@ DROP TABLE t1,t2,t3; ...@@ -2181,5 +2183,65 @@ DROP TABLE t1,t2,t3;
SET SESSION join_cache_level=DEFAULT; SET SESSION join_cache_level=DEFAULT;
SET SESSION join_buffer_size=DEFAULT; SET SESSION join_buffer_size=DEFAULT;
--echo #
--echo # Bug #664508: 'Simple' GROUP BY + ORDER BY
--echo # when join buffers are used
--echo #
CREATE TABLE t1 (
pk int NOT NULL, i int NOT NULL, v varchar(1) NOT NULL,
PRIMARY KEY (pk), INDEX idx1(i), INDEX idx2 (v,i)
);
INSERT INTO t1 VALUES
(10,8,'v'), (11,8,'f'), (12,5,'v'), (13,8,'s'), (14,8,'a'),
(15,6,'p'), (16,7,'z'), (17,2,'a'), (18,5,'h'), (19,7,'h'),
(25,3,'m'), (26,5,'z'), (27,9,'n'), (28,1,'d'), (29,107,'a');
CREATE TABLE t2 (
pk int NOT NULL, i int NOT NULL, v varchar(1) NOT NULL,
PRIMARY KEY (pk), INDEX idx1(i), INDEX idx2(v,i)
);
INSERT INTO t2 VALUES
(10,8,'v'), (11,8,'f'), (12,5,'v'), (13,8,'s'), (14,8,'a'),
(15,6,'p'), (16,7,'z'), (17,2,'a'), (18,5,'h'), (19,7,'h'),
(20,2,'v'), (21,9,'v'), (22,142,'b'), (23,3,'y'), (24,0,'v'),
(25,3,'m'), (26,5,'z'), (27,9,'n'), (28,1,'d'), (29,107,'a');
CREATE TABLE t3 (
pk int NOT NULL, i int(11) NOT NULL, v varchar(1) NOT NULL,
PRIMARY KEY (pk), INDEX idx1(i), INDEX idx2(v,i)
);
INSERT INTO t3 VALUES
(1,9,'x'), (2,5,'g'), (3,1,'o'), (4,0,'g'), (5,1,'v'),
(6,190,'m'), (7,6,'x'), (8,3,'c'), (9,4,'z'), (10,3,'i'),
(11,186,'x'), (12,1,'g'), (13,8,'q'), (14,226,'m'), (15,133,'p'),
(16,6,'e'), (17,3,'t'), (18,8,'j'), (19,5,'h'), (20,7,'w');
SET SESSION join_cache_level=1;
EXPLAIN
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SET SESSION join_cache_level=6;
EXPLAIN
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SET SESSION join_cache_level=4;
EXPLAIN
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SELECT t2.v FROM t1, t2, t3 WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v
GROUP BY t2.v ORDER BY t1.pk,t2.v;
DROP TABLE t1,t2,t3;
SET SESSION join_cache_level=DEFAULT;
# this must be the last command in the file # this must be the last command in the file
set @@optimizer_switch=@save_optimizer_switch; set @@optimizer_switch=@save_optimizer_switch;
...@@ -7980,8 +7980,11 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after) ...@@ -7980,8 +7980,11 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
JOIN_TAB *tab=join->join_tab+i; JOIN_TAB *tab=join->join_tab+i;
if (tab->use_join_cache) if (tab->use_join_cache)
{ {
JOIN_TAB *sort_by_tab= join->get_sort_by_join_tab(); JOIN_TAB *sort_by_tab= join->group && join->simple_group &&
if (sort_by_tab && !join->need_tmp) join->group_list ?
join->join_tab+join->const_tables :
join->get_sort_by_join_tab();
if (sort_by_tab)
{ {
join->need_tmp= 1; join->need_tmp= 1;
join->simple_order= join->simple_group= 0; join->simple_order= join->simple_group= 0;
......
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