mariadb/mysql-test/main/join_cache_cardinality.result
Yuchen Pei c5154d8587
MDEV-36055 [wip] Add a check of join table counts before heuristic pruning
If we completely disable heuristic pruning, the test
main.greedy_optimizer will grind to almost hanging when the following
iterates to i > 20:

  let $query= SELECT COUNT(*) FROM t1 AS x;
  let $i= 1;
  while ($i < 61)
  {
    let $query= $query JOIN t$i ON t$i.I=x.I;
    inc $i;

    select @@optimizer_prune_level;
    select @@optimizer_search_depth;
    eval EXPLAIN $query;
  }
2025-07-28 16:33:02 +10:00

139 lines
3.7 KiB
Text

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
},
{
"hash_join_columns":
[
{
"field": "a",
"avg_frequency": 1
},
{
"field": "a",
"avg_frequency": 1
},
{
"field": "c",
"avg_frequency": 3.8462
}
],
"rows": 1
}
]
select json_detailed(json_extract(@json, '$**.rest_of_plan[*].rows_for_plan'))
as ROWS_FOR_PLAN;
ROWS_FOR_PLAN
[
100,
200
]
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
},
{
"hash_join_columns":
[
{
"field": "c",
"avg_frequency": 3.8462
}
],
"rows": 3.8462
}
]
select json_detailed(json_extract(@json, '$**.rest_of_plan[*].rows_for_plan'))
as ROWS_FOR_PLAN;
ROWS_FOR_PLAN
[
392.16,
769.24
]
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": 0.568632
}
]
# 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;