Skip to content

Commit 97ff62b

Browse files
committed
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
1 parent 7a277a3 commit 97ff62b

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

mysql-test/main/subselect3.inc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ from t2;
242242

243243
drop table t1,t2,t3,t4;
244244

245-
# More tests for tricky multi-column cases, where some of pushed-down
246-
# equalities are used for index lookups and some arent.
245+
--echo # More tests for tricky multi-column cases, where some of pushed-down
246+
--echo # equalities are used for index lookups and some are not.
247+
247248
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
248249
insert into t1 (oref, grp, ie1, ie2) values
249250
('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;
470471

471472
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
472473

474+
explain select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
475+
473476
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
474477

478+
explain select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
475479
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
476480

477481
explain
@@ -1012,7 +1016,6 @@ explain select * from t1 where a in (select a from t1);
10121016
drop table t1;
10131017
set @@optimizer_switch=@save_optimizer_switch;
10141018

1015-
set @@optimizer_switch=@save_optimizer_switch;
10161019
set @@optimizer_switch='materialization=off';
10171020

10181021
#
@@ -1034,19 +1037,22 @@ explain select * from t2 where a in (select straight_join A.a from t1 A, t1 B);
10341037
explain select straight_join * from t2 X, t2 Y
10351038
where X.a in (select straight_join A.a from t1 A, t1 B);
10361039

1037-
#
1038-
# SJ-Materialization scan + first table being system const table
1039-
#
1040+
--echo #
1041+
--echo # SJ-Materialization scan + first table being system const table
1042+
--echo #
10401043
create table t0 (a int, b int);
10411044
insert into t0 values(1,1);
10421045
explain select * from t0, t3 where t3.a in (select a from t2) and (t3.a < 10 or t3.a >30);
10431046
create table t4 as select a as x, a as y from t1;
10441047
explain select * from t0, t3 where (t3.a, t3.b) in (select x,y from t4) and (t3.a < 10 or t3.a >30);
10451048
drop table t0,t1,t2,t3,t4;
10461049

1047-
#
1048-
# LooseScan with ref access
1049-
#
1050+
--echo #
1051+
--echo # LooseScan with ref access
1052+
--echo #
1053+
1054+
set @@optimizer_switch='join_cache_hashed=off';
1055+
10501056
create table t0 (a int);
10511057
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
10521058
create table t1 (a int, b int, filler char(100), key(a,b));

mysql-test/main/subselect3.result

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ a b oref Z
278278
NULL 1 100 0
279279
NULL 2 100 NULL
280280
drop table t1,t2,t3,t4;
281+
# More tests for tricky multi-column cases, where some of pushed-down
282+
# equalities are used for index lookups and some are not.
281283
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
282284
insert into t1 (oref, grp, ie1, ie2) values
283285
('aa', 10, 2, 1),
@@ -617,10 +619,18 @@ cc 2 0
617619
cc NULL NULL
618620
aa 1 1
619621
bb NULL NULL
622+
explain select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
623+
id select_type table type possible_keys key key_len ref rows Extra
624+
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where
625+
1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 Using where
620626
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
621627
oref a
622628
cc 5
623629
aa 1
630+
explain select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
631+
id select_type table type possible_keys key key_len ref rows Extra
632+
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where
633+
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using where; Full scan on NULL key
624634
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
625635
oref a
626636
ee NULL
@@ -1220,7 +1230,6 @@ id select_type table type possible_keys key key_len ref rows Extra
12201230
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where; Start temporary; End temporary; Using join buffer (flat, BNL join)
12211231
drop table t1;
12221232
set @@optimizer_switch=@save_optimizer_switch;
1223-
set @@optimizer_switch=@save_optimizer_switch;
12241233
set @@optimizer_switch='materialization=off';
12251234
create table t1 (a int);
12261235
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
12541263
1 PRIMARY Y ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
12551264
2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 10 Using where
12561265
2 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
1266+
#
1267+
# SJ-Materialization scan + first table being system const table
1268+
#
12571269
create table t0 (a int, b int);
12581270
insert into t0 values(1,1);
12591271
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
12681280
1 PRIMARY t4 ALL NULL NULL NULL NULL 10 Using where; Start temporary
12691281
1 PRIMARY t3 ref a a 5 test.t4.x 10 Using where; End temporary
12701282
drop table t0,t1,t2,t3,t4;
1283+
#
1284+
# LooseScan with ref access
1285+
#
1286+
set @@optimizer_switch='join_cache_hashed=off';
12711287
create table t0 (a int);
12721288
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
12731289
create table t1 (a int, b int, filler char(100), key(a,b));

mysql-test/main/subselect3_jcl6.result

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ a b oref Z
281281
NULL 1 100 0
282282
NULL 2 100 NULL
283283
drop table t1,t2,t3,t4;
284+
# More tests for tricky multi-column cases, where some of pushed-down
285+
# equalities are used for index lookups and some are not.
284286
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
285287
insert into t1 (oref, grp, ie1, ie2) values
286288
('aa', 10, 2, 1),
@@ -620,10 +622,18 @@ cc 2 0
620622
cc NULL NULL
621623
aa 1 1
622624
bb NULL NULL
625+
explain select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
626+
id select_type table type possible_keys key key_len ref rows Extra
627+
1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 6 Using where
628+
1 PRIMARY t2 hash_ALL NULL #hash#$hj 10 test.t1.oref,test.t1.ie 7 Using where; Using join buffer (flat, BNLH join)
623629
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
624630
oref a
625-
aa 1
626631
cc 5
632+
aa 1
633+
explain select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
634+
id select_type table type possible_keys key key_len ref rows Extra
635+
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where
636+
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using where; Full scan on NULL key
627637
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
628638
oref a
629639
ee NULL
@@ -1223,7 +1233,6 @@ id select_type table type possible_keys key key_len ref rows Extra
12231233
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)
12241234
drop table t1;
12251235
set @@optimizer_switch=@save_optimizer_switch;
1226-
set @@optimizer_switch=@save_optimizer_switch;
12271236
set @@optimizer_switch='materialization=off';
12281237
create table t1 (a int);
12291238
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
12571266
1 PRIMARY Y ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
12581267
2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 10 Using where
12591268
2 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 10 Using join buffer (flat, BNL join)
1269+
#
1270+
# SJ-Materialization scan + first table being system const table
1271+
#
12601272
create table t0 (a int, b int);
12611273
insert into t0 values(1,1);
12621274
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
12711283
1 PRIMARY t4 ALL NULL NULL NULL NULL 10 Using where; Start temporary
12721284
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
12731285
drop table t0,t1,t2,t3,t4;
1286+
#
1287+
# LooseScan with ref access
1288+
#
1289+
set @@optimizer_switch='join_cache_hashed=off';
12741290
create table t0 (a int);
12751291
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
12761292
create table t1 (a int, b int, filler char(100), key(a,b));

mysql-test/main/subselect_sj2_jcl6.result

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,8 @@ explain select
264264
a, mid(filler1, 1,10), length(filler1)=length(filler2)
265265
from t2 ot where a in (select a from t1 it);
266266
id select_type table type possible_keys key key_len ref rows Extra
267-
1 PRIMARY ot ALL NULL NULL NULL NULL 22
268-
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
269-
2 MATERIALIZED it ALL NULL NULL NULL NULL 52
267+
1 PRIMARY ot ALL NULL NULL NULL NULL 22 Using where
268+
1 PRIMARY it hash_ALL NULL #hash#$hj 5 test.ot.a 52 Using where; FirstMatch(ot); Using join buffer (flat, BNLH join)
270269
select
271270
a, mid(filler1, 1,10), length(filler1)=length(filler2)
272271
from t2 ot where a in (select a from t1 it);
@@ -290,8 +289,8 @@ a mid(filler1, 1,10) length(filler1)=length(filler2)
290289
16 filler1234 1
291290
17 filler1234 1
292291
18 filler1234 1
293-
19 filler1234 1
294292
3 duplicate 1
293+
19 filler1234 1
295294
19 duplicate 1
296295
drop table t1, t2;
297296
create table t1 (a int, b int, key(a));

sql/sql_select.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8946,7 +8946,7 @@ best_access_path(JOIN *join,
89468946
We assume here that, thanks to the hash, we don't have to compare all
89478947
row combinations, only a HASH_FANOUT (10%) rows in the cache.
89488948
*/
8949-
row_copy_cost= (ROW_COPY_COST_THD(thd) * 2 *
8949+
row_copy_cost= (ROW_COPY_COST_THD(thd) *
89508950
JOIN_CACHE_ROW_COPY_COST_FACTOR(thd));
89518951
cmp_time= (record_count * row_copy_cost +
89528952
rnd_records * record_count * HASH_FANOUT *
@@ -8957,6 +8957,9 @@ best_access_path(JOIN *join,
89578957
best.cost= cur_cost;
89588958
best.records_read= best.records_after_filter= rows2double(s->records);
89598959
best.records= rnd_records;
8960+
#ifdef NOT_YET
8961+
set_if_smaller(best.records_out, rnd_records * HASH_FANOUT);
8962+
#endif
89608963
best.key= hj_start_key;
89618964
best.ref_depends_map= 0;
89628965
best.use_join_buffer= TRUE;

0 commit comments

Comments
 (0)