1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
create table t1 (a int, b int, c int);
insert into t1 select seq,seq/2, seq/4 from seq_1_to_100;
create table t2 (a int, b int, c int);
insert into t2 select seq, seq/2, seq/4 from seq_1_to_200;
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 OK
test.t2 analyze status Engine-independent statistics collected
test.t2 analyze status OK
set optimizer_trace=1;
set join_cache_level=6;
set optimizer_switch='hash_join_cardinality=on';
explain select *
from t1, t2
where t1.a=t2.a and t1.a=t2.b and t1.c=t2.c;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE t2 hash_ALL NULL #hash#$hj 15 test.t1.a,test.t1.a,test.t1.c 200 Using where; Using join buffer (flat, BNLH join)
set @json= (select trace from information_schema.optimizer_trace);
select json_detailed(json_extract(@json, '$**.hash_join_cardinality')) as JS;
JS
[
{
"hash_join_columns":
[
{
"field": "a",
"avg_frequency": 1
},
{
"field": "b",
"avg_frequency": 2
},
{
"field": "c",
"avg_frequency": 3.9216
}
],
"rows": 1
}
]
select json_detailed(json_extract(@json, '$**.rest_of_plan[*].rows_for_plan'))
as ROWS_FOR_PLAN;
ROWS_FOR_PLAN
[100]
explain select *
from t1, t2 where t1.c=t2.c;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE t2 hash_ALL NULL #hash#$hj 5 test.t1.c 200 Using where; Using join buffer (flat, BNLH join)
set @json= (select trace from information_schema.optimizer_trace);
select json_detailed(json_extract(@json, '$**.hash_join_cardinality')) as JS;
JS
[
{
"hash_join_columns":
[
{
"field": "c",
"avg_frequency": 3.9216
}
],
"rows": 3.9216
}
]
select json_detailed(json_extract(@json, '$**.rest_of_plan[*].rows_for_plan'))
as ROWS_FOR_PLAN;
ROWS_FOR_PLAN
[392.16]
explain select *
from t1 straight_join t2 where t1.c=t2.c and t2.a<30;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE t2 hash_ALL NULL #hash#$hj 5 test.t1.c 200 Using where; Using join buffer (flat, BNLH join)
set @json= (select trace from information_schema.optimizer_trace);
# Note that rows is the same:
select json_detailed(json_extract(@json, '$**.hash_join_cardinality')) as JS;
JS
[
{
"hash_join_columns":
[
{
"field": "c",
"avg_frequency": 3.9216
}
],
"rows": 3.9216
}
]
# Despite available selectivity:
select json_detailed(json_extract(@json, '$**.selectivity_for_columns')) as JS;
JS
[
[
{
"column_name": "a",
"ranges":
["NULL < a < 30"],
"selectivity_from_histogram": 0.145
}
]
]
drop table t1,t2;
|