摘要:
记录STONEDB对于mysql的查询优化器的使用
逻辑关系:
调用堆栈:
(gdb) bt
#0 JOIN::optimize (this=0x7fb23804e600, part=1 '\001') at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:296
#1 0x0000000002c70a23 in Tianmu::core::optimize_select (thd=0x7fb238002c10, select_options=2147748608, result=0x7fb238011940, select_lex=0x7fb238007da0,
optimize_after_tianmu=@0x7fb468da9d0c: 1, free_join=@0x7fb468da9d10: 1) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/tianmu/core/engine_execute.cpp:352
#2 0x0000000002c702d6 in Tianmu::core::Engine::HandleSelect (this=0x5df8fd0, thd=0x7fb238002c10, lex=0x7fb238004f38, result=@0x7fb468da9d18: 0x7fb238011940, setup_tables_done_option=0,
res=@0x7fb468da9d14: 0, optimize_after_tianmu=@0x7fb468da9d0c: 1, tianmu_free_join=@0x7fb468da9d10: 1, with_insert=0)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/tianmu/core/engine_execute.cpp:225
#3 0x0000000002d58d6d in Tianmu::dbhandler::TIANMU_HandleSelect (thd=0x7fb238002c10, lex=0x7fb238004f38, result=@0x7fb468da9d18: 0x7fb238011940, setup_tables_done_option=0,
res=@0x7fb468da9d14: 0, optimize_after_tianmu=@0x7fb468da9d0c: 1, tianmu_free_join=@0x7fb468da9d10: 1, with_insert=0)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/tianmu/handler/ha_rcengine.cpp:82
#4 0x000000000239b0ca in execute_sqlcom_select (thd=0x7fb238002c10, all_tables=0x7fb23804b670) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:5182
#5 0x000000000239444e in mysql_execute_command (thd=0x7fb238002c10, first_level=true) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:2831
#6 0x000000000239c093 in mysql_parse (thd=0x7fb238002c10, parser_state=0x7fb468daaeb0) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:5621
#7 0x000000000239132b in dispatch_command (thd=0x7fb238002c10, com_data=0x7fb468dab650, command=COM_QUERY)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:1495
#8 0x0000000002390257 in do_command (thd=0x7fb238002c10) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:1034
#9 0x00000000024c2e57 in handle_connection (arg=0x8bf3c30) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/conn_handler/connection_handler_per_:313
#10 0x0000000002ba6a88 in pfs_spawn_thread (arg=0x8c2c1e0) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/perfschema/:2197
#11 0x00007fb4731beea5 in start_thread () from /lib64/libpthread.so.0
#12 0x00007fb4706e2b0d in clone () from /lib64/libc.so.6
核心函数:
Engine::optimize_select
/*
Prepares and optimizes a single select for Tianmu engine
*/
int optimize_select(THD *thd, ulong select_options, Query_result *result,
SELECT_LEX *select_lex, int &optimize_after_tianmu, int &free_join)
{
// copied from sql_select.cpp from the beginning of mysql_select(...)
int err = 0;
free_join = 1;
select_lex->context.resolve_in_select_list = TRUE;
JOIN *join;
if (select_lex->join != 0) {
join = select_lex->join;
// here is EXPLAIN of subselect or derived table
if (select_lex->linkage != DERIVED_TABLE_TYPE || (select_options & (1ULL << 2))) {
if (select_lex->linkage != GLOBAL_OPTIONS_TYPE) {
if (result->prepare(select_lex->join->fields_list, select_lex->master_unit()) || result->prepare2())
{
return TRUE;
}
} else {
if ((err = select_lex->prepare(thd)))
{
return err;
}
}
}
free_join = 0;
join->select_options = select_options;
}
else
{
thd_proc_info(thd, "init");
if ((err = select_lex->prepare(thd)))
{
return err;
}
if (result->prepare(select_lex->fields_list, select_lex->master_unit()) || result->prepare2()) {
return TRUE;
}
if (!(join = new JOIN(thd, select_lex)))
return TRUE; /* purecov: inspected */
select_lex->set_join(join);
}
join->best_rowcount = 2;
optimize_after_tianmu = TRUE;
if ((err = join->optimize(1)))
return err;
return FALSE;
}
if ((err = join->optimize(1)))
return err;
JOIN::optimize
/**
Optimizes one query block into a query execution plan (QEP.)
This is the entry point to the query optimization phase. This phase
applies both logical (equivalent) query rewrites, cost-based join
optimization, and rule-based access path selection. Once an optimal
plan is found, the member function creates/initializes all
structures needed for query execution. The main optimization phases
are outlined below:
-# Logical transformations:
- Outer to inner joins transformation.
- Equality/constant propagation.
- Partition pruning.
- COUNT(*), MIN(), MAX() constant substitution in case of
implicit grouping.
- ORDER BY optimization.
-# Perform cost-based optimization of table order and access path
selection. See JOIN::make_join_plan()
-# Post-join order optimization:
- Create optimal table conditions from the where clause and the
join conditions.
- Inject outer-join guarding conditions.
- Adjust data access methods after determining table condition
(several times.)
- Optimize ORDER BY/DISTINCT.
-# Code generation
- Set data access functions.
- Try to optimize away sorting/distinct.
- Setup temporary table usage for grouping and/or sorting.
@retval 0 Success.
@retval 1 Error, error code saved in member JOIN::error.
*/
int
JOIN::optimize(unsigned char part) //TIANMU UPGRADE
{
uint no_jbuf_after= UINT_MAX;
DBUG_ENTER("JOIN::optimize");
assert(select_lex->leaf_table_count == 0 ||
thd->lex->is_query_tables_locked() ||
select_lex == unit->fake_select_lex);
//assert(tables == 0 &&
// primary_tables == 0 &&
// tables_list == (TABLE_LIST*)1);
//TIANMU UPGRADE BEGIN
/*
Two more values of part were introduced part=3 and part=4. The main reason is to break optimization in sense of part=2 in point
where all transformations of LOJ conditions are finished. The optimization is continued in case we switch to MySQL.
The case was wrong result in "select * from t1 left join t2 on a1=b1 and b1=3 where a1=1;". That was due to optimization
of ON condition "a1=b1 and b1=3" into "a1=b1 and b1=1" which after part=2 would be transformed to FALSE. Part=3 does this transformation.
However, it has to be stopped at some point (compared to part=2) to avoid rest of optimizations, e.g., creation of temporary tables. (P.S.)
*/
//const bool first_optimization= select_lex->first_cond_optimization;
Opt_trace_context * const trace= &thd->opt_trace;
Opt_trace_object trace_wrapper(trace);
Opt_trace_object trace_optimize(trace, "join_optimization");
trace_optimize.add_select_number(select_lex->select_number);
Opt_trace_array trace_steps(trace, "steps");
if(part != 4) {
if (part==0 || part==1)
{
//END
// to prevent double initialization on EXPLAIN
if (optimized)
DBUG_RETURN(0);
Prepare_error_tracker tracker(thd);
DEBUG_SYNC(thd, "before_join_optimize");
THD_STAGE_INFO(thd, stage_optimizing);
if (select_lex->first_execution)
{
/**
@todo
This query block didn't transform itself in SELECT_LEX::prepare(), so
belongs to a parent query block. That parent, or its parents, had to
transform us - it has not; maybe it is itself in prepare() and
evaluating the present query block as an Item_subselect. Such evaluation
in prepare() is expected to be a rare case to be eliminated in the
future ("SET x=(subq)" is one such case; because it locks tables before
prepare()).
*/
if (select_lex->apply_local_transforms(thd, false))
DBUG_RETURN(error= 1);
}
//TIANMU UPGRADE
/*
Opt_trace_context * const trace= &thd->opt_trace;
Opt_trace_object trace_wrapper(trace);
Opt_trace_object trace_optimize(trace, "join_optimization");
trace_optimize.add_select_number(select_lex->select_number);
Opt_trace_array trace_steps(trace, "steps");
*/
//END
count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
assert(tmp_table_param.sum_func_count == 0 ||
group_list || implicit_grouping);
if (select_lex->olap == ROLLUP_TYPE && optimize_rollup())
DBUG_RETURN(true); /* purecov: inspected */
if (alloc_func_list())
DBUG_RETURN(1); /* purecov: inspected */
if (select_lex->get_optimizable_conditions(thd, &where_cond, &having_cond))
DBUG_RETURN(1);
set_optimized();
tables_list= select_lex->get_table_list();
/* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
/*
Run optimize phase for all derived tables/views used in this SELECT,
including those in semi-joins.
*/
if (select_lex->materialized_derived_table_count)
{
for (TABLE_LIST *tl= select_lex->leaf_tables; tl; tl= tl->next_leaf)
{
if (tl->is_view_or_derived() && tl->optimize_derived(thd))
DBUG_RETURN(1);
}
}
/* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
row_limit= ((select_distinct || order || group_list) ?
HA_POS_ERROR : unit->select_limit_cnt);
// m_select_limit is used to decide if we are likely to scan the whole table.
m_select_limit= unit->select_limit_cnt;
if (unit->first_select()->active_options() & OPTION_FOUND_ROWS)
{
/*
Calculate found rows if
- LIMIT is set, and
- Query block is not equipped with "braces". In this case, each
query block must be calculated fully and the limit is applied on
the final UNION evaluation.
*/
calc_found_rows= m_select_limit != HA_POS_ERROR && !select_lex->braces;
}
if (having_cond || calc_found_rows)
m_select_limit= HA_POS_ERROR;
if (unit->select_limit_cnt == 0 && !calc_found_rows)
{
zero_result_cause= "Zero limit";
best_rowcount= 0;
goto setup_subq_exit;
}
}//TIANMU UPGRADE END part=0||part=1
if (where_cond || select_lex->outer_join)
{
if (optimize_cond(thd, &where_cond, &cond_equal,
&select_lex->top_join_list, &select_lex->cond_value,part))//TIANMU UPGRADE
{
error= 1;
DBUG_PRINT("error",("Error from optimize_cond"));
DBUG_RETURN(1);
}
if (select_lex->cond_value == Item::COND_FALSE)
{
zero_result_cause= "Impossible WHERE";
best_rowcount= 0;
goto setup_subq_exit;
}
}
if (having_cond)
{
if (optimize_cond(thd, &having_cond, &cond_equal, NULL,
&select_lex->having_value, part))//TIANMU UPGRADE
{
error= 1;
DBUG_PRINT("error",("Error from optimize_cond"));
DBUG_RETURN(1);
}
if (select_lex->having_value == Item::COND_FALSE)
{
zero_result_cause= "Impossible HAVING";
best_rowcount= 0;
goto setup_subq_exit;
}
}
//TIANMU UPGRADE
if (part == 1)
{
error= 0;
DBUG_RETURN(0);
}
//END
if (select_lex->partitioned_table_count && prune_table_partitions())
{
error= 1;
DBUG_PRINT("error", ("Error from prune_partitions"));
DBUG_RETURN(1);
}
/*
Try to optimize count(*), min() and max() to const fields if
there is implicit grouping (aggregate functions but no
group_list). In this case, the result set shall only contain one
row.
*/
if (tables_list && implicit_grouping)
{
int res;
/*
opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
the WHERE condition,
or 1 if all items were resolved (optimized away),
or 0, or an error number HA_ERR_...
If all items were resolved by opt_sum_query, there is no need to
open any tables.
*/
if ((res= opt_sum_query(thd, select_lex->leaf_tables, all_fields,
where_cond)))
{
best_rowcount= 0;
if (res == HA_ERR_KEY_NOT_FOUND)
{
DBUG_PRINT("info",("No matching min/max row"));
zero_result_cause= "No matching min/max row";
goto setup_subq_exit;
}
if (res > 1)
{
error= res;
DBUG_PRINT("error",("Error from opt_sum_query"));
DBUG_RETURN(1);
}
if (res < 0)
{
DBUG_PRINT("info",("No matching min/max row"));
zero_result_cause= "No matching min/max row";
goto setup_subq_exit;
}
DBUG_PRINT("info",("Select tables optimized away"));
zero_result_cause= "Select tables optimized away";
tables_list= 0; // All tables resolved
best_rowcount= 1;
const_tables= tables= primary_tables= select_lex->leaf_table_count;
/*
Extract all table-independent conditions and replace the WHERE
clause with them. All other conditions were computed by opt_sum_query
and the MIN/MAX/COUNT function(s) have been replaced by constants,
so there is no need to compute the whole WHERE clause again.
Notice that make_cond_for_table() will always succeed to remove all
computed conditions, because opt_sum_query() is applicable only to
conjunctions.
Preserve conditions for EXPLAIN.
*/
if (where_cond && !thd->lex->describe)
{
Item *table_independent_conds=
make_cond_for_table(where_cond, PSEUDO_TABLE_BITS, 0, 0);
DBUG_EXECUTE("where",
print_where(table_independent_conds,
"where after opt_sum_query()",
QT_ORDINARY););
where_cond= table_independent_conds;
}
goto setup_subq_exit;
}
}
if (!tables_list)
{
DBUG_PRINT("info",("No tables"));
best_rowcount= 1;
error= 0;
if (make_tmp_tables_info())
DBUG_RETURN(1);
count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
// Make plan visible for EXPLAIN
set_plan_state(NO_TABLES);
DBUG_RETURN(0);
}
error= -1; // Error is sent to client
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
if ((where_cond || group_list || order) &&
substitute_gc(thd, select_lex, where_cond, group_list, order))
{
// We added hidden fields to the all_fields list, count them.
count_field_types(select_lex, &tmp_table_param, select_lex->all_fields,
false, false);
}
// Set up join order and initial access paths
THD_STAGE_INFO(thd, stage_statistics);
if (make_join_plan())
{
if (thd->killed)
thd->send_kill_message();
DBUG_PRINT("error",("Error: JOIN::make_join_plan() failed"));
DBUG_RETURN(1);
}
// At this stage, join_tab==NULL, JOIN_TABs are listed in order by best_ref.
ASSERT_BEST_REF_IN_JOIN_ORDER(this);
if (zero_result_cause)
goto setup_subq_exit;
if (rollup.state != ROLLUP::STATE_NONE)
{
if (rollup_process_const_fields())
{
DBUG_PRINT("error", ("Error: rollup_process_fields() failed"));
DBUG_RETURN(1);
}
/*
Fields may have been replaced by Item_func_rollup_const, so
recalculate the number of fields and functions for this query block.
*/
// JOIN::optimize_rollup() may set quick_group=0, and we must not undo that.
const uint save_quick_group= tmp_table_param.quick_group;
count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
tmp_table_param.quick_group= save_quick_group;
}
else
{
/* Remove distinct if only const tables */
select_distinct&= !plan_is_const();
}
if (const_tables && !thd->locked_tables_mode &&
!(select_lex->active_options() & SELECT_NO_UNLOCK )&& part!=3) //TIANMU UPGRADE
{
TABLE *ct[MAX_TABLES];
for (uint i= 0; i < const_tables; i++)
ct[i]= best_ref[i]->table();
mysql_unlock_some_tables(thd, ct, const_tables);
}
if (!where_cond && select_lex->outer_join)
{
/* Handle the case where we have an OUTER JOIN without a WHERE */
where_cond=new Item_int((longlong) 1,1); // Always true
}
error= 0;
/*
Among the equal fields belonging to the same multiple equality
choose the one that is to be retrieved first and substitute
all references to these in where condition for a reference for
the selected field.
*/
if (where_cond)
{
where_cond= substitute_for_best_equal_field(where_cond, cond_equal,
map2table);
if (thd->is_error())
{
error= 1;
DBUG_PRINT("error",("Error from substitute_for_best_equal"));
DBUG_RETURN(1);
}
where_cond->update_used_tables();
DBUG_EXECUTE("where",
print_where(where_cond,
"after substitute_best_equal",
QT_ORDINARY););
}
/*
Perform the same optimization on field evaluation for all join conditions.
*/
for (uint i= const_tables; i < tables ; ++i)
{
JOIN_TAB *const tab= best_ref[i];
if (tab->position() && tab->join_cond())
{
tab->set_join_cond(substitute_for_best_equal_field(tab->join_cond(),
tab->cond_equal,
map2table));
if (thd->is_error())
{
error= 1;
DBUG_PRINT("error",("Error from substitute_for_best_equal"));
DBUG_RETURN(1);
}
tab->join_cond()->update_used_tables();
}
}
//TIANMU UPGRADE BEGIN
// this is end of part=3 and beginning of part=4
if(part == 3) {
DBUG_RETURN(0); // error == 0
}
//END
}// end of if(part!=4)
if (init_ref_access())
{
error= 1;
DBUG_PRINT("error",("Error from init_ref_access"));
DBUG_RETURN(1);
}
// Update table dependencies after assigning ref access fields
update_depend_map();
THD_STAGE_INFO(thd, stage_preparing);
if (make_join_select(this, where_cond))
{
if (thd->is_error())
DBUG_RETURN(1);
zero_result_cause=
"Impossible WHERE noticed after reading const tables";
goto setup_subq_exit;
}
if (select_lex->query_result()->initialize_tables(this))
{
DBUG_PRINT("error",("Error: initialize_tables() failed"));
DBUG_RETURN(1); // error == -1
}
error= -1; /* if goto err */
if (optimize_distinct_group_order())
DBUG_RETURN(true);
if ((select_lex->active_options() & SELECT_NO_JOIN_CACHE) ||
select_lex->ftfunc_list->elements)
no_jbuf_after= 0;
/* Perform FULLTEXT search before all regular searches */
if (select_lex->has_ft_funcs() && optimize_fts_query())
DBUG_RETURN(1);
/*
By setting child_subquery_can_materialize so late we gain the following:
JOIN::compare_costs_of_subquery_strategies() can test this variable to
know if we are have finished evaluating constant conditions, which itself
helps determining fanouts.
*/
child_subquery_can_materialize= true;
/*
It's necessary to check const part of HAVING cond as
there is a chance that some cond parts may become
const items after make_join_statisctics(for example
when Item is a reference to const table field from
outer join).
This check is performed only for those conditions
which do not use aggregate functions. In such case
temporary table may not be used and const condition
elements may be lost during further having
condition transformation in JOIN::exec.
*/
if (having_cond && const_table_map && !having_cond->with_sum_func)
{
having_cond->update_used_tables();
if (remove_eq_conds(thd, having_cond, &having_cond,
&select_lex->having_value, part))
{
error= 1;
DBUG_PRINT("error",("Error from remove_eq_conds"));
DBUG_RETURN(1);
}
if (select_lex->having_value == Item::COND_FALSE)
{
having_cond= new Item_int((longlong) 0,1);
zero_result_cause= "Impossible HAVING noticed after reading const tables";
goto setup_subq_exit;
}
}
/* Cache constant expressions in WHERE, HAVING, ON clauses. */
if (!plan_is_const() && cache_const_exprs())
DBUG_RETURN(1);
// See if this subquery can be evaluated with subselect_indexsubquery_engine
if (const int ret= replace_index_subquery())
{
set_plan_state(PLAN_READY);
/*
We leave optimize() because the rest of it is only about order/group
which those subqueries don't have and about setting up plan which
we're not going to use due to different execution method.
*/
DBUG_RETURN(ret < 0);
}
{
/*
If the hint FORCE INDEX FOR ORDER BY/GROUP BY is used for the first
table (it does not make sense for other tables) then we cannot do join
buffering.
*/
if (!plan_is_const())
{
const TABLE * const first= best_ref[const_tables]->table();
if ((first->force_index_order && order) ||
(first->force_index_group && group_list))
no_jbuf_after= 0;
}
bool simple_sort= true;
// Check whether join cache could be used
for (uint i= const_tables; i < tables; i++)
{
JOIN_TAB *const tab= best_ref[i];
if (!tab->position())
continue;
if (setup_join_buffering(tab, this, no_jbuf_after))
DBUG_RETURN(true);
if (tab->use_join_cache() != JOIN_CACHE::ALG_NONE)
simple_sort= false;
assert(tab->type() != JT_FT ||
tab->use_join_cache() == JOIN_CACHE::ALG_NONE);
}
if (!simple_sort)
{
/*
A join buffer is used for this table. We here inform the optimizer
that it should not rely on rows of the first non-const table being in
order thanks to an index scan; indeed join buffering of the present
table subsequently changes the order of rows.
*/
simple_order= simple_group= false;
}
}
if (!plan_is_const() && order)
{
/*
Force using of tmp table if sorting by a SP or UDF function due to
their expensive and probably non-deterministic nature.
*/
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
{
Item *item= *tmp_order->item;
if (item->is_expensive())
{
/* Force tmp table without sort */
simple_order= simple_group= false;
break;
}
}
}
/*
Check if we need to create a temporary table.
This has to be done if all tables are not already read (const tables)
and one of the following conditions holds:
- We are using DISTINCT (simple distinct's have already been optimized away)
- We are using an ORDER BY or GROUP BY on fields not in the first table
- We are using different ORDER BY and GROUP BY orders
- The user wants us to buffer the result.
When the WITH ROLLUP modifier is present, we cannot skip temporary table
creation for the DISTINCT clause just because there are only const tables.
*/
need_tmp= ((!plan_is_const() &&
((select_distinct || (order && !simple_order) ||
(group_list && !simple_group)) ||
(group_list && order) ||
(select_lex->active_options() & OPTION_BUFFER_RESULT))) ||
(rollup.state != ROLLUP::STATE_NONE && select_distinct));
DBUG_EXECUTE("info", TEST_join(this););
if (!plan_is_const())
{
JOIN_TAB *tab= best_ref[const_tables];
/*
Because filesort always does a full table scan or a quick range scan
we must add the removed reference to the select for the table.
We only need to do this when we have a simple_order or simple_group
as in other cases the join is done before the sort.
*/
if ((order || group_list) &&
tab->type() != JT_ALL &&
tab->type() != JT_FT &&
tab->type() != JT_REF_OR_NULL &&
((order && simple_order) || (group_list && simple_group)))
{
if (add_ref_to_table_cond(thd,tab)) {
DBUG_RETURN(1);
}
}
// Test if we can use an index instead of sorting
test_skip_sort();
}
if (alloc_qep(tables))
DBUG_RETURN(error= 1); /* purecov: inspected */
if (make_join_readinfo(this, no_jbuf_after))
DBUG_RETURN(1); /* purecov: inspected */
if (make_tmp_tables_info())
DBUG_RETURN(1);
// At this stage, we have fully set QEP_TABs; JOIN_TABs are unaccessible,
// pushed joins(see below) are still allowed to change the QEP_TABs
/*
Push joins to handlerton(s)
The handlerton(s) will inspect the QEP through the
AQP (Abstract Query Plan) and extract from it whatever
it might implement of pushed execution.
It is the responsibility of the handler:
- to store any information it need for later
execution of pushed queries.
- to call appropriate AQP functions which modifies the
QEP to use the special 'linked' read functions
for those parts of the join which have been pushed.
Currently pushed joins are only implemented by NDB.
It only make sense to try pushing if > 1 non-const tables.
*/
if (!plan_is_single_table() && !plan_is_const())
{
const AQP::Join_plan plan(this);
if (ha_make_pushed_joins(thd, &plan))
DBUG_RETURN(1);
}
// Update m_current_query_cost to reflect actual need of filesort.
if (sort_cost > 0.0 && !explain_flags.any(ESP_USING_FILESORT))
{
best_read-= sort_cost;
sort_cost= 0.0;
if (thd->lex->is_single_level_stmt())
thd->m_current_query_cost= best_read;
}
count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
// Make plan visible for EXPLAIN
set_plan_state(PLAN_READY);
DEBUG_SYNC(thd, "after_join_optimize");
error= 0;
DBUG_RETURN(0);
setup_subq_exit:
assert(zero_result_cause != NULL);
/*
Even with zero matching rows, subqueries in the HAVING clause may
need to be evaluated if there are aggregate functions in the
query. If this JOIN is part of an outer query, subqueries in HAVING may
be evaluated several times in total; so subquery materialization makes
sense.
*/
child_subquery_can_materialize= true;
trace_steps.end(); // because all steps are done
Opt_trace_object(trace, "empty_result")
.add_alnum("cause", zero_result_cause);
having_for_explain= having_cond;
error= 0;
if (!qep_tab && best_ref)
{
/*
After creation of JOIN_TABs in make_join_plan(), we have shortcut due to
some zero_result_cause. For simplification, if we have JOIN_TABs we
want QEP_TABs too.
*/
if (alloc_qep(tables))
DBUG_RETURN(1); /* purecov: inspected */
unplug_join_tabs();
}
set_plan_state(ZERO_RESULT);
DBUG_RETURN(0);
}
//TIANMU UPGRADE
if (part == 1)
{
error= 0;
DBUG_RETURN(0);
}
遇到的不解的地方记录:
一. STONEDB引擎调用SQL层的JOIN::optimize, 为什么将参数part设置为1
二. 为何要魔改SQL层的JOIN::optimize, part为1的情况下, 不继续执行后续的查询优化
2.1 TIANMU UPGRADE的注释是什么意思? 为什么不继续执行后续逻辑
2.2 part的语义, 是否还和mysql原始代码中的语义相同?
2.3 make_join_plan的逻辑在TIANMU UPGRADE的逻辑之后, 无法执行相关join优化
- JOIN::decide_subquery_strategy 通过代价计算判定是将IN转换为EXISTS还是使用物化
- Item_in_subselect::finalize_exists_transform 完成将IN转换为EXISTS