Skip to content

Commit

Permalink
MDEV-23645: Optimizer trace: print conditions after substitute_for_be…
Browse files Browse the repository at this point in the history
…st_equal_field

Print the conditions for WHERE, HAVING, and ON.
  • Loading branch information
spetrunia committed Mar 19, 2021
1 parent 00528a0 commit b9a45ba
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 35 deletions.
236 changes: 206 additions & 30 deletions mysql-test/main/opt_trace.result

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions mysql-test/main/opt_trace.test
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,57 @@ select

drop table t1,t2,t3;

--echo #
--echo # MDEV-23645: Optimizer trace: print conditions after substitute_for_best_equal_field
--echo #
create table t1 (a int, b int, c int);
insert into t1 values (1,1,1),(2,2,2);

create table t2 as select * from t1;
insert into t2 select * from t2;

create table t3 as select * from t2;
insert into t3 select * from t3;

--echo # Check how HAVING is printed
explain
select
a,b, count(*)
from t1
where a=3
group by b,b
having a+b < 10;

select
json_detailed(json_extract(trace, '$**.substitute_best_equal'))
from
information_schema.optimizer_trace;

--echo # Check ON expression
explain
select
*
from t1 left join t2 on t2.a=t1.a and t2.a<3
where
t1.b > 5555;

select
json_detailed(json_extract(trace, '$**.substitute_best_equal'))
from
information_schema.optimizer_trace;

--echo # Check nested ON expression
explain
select
*
from t1 left join (t2,t3) on t2.a=t1.a and t3.a=t2.a and t3.a + t2.a <1000
where
t1.b > 5555;
select
json_detailed(json_extract(trace, '$**.substitute_best_equal'))
from
information_schema.optimizer_trace;

drop table t1,t2,t3;

set optimizer_trace='enabled=off';
7 changes: 6 additions & 1 deletion mysql-test/main/opt_trace_index_merge.result
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,14 @@ explain select * from t1 where a=1 or b=1 {
{
"best_join_order": ["t1"]
},
{
"substitute_best_equal": {
"condition": "WHERE",
"resulting_condition": "t1.a = 1 or t1.b = 1"
}
},
{
"attaching_conditions_to_tables": {
"original_condition": "t1.a = 1 or t1.b = 1",
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
Expand Down
7 changes: 6 additions & 1 deletion mysql-test/main/opt_trace_index_merge_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,14 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
{
"best_join_order": ["t1"]
},
{
"substitute_best_equal": {
"condition": "WHERE",
"resulting_condition": "t1.key1 = 1 and t1.pk1 <> 0"
}
},
{
"attaching_conditions_to_tables": {
"original_condition": "t1.key1 = 1 and t1.pk1 <> 0",
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
Expand Down
2 changes: 0 additions & 2 deletions mysql-test/main/opt_trace_security.result
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ select * from db1.t1 {
},
{
"attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
Expand Down Expand Up @@ -240,7 +239,6 @@ select * from db1.v1 {
},
{
"attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
Expand Down
12 changes: 12 additions & 0 deletions sql/opt_trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,18 @@ void Json_writer::add_table_name(const TABLE *table)
}


void trace_condition(THD * thd, const char *name, const char *transform_type,
Item *item, const char *table_name)
{
Json_writer_object trace_wrapper(thd);
Json_writer_object trace_cond(thd, transform_type);
trace_cond.add("condition", name);
if (table_name)
trace_cond.add("attached_to", table_name);
trace_cond.add("resulting_condition", item);
}


void add_table_scan_values_to_trace(THD *thd, JOIN_TAB *tab)
{
DBUG_ASSERT(thd->trace_started());
Expand Down
4 changes: 4 additions & 0 deletions sql/opt_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void print_final_join_order(JOIN *join);
void print_best_access_for_table(THD *thd, POSITION *pos,
enum join_type type);

void trace_condition(THD * thd, const char *name, const char *transform_type,
Item *item, const char *table_name= nullptr);


/*
Security related (need to add a proper comment here)
*/
Expand Down
15 changes: 14 additions & 1 deletion sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,10 @@ int JOIN::optimize_stage2()
DBUG_RETURN(1);
}
conds->update_used_tables();

if (unlikely(thd->trace_started()))
trace_condition(thd, "WHERE", "substitute_best_equal", conds);

DBUG_EXECUTE("where",
print_where(conds,
"after substitute_best_equal",
Expand All @@ -2424,7 +2428,12 @@ int JOIN::optimize_stage2()
DBUG_RETURN(1);
}
if (having)
{
having->update_used_tables();
if (unlikely(thd->trace_started()))
trace_condition(thd, "HAVING", "substitute_best_equal", having);
}

DBUG_EXECUTE("having",
print_where(having,
"after substitute_best_equal",
Expand All @@ -2451,6 +2460,11 @@ int JOIN::optimize_stage2()
DBUG_RETURN(1);
}
(*tab->on_expr_ref)->update_used_tables();
if (unlikely(thd->trace_started()))
{
trace_condition(thd, "ON expr", "substitute_best_equal",
(*tab->on_expr_ref), tab->table->alias.c_ptr());
}
}
}

Expand Down Expand Up @@ -11479,7 +11493,6 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
*/
Json_writer_object trace_wrapper(thd);
Json_writer_object trace_conditions(thd, "attaching_conditions_to_tables");
trace_conditions.add("original_condition", cond);
Json_writer_array trace_attached_comp(thd,
"attached_conditions_computation");
uint i;
Expand Down

0 comments on commit b9a45ba

Please sign in to comment.