Commit b9a45ba4 authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-23645: Optimizer trace: print conditions after substitute_for_best_equal_field

Print the conditions for WHERE, HAVING, and ON.
parent 00528a04
This diff is collapsed.
...@@ -727,4 +727,57 @@ select ...@@ -727,4 +727,57 @@ select
drop table t1,t2,t3; 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'; set optimizer_trace='enabled=off';
...@@ -229,9 +229,14 @@ explain select * from t1 where a=1 or b=1 { ...@@ -229,9 +229,14 @@ explain select * from t1 where a=1 or b=1 {
{ {
"best_join_order": ["t1"] "best_join_order": ["t1"]
}, },
{
"substitute_best_equal": {
"condition": "WHERE",
"resulting_condition": "t1.a = 1 or t1.b = 1"
}
},
{ {
"attaching_conditions_to_tables": { "attaching_conditions_to_tables": {
"original_condition": "t1.a = 1 or t1.b = 1",
"attached_conditions_computation": [], "attached_conditions_computation": [],
"attached_conditions_summary": [ "attached_conditions_summary": [
{ {
......
...@@ -234,9 +234,14 @@ explain select * from t1 where pk1 != 0 and key1 = 1 { ...@@ -234,9 +234,14 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
{ {
"best_join_order": ["t1"] "best_join_order": ["t1"]
}, },
{
"substitute_best_equal": {
"condition": "WHERE",
"resulting_condition": "t1.key1 = 1 and t1.pk1 <> 0"
}
},
{ {
"attaching_conditions_to_tables": { "attaching_conditions_to_tables": {
"original_condition": "t1.key1 = 1 and t1.pk1 <> 0",
"attached_conditions_computation": [], "attached_conditions_computation": [],
"attached_conditions_summary": [ "attached_conditions_summary": [
{ {
......
...@@ -117,7 +117,6 @@ select * from db1.t1 { ...@@ -117,7 +117,6 @@ select * from db1.t1 {
}, },
{ {
"attaching_conditions_to_tables": { "attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [], "attached_conditions_computation": [],
"attached_conditions_summary": [ "attached_conditions_summary": [
{ {
...@@ -240,7 +239,6 @@ select * from db1.v1 { ...@@ -240,7 +239,6 @@ select * from db1.v1 {
}, },
{ {
"attaching_conditions_to_tables": { "attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [], "attached_conditions_computation": [],
"attached_conditions_summary": [ "attached_conditions_summary": [
{ {
......
...@@ -595,6 +595,18 @@ void Json_writer::add_table_name(const TABLE *table) ...@@ -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) void add_table_scan_values_to_trace(THD *thd, JOIN_TAB *tab)
{ {
DBUG_ASSERT(thd->trace_started()); DBUG_ASSERT(thd->trace_started());
......
...@@ -108,6 +108,10 @@ void print_final_join_order(JOIN *join); ...@@ -108,6 +108,10 @@ void print_final_join_order(JOIN *join);
void print_best_access_for_table(THD *thd, POSITION *pos, void print_best_access_for_table(THD *thd, POSITION *pos,
enum join_type type); 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) Security related (need to add a proper comment here)
*/ */
......
...@@ -2408,6 +2408,10 @@ int JOIN::optimize_stage2() ...@@ -2408,6 +2408,10 @@ int JOIN::optimize_stage2()
DBUG_RETURN(1); DBUG_RETURN(1);
} }
conds->update_used_tables(); conds->update_used_tables();
if (unlikely(thd->trace_started()))
trace_condition(thd, "WHERE", "substitute_best_equal", conds);
DBUG_EXECUTE("where", DBUG_EXECUTE("where",
print_where(conds, print_where(conds,
"after substitute_best_equal", "after substitute_best_equal",
...@@ -2424,7 +2428,12 @@ int JOIN::optimize_stage2() ...@@ -2424,7 +2428,12 @@ int JOIN::optimize_stage2()
DBUG_RETURN(1); DBUG_RETURN(1);
} }
if (having) if (having)
{
having->update_used_tables(); having->update_used_tables();
if (unlikely(thd->trace_started()))
trace_condition(thd, "HAVING", "substitute_best_equal", having);
}
DBUG_EXECUTE("having", DBUG_EXECUTE("having",
print_where(having, print_where(having,
"after substitute_best_equal", "after substitute_best_equal",
...@@ -2451,6 +2460,11 @@ int JOIN::optimize_stage2() ...@@ -2451,6 +2460,11 @@ int JOIN::optimize_stage2()
DBUG_RETURN(1); DBUG_RETURN(1);
} }
(*tab->on_expr_ref)->update_used_tables(); (*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());
}
} }
} }
...@@ -11479,7 +11493,6 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -11479,7 +11493,6 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
*/ */
Json_writer_object trace_wrapper(thd); Json_writer_object trace_wrapper(thd);
Json_writer_object trace_conditions(thd, "attaching_conditions_to_tables"); Json_writer_object trace_conditions(thd, "attaching_conditions_to_tables");
trace_conditions.add("original_condition", cond);
Json_writer_array trace_attached_comp(thd, Json_writer_array trace_attached_comp(thd,
"attached_conditions_computation"); "attached_conditions_computation");
uint i; uint i;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment