1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

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.
This commit is contained in:
Sergei Petrunia
2022-12-08 21:24:31 +03:00
parent 07f21cfb14
commit cbd99688af
6 changed files with 551 additions and 812 deletions

File diff suppressed because it is too large Load Diff

View File

@ -256,17 +256,6 @@ explain select * from t1 where a=1 or b=1 {
"resulting_condition": "t1.a = 1 or t1.b = 1"
}
},
{
"make_join_readinfo": [
{
"table": "t1",
"index_condition": "t1.a = 1 or t1.b = 1"
}
]
}
]
}
},
{
"attaching_conditions_to_tables": {
"attached_conditions_computation": [],
@ -278,6 +267,12 @@ explain select * from t1 where a=1 or b=1 {
]
}
},
{
"make_join_readinfo": []
}
]
}
},
{
"join_execution": {
"select_id": 1,

View File

@ -255,28 +255,27 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
"resulting_condition": "t1.key1 = 1 and t1.pk1 <> 0"
}
},
{
"make_join_readinfo": [
{
"table": "t1",
"index_condition": null
}
]
}
]
}
},
{
"attaching_conditions_to_tables": {
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "t1",
"attached_condition": null,
"attached_condition": "t1.pk1 <> 0"
}
]
}
},
{
"make_join_readinfo": [
{
"table": "t1",
"index_condition": "t1.pk1 <> 0"
}
]
}
]
}
},
{
"join_execution": {

View File

@ -132,17 +132,6 @@ select * from db1.t1 {
"rows": 3,
"cost": 0.010504815
},
{
"make_join_readinfo": [
{
"table": "t1",
"index_condition": null
}
]
}
]
}
},
{
"attaching_conditions_to_tables": {
"attached_conditions_computation": [],
@ -154,6 +143,12 @@ select * from db1.t1 {
]
}
},
{
"make_join_readinfo": []
}
]
}
},
{
"join_execution": {
"select_id": 1,
@ -279,17 +274,6 @@ select * from db1.v1 {
"rows": 3,
"cost": 0.010504815
},
{
"make_join_readinfo": [
{
"table": "t1",
"index_condition": null
}
]
}
]
}
},
{
"attaching_conditions_to_tables": {
"attached_conditions_computation": [],
@ -301,6 +285,12 @@ select * from db1.v1 {
]
}
},
{
"make_join_readinfo": []
}
]
}
},
{
"join_execution": {
"select_id": 1,

View File

@ -17,6 +17,7 @@
#include "mariadb.h"
#include "sql_select.h"
#include "sql_test.h"
#include "opt_trace.h"
/****************************************************************************
* Index Condition Pushdown code starts
@ -355,6 +356,8 @@ void push_index_cond(JOIN_TAB *tab, uint keyno)
{
Item *idx_remainder_cond= 0;
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
because evaluation of the condition requires additional operations
@ -387,6 +390,7 @@ void push_index_cond(JOIN_TAB *tab, uint keyno)
idx_remainder_cond= NULL;
}
}
trace.add("index_condition", idx_cond);
/*
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)
}
else
tab->select_cond= idx_remainder_cond;
if (tab->select_cond)
trace.add("row_condition", tab->select_cond);
if (tab->select)
{
DBUG_EXECUTE("where",

View File

@ -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 bool process_direct_rownum_comparison(THD *thd, SELECT_LEX_UNIT *unit,
Item *cond);
void trace_attached_conditions(THD *thd, JOIN *join);
void trace_join_readinfo(THD *thd, JOIN *join);
#ifndef DBUG_OFF
@ -3139,8 +3137,6 @@ int JOIN::optimize_stage2()
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
DBUG_RETURN(1);
trace_join_readinfo(thd, this);
/* Perform FULLTEXT search before all regular searches */
if (!(select_options & SELECT_DESCRIBE))
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,
goto err; // 1
}
trace_attached_conditions(thd, join);
if (thd->lex->describe & DESCRIBE_EXTENDED)
{
join->conds_history= join->conds;
@ -12980,6 +12974,10 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
DBUG_ENTER("make_join_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);
table_map used_tables;
/*
@ -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);
join->outer_ref_cond= outer_ref_cond;
Json_writer_object trace(thd);
trace.add("outer_ref_cond", outer_ref_cond);
}
}
else
@ -13071,6 +13072,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
add_cond_and_fix(thd, &pseudo_bits_cond,
join->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)
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);
}
@ -14724,6 +14744,9 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
uint i;
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 sorted= 1;
@ -31905,61 +31928,6 @@ bool JOIN::transform_all_conds_and_on_exprs_in_join_list(
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)
*/