Skip to content
/ server Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions mysql-test/main/opt_trace_selectivity.result
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,60 @@ JS
]
]
drop table t1;
#
# MDEV-38273: Optimizer trace should have selectivities collected via sampling
#
create table t1 (a int, b int, c varchar(32), d varchar(32));
insert into t1
select
seq, seq,
if(mod(seq, 10) < 4,'c-ccc-c', 'no-match'),
if(mod(seq, 10) < 3,'d-ddd-d', 'no-match')
from seq_1_to_1000;
analyze table t1 persistent for all;
Table Op Msg_type Msg_text
test.t1 analyze status Engine-independent statistics collected
test.t1 analyze status OK
set statement optimizer_use_condition_selectivity=5 for
explain select * from t1
where a < 700 and b < 500 and c like '%ccc%' and d like '%ddd%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 Using where
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.rows_estimation[0]')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
{
"selectivity_for_indexes":
[],
"selectivity_for_columns":
[
{
"column_name": "a",
"ranges":
["NULL < a < 700"],
"selectivity_from_histogram": 0.699
},
{
"column_name": "b",
"ranges":
["NULL < b < 500"],
"selectivity_from_histogram": 0.499
}
],
"sampled_selectivity":
[
{
"cond": "t1.c like '%ccc%'",
"selectivity": 0.4
},
{
"cond": "t1.d like '%ddd%'",
"selectivity": 0.3
}
],
"cond_selectivity": 0.04185612
}
]
drop table t1;
set optimizer_trace='enabled=off';
23 changes: 23 additions & 0 deletions mysql-test/main/opt_trace_selectivity.test
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,27 @@ select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;

drop table t1;

--echo #
--echo # MDEV-38273: Optimizer trace should have selectivities collected via sampling
--echo #
create table t1 (a int, b int, c varchar(32), d varchar(32));
insert into t1
select
seq, seq,
if(mod(seq, 10) < 4,'c-ccc-c', 'no-match'),
if(mod(seq, 10) < 3,'d-ddd-d', 'no-match')
from seq_1_to_1000;

analyze table t1 persistent for all;

set statement optimizer_use_condition_selectivity=5 for
explain select * from t1
where a < 700 and b < 500 and c like '%ccc%' and d like '%ddd%';

select JSON_DETAILED(JSON_EXTRACT(trace, '$**.rows_estimation[0]')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;

drop table t1;

set optimizer_trace='enabled=off';
8 changes: 8 additions & 0 deletions sql/opt_range.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3873,6 +3873,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
if (*cond && check_rows > SELECTIVITY_SAMPLING_THRESHOLD &&
thd->variables.optimizer_use_condition_selectivity > 4)
{
Json_writer_array trace_sampled_sel(thd, "sampled_selectivity");
find_selective_predicates_list_processor_data *dt=
(find_selective_predicates_list_processor_data *)
alloc_root(thd->mem_root,
Expand Down Expand Up @@ -3902,6 +3903,13 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
(double)stat->positive / examined_rows));
double selectivity= ((double)stat->positive) / examined_rows;
table->multiply_cond_selectivity(selectivity);

if (unlikely(trace_sampled_sel.trace_started()))
{
Json_writer_object selectivity_for_cond(thd);
selectivity_for_cond.add("cond", stat->cond);
selectivity_for_cond.add("selectivity", selectivity);
}
/*
If a field is involved then we register its selectivity in case
there in an equality with the field.
Expand Down