Commit 97ff62b9 authored by Monty's avatar Monty

Fixed the cost for HASH join

Removed an old '* 2' from the HASH join cost. This was made obsolete by
a later patch that added cost for copying the data out from the join buffer
to table->record.

I also added some 'echo' to some test cases to make it easier to debug
test case changes.

Test case changes:
- subselect3_jcl6 and subselect_sj2_jcl6 result changes as materialized
  tables changed to hash join + first_match
parent 7a277a33
...@@ -242,8 +242,9 @@ from t2; ...@@ -242,8 +242,9 @@ from t2;
drop table t1,t2,t3,t4; drop table t1,t2,t3,t4;
# More tests for tricky multi-column cases, where some of pushed-down --echo # More tests for tricky multi-column cases, where some of pushed-down
# equalities are used for index lookups and some arent. --echo # equalities are used for index lookups and some are not.
create table t1 (oref char(4), grp int, ie1 int, ie2 int); create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values insert into t1 (oref, grp, ie1, ie2) values
('aa', 10, 2, 1), ('aa', 10, 2, 1),
...@@ -470,8 +471,11 @@ explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2; ...@@ -470,8 +471,11 @@ explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2; select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
explain select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
explain select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref); select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
explain explain
...@@ -1012,7 +1016,6 @@ explain select * from t1 where a in (select a from t1); ...@@ -1012,7 +1016,6 @@ explain select * from t1 where a in (select a from t1);
drop table t1; drop table t1;
set @@optimizer_switch=@save_optimizer_switch; set @@optimizer_switch=@save_optimizer_switch;
set @@optimizer_switch=@save_optimizer_switch;
set @@optimizer_switch='materialization=off'; set @@optimizer_switch='materialization=off';
# #
...@@ -1034,9 +1037,9 @@ explain select * from t2 where a in (select straight_join A.a from t1 A, t1 B); ...@@ -1034,9 +1037,9 @@ explain select * from t2 where a in (select straight_join A.a from t1 A, t1 B);
explain select straight_join * from t2 X, t2 Y explain select straight_join * from t2 X, t2 Y
where X.a in (select straight_join A.a from t1 A, t1 B); where X.a in (select straight_join A.a from t1 A, t1 B);
# --echo #
# SJ-Materialization scan + first table being system const table --echo # SJ-Materialization scan + first table being system const table
# --echo #
create table t0 (a int, b int); create table t0 (a int, b int);
insert into t0 values(1,1); insert into t0 values(1,1);
explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30); explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30);
...@@ -1044,9 +1047,12 @@ create table t4 as select a as x, a as y from t1; ...@@ -1044,9 +1047,12 @@ create table t4 as select a as x, a as y from t1;
explain select * from t0, t3 where (t3.a, t3.b) in (select x,y from t4) and (t3.a < 10 or t3.a >30); explain select * from t0, t3 where (t3.a, t3.b) in (select x,y from t4) and (t3.a < 10 or t3.a >30);
drop table t0,t1,t2,t3,t4; drop table t0,t1,t2,t3,t4;
# --echo #
# LooseScan with ref access --echo # LooseScan with ref access
# --echo #
set @@optimizer_switch='join_cache_hashed=off';
create table t0 (a int); create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, filler char(100), key(a,b)); create table t1 (a int, b int, filler char(100), key(a,b));
......
...@@ -278,6 +278,8 @@ a b oref Z ...@@ -278,6 +278,8 @@ a b oref Z
NULL 1 100 0 NULL 1 100 0
NULL 2 100 NULL NULL 2 100 NULL
drop table t1,t2,t3,t4; drop table t1,t2,t3,t4;
# More tests for tricky multi-column cases, where some of pushed-down
# equalities are used for index lookups and some are not.
create table t1 (oref char(4), grp int, ie1 int, ie2 int); create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values insert into t1 (oref, grp, ie1, ie2) values
('aa', 10, 2, 1), ('aa', 10, 2, 1),
...@@ -617,10 +619,18 @@ cc 2 0 ...@@ -617,10 +619,18 @@ cc 2 0
cc NULL NULL cc NULL NULL
aa 1 1 aa 1 1
bb NULL NULL bb NULL NULL
explain select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where
1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 Using where
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
oref a oref a
cc 5 cc 5
aa 1 aa 1
explain select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using where; Full scan on NULL key
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref); select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
oref a oref a
ee NULL ee NULL
...@@ -1220,7 +1230,6 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1220,7 +1230,6 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where; Start temporary; End temporary; Using join buffer (flat, BNL join) 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where; Start temporary; End temporary; Using join buffer (flat, BNL join)
drop table t1; drop table t1;
set @@optimizer_switch=@save_optimizer_switch; set @@optimizer_switch=@save_optimizer_switch;
set @@optimizer_switch=@save_optimizer_switch;
set @@optimizer_switch='materialization=off'; set @@optimizer_switch='materialization=off';
create table t1 (a int); create table t1 (a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
...@@ -1254,6 +1263,9 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1254,6 +1263,9 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY Y ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join) 1 PRIMARY Y ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 10 Using where
2 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join) 2 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
#
# SJ-Materialization scan + first table being system const table
#
create table t0 (a int, b int); create table t0 (a int, b int);
insert into t0 values(1,1); insert into t0 values(1,1);
explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30); explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30);
...@@ -1268,6 +1280,10 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1268,6 +1280,10 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 10 Using where; Start temporary 1 PRIMARY t4 ALL NULL NULL NULL NULL 10 Using where; Start temporary
1 PRIMARY t3 ref a a 5 test.t4.x 10 Using where; End temporary 1 PRIMARY t3 ref a a 5 test.t4.x 10 Using where; End temporary
drop table t0,t1,t2,t3,t4; drop table t0,t1,t2,t3,t4;
#
# LooseScan with ref access
#
set @@optimizer_switch='join_cache_hashed=off';
create table t0 (a int); create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, filler char(100), key(a,b)); create table t1 (a int, b int, filler char(100), key(a,b));
......
...@@ -281,6 +281,8 @@ a b oref Z ...@@ -281,6 +281,8 @@ a b oref Z
NULL 1 100 0 NULL 1 100 0
NULL 2 100 NULL NULL 2 100 NULL
drop table t1,t2,t3,t4; drop table t1,t2,t3,t4;
# More tests for tricky multi-column cases, where some of pushed-down
# equalities are used for index lookups and some are not.
create table t1 (oref char(4), grp int, ie1 int, ie2 int); create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values insert into t1 (oref, grp, ie1, ie2) values
('aa', 10, 2, 1), ('aa', 10, 2, 1),
...@@ -620,10 +622,18 @@ cc 2 0 ...@@ -620,10 +622,18 @@ cc 2 0
cc NULL NULL cc NULL NULL
aa 1 1 aa 1 1
bb NULL NULL bb NULL NULL
explain select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 6 Using where
1 PRIMARY t2 hash_ALL NULL #hash#$hj 10 test.t1.oref,test.t1.ie 7 Using where; Using join buffer (flat, BNLH join)
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
oref a oref a
aa 1
cc 5 cc 5
aa 1
explain select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using where; Full scan on NULL key
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref); select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
oref a oref a
ee NULL ee NULL
...@@ -1223,7 +1233,6 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1223,7 +1233,6 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 hash_ALL NULL #hash#$hj 6 test.t1.a 2 Using where; Start temporary; End temporary; Using join buffer (flat, BNLH join) 1 PRIMARY t1 hash_ALL NULL #hash#$hj 6 test.t1.a 2 Using where; Start temporary; End temporary; Using join buffer (flat, BNLH join)
drop table t1; drop table t1;
set @@optimizer_switch=@save_optimizer_switch; set @@optimizer_switch=@save_optimizer_switch;
set @@optimizer_switch=@save_optimizer_switch;
set @@optimizer_switch='materialization=off'; set @@optimizer_switch='materialization=off';
create table t1 (a int); create table t1 (a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
...@@ -1257,6 +1266,9 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1257,6 +1266,9 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY Y ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join) 1 PRIMARY Y ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 10 Using where
2 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join) 2 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
#
# SJ-Materialization scan + first table being system const table
#
create table t0 (a int, b int); create table t0 (a int, b int);
insert into t0 values(1,1); insert into t0 values(1,1);
explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30); explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30);
...@@ -1271,6 +1283,10 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1271,6 +1283,10 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 10 Using where; Start temporary 1 PRIMARY t4 ALL NULL NULL NULL NULL 10 Using where; Start temporary
1 PRIMARY t3 ref a a 5 test.t4.x 10 Using where; End temporary; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 1 PRIMARY t3 ref a a 5 test.t4.x 10 Using where; End temporary; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan
drop table t0,t1,t2,t3,t4; drop table t0,t1,t2,t3,t4;
#
# LooseScan with ref access
#
set @@optimizer_switch='join_cache_hashed=off';
create table t0 (a int); create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, filler char(100), key(a,b)); create table t1 (a int, b int, filler char(100), key(a,b));
......
...@@ -264,9 +264,8 @@ explain select ...@@ -264,9 +264,8 @@ explain select
a, mid(filler1, 1,10), length(filler1)=length(filler2) a, mid(filler1, 1,10), length(filler1)=length(filler2)
from t2 ot where a in (select a from t1 it); from t2 ot where a in (select a from t1 it);
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 ot ALL NULL NULL NULL NULL 22 1 PRIMARY ot ALL NULL NULL NULL NULL 22 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY it hash_ALL NULL #hash#$hj 5 test.ot.a 52 Using where; FirstMatch(ot); Using join buffer (flat, BNLH join)
2 MATERIALIZED it ALL NULL NULL NULL NULL 52
select select
a, mid(filler1, 1,10), length(filler1)=length(filler2) a, mid(filler1, 1,10), length(filler1)=length(filler2)
from t2 ot where a in (select a from t1 it); from t2 ot where a in (select a from t1 it);
...@@ -290,8 +289,8 @@ a mid(filler1, 1,10) length(filler1)=length(filler2) ...@@ -290,8 +289,8 @@ a mid(filler1, 1,10) length(filler1)=length(filler2)
16 filler1234 1 16 filler1234 1
17 filler1234 1 17 filler1234 1
18 filler1234 1 18 filler1234 1
19 filler1234 1
3 duplicate 1 3 duplicate 1
19 filler1234 1
19 duplicate 1 19 duplicate 1
drop table t1, t2; drop table t1, t2;
create table t1 (a int, b int, key(a)); create table t1 (a int, b int, key(a));
......
...@@ -8946,7 +8946,7 @@ best_access_path(JOIN *join, ...@@ -8946,7 +8946,7 @@ best_access_path(JOIN *join,
We assume here that, thanks to the hash, we don't have to compare all We assume here that, thanks to the hash, we don't have to compare all
row combinations, only a HASH_FANOUT (10%) rows in the cache. row combinations, only a HASH_FANOUT (10%) rows in the cache.
*/ */
row_copy_cost= (ROW_COPY_COST_THD(thd) * 2 * row_copy_cost= (ROW_COPY_COST_THD(thd) *
JOIN_CACHE_ROW_COPY_COST_FACTOR(thd)); JOIN_CACHE_ROW_COPY_COST_FACTOR(thd));
cmp_time= (record_count * row_copy_cost + cmp_time= (record_count * row_copy_cost +
rnd_records * record_count * HASH_FANOUT * rnd_records * record_count * HASH_FANOUT *
...@@ -8957,6 +8957,9 @@ best_access_path(JOIN *join, ...@@ -8957,6 +8957,9 @@ best_access_path(JOIN *join,
best.cost= cur_cost; best.cost= cur_cost;
best.records_read= best.records_after_filter= rows2double(s->records); best.records_read= best.records_after_filter= rows2double(s->records);
best.records= rnd_records; best.records= rnd_records;
#ifdef NOT_YET
set_if_smaller(best.records_out, rnd_records * HASH_FANOUT);
#endif
best.key= hj_start_key; best.key= hj_start_key;
best.ref_depends_map= 0; best.ref_depends_map= 0;
best.use_join_buffer= TRUE; best.use_join_buffer= TRUE;
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