Commit cbd99688 authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-21095: Make Optimizer Trace support Index Condition Pushdown

Fixes over previous patches: do tracing of attached conditions
close to where we generate them.

Fix the tracing code to print the right conditions.
parent 07f21cfb
This diff is collapsed.
...@@ -257,23 +257,18 @@ explain select * from t1 where a=1 or b=1 { ...@@ -257,23 +257,18 @@ explain select * from t1 where a=1 or b=1 {
} }
}, },
{ {
"make_join_readinfo": [ "attaching_conditions_to_tables": {
"attached_conditions_computation": [],
"attached_conditions_summary": [
{ {
"table": "t1", "table": "t1",
"index_condition": "t1.a = 1 or t1.b = 1" "attached_condition": "t1.a = 1 or t1.b = 1"
}
]
} }
] ]
} }
}, },
{ {
"attaching_conditions_to_tables": { "make_join_readinfo": []
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "t1",
"attached_condition": "t1.a = 1 or t1.b = 1"
} }
] ]
} }
......
...@@ -256,27 +256,26 @@ explain select * from t1 where pk1 != 0 and key1 = 1 { ...@@ -256,27 +256,26 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
} }
}, },
{ {
"make_join_readinfo": [ "attaching_conditions_to_tables": {
"attached_conditions_computation": [],
"attached_conditions_summary": [
{ {
"table": "t1", "table": "t1",
"index_condition": null "attached_condition": "t1.pk1 <> 0"
}
]
} }
] ]
} }
}, },
{ {
"attaching_conditions_to_tables": { "make_join_readinfo": [
"attached_conditions_computation": [],
"attached_conditions_summary": [
{ {
"table": "t1", "table": "t1",
"attached_condition": null,
"index_condition": "t1.pk1 <> 0" "index_condition": "t1.pk1 <> 0"
} }
] ]
} }
]
}
}, },
{ {
"join_execution": { "join_execution": {
......
...@@ -133,23 +133,18 @@ select * from db1.t1 { ...@@ -133,23 +133,18 @@ select * from db1.t1 {
"cost": 0.010504815 "cost": 0.010504815
}, },
{ {
"make_join_readinfo": [ "attaching_conditions_to_tables": {
"attached_conditions_computation": [],
"attached_conditions_summary": [
{ {
"table": "t1", "table": "t1",
"index_condition": null "attached_condition": null
}
]
} }
] ]
} }
}, },
{ {
"attaching_conditions_to_tables": { "make_join_readinfo": []
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "t1",
"attached_condition": null
} }
] ]
} }
...@@ -280,23 +275,18 @@ select * from db1.v1 { ...@@ -280,23 +275,18 @@ select * from db1.v1 {
"cost": 0.010504815 "cost": 0.010504815
}, },
{ {
"make_join_readinfo": [ "attaching_conditions_to_tables": {
"attached_conditions_computation": [],
"attached_conditions_summary": [
{ {
"table": "t1", "table": "t1",
"index_condition": null "attached_condition": null
}
]
} }
] ]
} }
}, },
{ {
"attaching_conditions_to_tables": { "make_join_readinfo": []
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "t1",
"attached_condition": null
} }
] ]
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "mariadb.h" #include "mariadb.h"
#include "sql_select.h" #include "sql_select.h"
#include "sql_test.h" #include "sql_test.h"
#include "opt_trace.h"
/**************************************************************************** /****************************************************************************
* Index Condition Pushdown code starts * Index Condition Pushdown code starts
...@@ -355,6 +356,8 @@ void push_index_cond(JOIN_TAB *tab, uint keyno) ...@@ -355,6 +356,8 @@ void push_index_cond(JOIN_TAB *tab, uint keyno)
{ {
Item *idx_remainder_cond= 0; Item *idx_remainder_cond= 0;
tab->pre_idx_push_select_cond= tab->select_cond; tab->pre_idx_push_select_cond= tab->select_cond;
Json_writer_object trace(tab->join->thd);
trace.add_table_name(tab);
/* /*
For BKA cache we store condition to special BKA cache field For BKA cache we store condition to special BKA cache field
because evaluation of the condition requires additional operations because evaluation of the condition requires additional operations
...@@ -387,6 +390,7 @@ void push_index_cond(JOIN_TAB *tab, uint keyno) ...@@ -387,6 +390,7 @@ void push_index_cond(JOIN_TAB *tab, uint keyno)
idx_remainder_cond= NULL; idx_remainder_cond= NULL;
} }
} }
trace.add("index_condition", idx_cond);
/* /*
Disable eq_ref's "lookup cache" if we've pushed down an index Disable eq_ref's "lookup cache" if we've pushed down an index
...@@ -424,6 +428,10 @@ void push_index_cond(JOIN_TAB *tab, uint keyno) ...@@ -424,6 +428,10 @@ void push_index_cond(JOIN_TAB *tab, uint keyno)
} }
else else
tab->select_cond= idx_remainder_cond; tab->select_cond= idx_remainder_cond;
if (tab->select_cond)
trace.add("row_condition", tab->select_cond);
if (tab->select) if (tab->select)
{ {
DBUG_EXECUTE("where", DBUG_EXECUTE("where",
......
...@@ -349,8 +349,6 @@ static void fix_items_after_optimize(THD *thd, SELECT_LEX *select_lex); ...@@ -349,8 +349,6 @@ static void fix_items_after_optimize(THD *thd, SELECT_LEX *select_lex);
static void optimize_rownum(THD *thd, SELECT_LEX_UNIT *unit, Item *cond); static void optimize_rownum(THD *thd, SELECT_LEX_UNIT *unit, Item *cond);
static bool process_direct_rownum_comparison(THD *thd, SELECT_LEX_UNIT *unit, static bool process_direct_rownum_comparison(THD *thd, SELECT_LEX_UNIT *unit,
Item *cond); Item *cond);
void trace_attached_conditions(THD *thd, JOIN *join);
void trace_join_readinfo(THD *thd, JOIN *join);
#ifndef DBUG_OFF #ifndef DBUG_OFF
...@@ -3139,8 +3137,6 @@ int JOIN::optimize_stage2() ...@@ -3139,8 +3137,6 @@ int JOIN::optimize_stage2()
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after)) if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
DBUG_RETURN(1); DBUG_RETURN(1);
trace_join_readinfo(thd, this);
/* Perform FULLTEXT search before all regular searches */ /* Perform FULLTEXT search before all regular searches */
if (!(select_options & SELECT_DESCRIBE)) if (!(select_options & SELECT_DESCRIBE))
if (init_ftfuncs(thd, select_lex, MY_TEST(order))) if (init_ftfuncs(thd, select_lex, MY_TEST(order)))
...@@ -5116,8 +5112,6 @@ mysql_select(THD *thd, TABLE_LIST *tables, List<Item> &fields, COND *conds, ...@@ -5116,8 +5112,6 @@ mysql_select(THD *thd, TABLE_LIST *tables, List<Item> &fields, COND *conds,
goto err; // 1 goto err; // 1
} }
trace_attached_conditions(thd, join);
if (thd->lex->describe & DESCRIBE_EXTENDED) if (thd->lex->describe & DESCRIBE_EXTENDED)
{ {
join->conds_history= join->conds; join->conds_history= join->conds;
...@@ -12980,6 +12974,10 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -12980,6 +12974,10 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
DBUG_ENTER("make_join_select"); DBUG_ENTER("make_join_select");
if (select) if (select)
{ {
Json_writer_object trace_wrapper(thd);
Json_writer_object trace_conditions(thd, "attaching_conditions_to_tables");
Json_writer_array trace_attached_comp(thd,
"attached_conditions_computation");
add_not_null_conds(join); add_not_null_conds(join);
table_map used_tables; table_map used_tables;
/* /*
...@@ -13056,6 +13054,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -13056,6 +13054,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
{ {
add_cond_and_fix(thd, &outer_ref_cond, join->outer_ref_cond); add_cond_and_fix(thd, &outer_ref_cond, join->outer_ref_cond);
join->outer_ref_cond= outer_ref_cond; join->outer_ref_cond= outer_ref_cond;
Json_writer_object trace(thd);
trace.add("outer_ref_cond", outer_ref_cond);
} }
} }
else else
...@@ -13071,6 +13072,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -13071,6 +13072,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
add_cond_and_fix(thd, &pseudo_bits_cond, add_cond_and_fix(thd, &pseudo_bits_cond,
join->pseudo_bits_cond); join->pseudo_bits_cond);
join->pseudo_bits_cond= pseudo_bits_cond; join->pseudo_bits_cond= pseudo_bits_cond;
Json_writer_object trace(thd);
trace.add("pseudo_bits_cond", pseudo_bits_cond);
} }
} }
} }
...@@ -13647,6 +13651,22 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -13647,6 +13651,22 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
i++; i++;
} }
if (unlikely(thd->trace_started()))
{
trace_attached_comp.end();
Json_writer_array trace_attached_summary(thd,
"attached_conditions_summary");
for (tab= first_depth_first_tab(join); tab;
tab= next_depth_first_tab(join, tab))
{
if (!tab->table)
continue;
Item *const cond = tab->select_cond;
Json_writer_object trace_one_table(thd);
trace_one_table.add_table_name(tab);
trace_one_table.add("attached_condition", cond);
}
}
} }
DBUG_RETURN(0); DBUG_RETURN(0);
} }
...@@ -14724,6 +14744,9 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after) ...@@ -14724,6 +14744,9 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
uint i; uint i;
DBUG_ENTER("make_join_readinfo"); DBUG_ENTER("make_join_readinfo");
Json_writer_object trace_wrapper(join->thd);
Json_writer_array trace_arr(join->thd, "make_join_readinfo");
bool statistics= MY_TEST(!(join->select_options & SELECT_DESCRIBE)); bool statistics= MY_TEST(!(join->select_options & SELECT_DESCRIBE));
bool sorted= 1; bool sorted= 1;
...@@ -31905,61 +31928,6 @@ bool JOIN::transform_all_conds_and_on_exprs_in_join_list( ...@@ -31905,61 +31928,6 @@ bool JOIN::transform_all_conds_and_on_exprs_in_join_list(
return false; return false;
} }
void trace_attached_conditions(THD *thd, JOIN *join)
{
if (!unlikely(thd->trace_started()))
return;
Json_writer_object trace_wrapper(thd);
Json_writer_object trace_conditions(thd, "attaching_conditions_to_tables");
Json_writer_array trace_attached_comp(thd,
"attached_conditions_computation");
JOIN_TAB *tab;
trace_attached_comp.end();
Json_writer_array trace_attached_summary(thd,
"attached_conditions_summary");
for (tab= first_depth_first_tab(join);
tab;
tab= next_depth_first_tab(join, tab))
{
if (!tab->table)
continue;
Item *const remaining_cond = tab->select_cond;
Item *const idx_cond = tab->table->file->pushed_idx_cond;
Json_writer_object trace_one_table(thd);
trace_one_table.add_table_name(tab);
trace_one_table.add("attached_condition", remaining_cond);
if (idx_cond)
trace_one_table.add("index_condition", idx_cond);
}
}
void trace_join_readinfo(THD *thd, JOIN *join)
{
if (!unlikely(thd->trace_started()))
return;
Json_writer_object trace_wrapper(thd);
Json_writer_array trace_conditions(thd, "make_join_readinfo");
JOIN_TAB *tab;
for (tab= first_linear_tab(join, WITH_BUSH_ROOTS, WITHOUT_CONST_TABLES);
tab;
tab= next_linear_tab(join, tab, WITH_BUSH_ROOTS))
{
Json_writer_object trace_one_table(thd);
trace_one_table.add_table_name(tab);
trace_one_table.add("index_condition", tab->select_cond);
}
}
/** /**
@} (end of group Query_Optimizer) @} (end of group Query_Optimizer)
*/ */
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