Commit 1951d40a authored by Sergey Petrunya's avatar Sergey Petrunya

[SHOW] EXPLAIN UPDATE/DELETE, code re-structuring

- Make EXPLAIN UPDATE/DELETE use "Query Plan Footprints", too.
parent 1c6fc3f6
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
QPF_query::QPF_query() QPF_query::QPF_query()
{ {
upd_del_plan= NULL;
memset(&unions, 0, sizeof(unions)); memset(&unions, 0, sizeof(unions));
memset(&selects, 0, sizeof(selects)); memset(&selects, 0, sizeof(selects));
} }
...@@ -19,6 +20,7 @@ QPF_query::QPF_query() ...@@ -19,6 +20,7 @@ QPF_query::QPF_query()
QPF_query::~QPF_query() QPF_query::~QPF_query()
{ {
delete upd_del_plan;
uint i; uint i;
for (i=0 ; i < MAX_TABLES; i++) for (i=0 ; i < MAX_TABLES; i++)
delete unions[i]; delete unions[i];
...@@ -70,9 +72,17 @@ void QPF_query::add_node(QPF_node *node) ...@@ -70,9 +72,17 @@ void QPF_query::add_node(QPF_node *node)
int QPF_query::print_explain(select_result_sink *output, int QPF_query::print_explain(select_result_sink *output,
uint8 explain_flags) uint8 explain_flags)
{ {
// Start with id=1 if (upd_del_plan)
QPF_node *node= get_node(1); {
return node->print_explain(this, output, explain_flags); upd_del_plan->print_explain(output, explain_flags);
return 0;
}
else
{
// Start with id=1
QPF_node *node= get_node(1);
return node->print_explain(this, output, explain_flags);
}
} }
......
...@@ -161,6 +161,9 @@ public: ...@@ -161,6 +161,9 @@ public:
/* This will return a select (even if there is a union with this id) */ /* This will return a select (even if there is a union with this id) */
QPF_select *get_select(uint select_id); QPF_select *get_select(uint select_id);
/* Delete_plan inherits from Update_plan */
Update_plan *upd_del_plan;
private: private:
QPF_union *unions[MAX_TABLES]; QPF_union *unions[MAX_TABLES];
...@@ -269,10 +272,10 @@ public: ...@@ -269,10 +272,10 @@ public:
key_map range_checked_map; key_map range_checked_map;
// valid with ET_USING_MRR // valid with ET_USING_MRR
StringBuffer <64> mrr_type; StringBuffer<64> mrr_type;
// valid with ET_USING_JOIN_BUFFER // valid with ET_USING_JOIN_BUFFER
StringBuffer <64> join_buffer_type; StringBuffer<64> join_buffer_type;
//TABLE *firstmatch_table; //TABLE *firstmatch_table;
StringBuffer<64> firstmatch_table_name; StringBuffer<64> firstmatch_table_name;
......
This diff is collapsed.
...@@ -448,7 +448,6 @@ void lex_start(THD *thd) ...@@ -448,7 +448,6 @@ void lex_start(THD *thd)
lex->thd= lex->unit.thd= thd; lex->thd= lex->unit.thd= thd;
lex->upd_del_plan= NULL;
lex->context_stack.empty(); lex->context_stack.empty();
lex->unit.init_query(); lex->unit.init_query();
lex->unit.init_select(); lex->unit.init_select();
...@@ -2561,7 +2560,6 @@ LEX::LEX() ...@@ -2561,7 +2560,6 @@ LEX::LEX()
INITIAL_LEX_PLUGIN_LIST_SIZE, 0); INITIAL_LEX_PLUGIN_LIST_SIZE, 0);
reset_query_tables_list(TRUE); reset_query_tables_list(TRUE);
mi.init(); mi.init();
upd_del_plan= NULL;
} }
...@@ -4176,11 +4174,11 @@ bool st_select_lex::is_merged_child_of(st_select_lex *ancestor) ...@@ -4176,11 +4174,11 @@ bool st_select_lex::is_merged_child_of(st_select_lex *ancestor)
int LEX::print_explain(select_result_sink *output, uint8 explain_flags, int LEX::print_explain(select_result_sink *output, uint8 explain_flags,
bool *printed_anything) bool *printed_anything)
{ {
if (upd_del_plan) /* if (upd_del_plan)
{ {
upd_del_plan->print_explain(output, explain_flags, printed_anything); upd_del_plan->print_explain(output, explain_flags, printed_anything);
return 0; return 0;
} }*/
//int res= unit.print_explain(output, explain_flags, printed_anything); //int res= unit.print_explain(output, explain_flags, printed_anything);
//psergey-todo: here, we should make Query Plan Footprint, and then produce //psergey-todo: here, we should make Query Plan Footprint, and then produce
......
...@@ -2378,6 +2378,7 @@ public: ...@@ -2378,6 +2378,7 @@ public:
SQL_SELECT *select; SQL_SELECT *select;
uint index; uint index;
ha_rows table_rows; /* Use if select==NULL */ ha_rows table_rows; /* Use if select==NULL */
/* /*
Top-level select_lex. Most of its fields are not used, we need it only to Top-level select_lex. Most of its fields are not used, we need it only to
get to the subqueries. get to the subqueries.
...@@ -2390,11 +2391,20 @@ public: ...@@ -2390,11 +2391,20 @@ public:
/* Set this plan to be a plan to do nothing because of impossible WHRE*/ /* Set this plan to be a plan to do nothing because of impossible WHRE*/
void set_impossible_where() { impossible_where= true; } void set_impossible_where() { impossible_where= true; }
virtual int print_explain(select_result_sink *output, uint8 explain_flags, virtual int print_explain(select_result_sink *output, uint8 explain_flags);
bool *printed_anything);
virtual ~Update_plan() {} virtual ~Update_plan() {}
Update_plan() : impossible_where(false), using_filesort(false) {} Update_plan() : impossible_where(false), using_filesort(false) {}
void save_query_plan_footprint();
/* Query Plan Footprint fields */
// cant use it here: enum join_type
int jtype;
bool using_where;
StringBuffer<128> possible_keys_line;
StringBuffer<128> key_str;
StringBuffer<128> key_len_str;
StringBuffer<64> mrr_type;
}; };
...@@ -2414,8 +2424,7 @@ public: ...@@ -2414,8 +2424,7 @@ public:
deleting_all_rows= true; deleting_all_rows= true;
table_rows= rows_arg; table_rows= rows_arg;
} }
int print_explain(select_result_sink *output, uint8 explain_flags, int print_explain(select_result_sink *output, uint8 explain_flags);
bool *printed_anything);
}; };
...@@ -2432,7 +2441,6 @@ struct LEX: public Query_tables_list ...@@ -2432,7 +2441,6 @@ struct LEX: public Query_tables_list
SELECT_LEX *all_selects_list; SELECT_LEX *all_selects_list;
/* For single-table DELETE: its query plan */ /* For single-table DELETE: its query plan */
Update_plan *upd_del_plan;
QPF_query *query_plan_footprint; QPF_query *query_plan_footprint;
char *length,*dec,*change; char *length,*dec,*change;
......
...@@ -276,9 +276,7 @@ int mysql_update(THD *thd, ...@@ -276,9 +276,7 @@ int mysql_update(THD *thd,
ulonglong id; ulonglong id;
List<Item> all_fields; List<Item> all_fields;
killed_state killed_status= NOT_KILLED; killed_state killed_status= NOT_KILLED;
Update_plan query_plan; Update_plan *query_plan;
query_plan.index= MAX_KEY;
query_plan.using_filesort= FALSE;
bool apc_target_enabled= false; // means was enabled *by code this function* bool apc_target_enabled= false; // means was enabled *by code this function*
DBUG_ENTER("mysql_update"); DBUG_ENTER("mysql_update");
...@@ -317,9 +315,12 @@ int mysql_update(THD *thd, ...@@ -317,9 +315,12 @@ int mysql_update(THD *thd,
/* Calculate "table->covering_keys" based on the WHERE */ /* Calculate "table->covering_keys" based on the WHERE */
table->covering_keys= table->s->keys_in_use; table->covering_keys= table->s->keys_in_use;
table->quick_keys.clear_all(); table->quick_keys.clear_all();
query_plan.select_lex= &thd->lex->select_lex; query_plan= new Update_plan;
query_plan.table= table; query_plan->index= MAX_KEY;
query_plan->using_filesort= FALSE;
query_plan->select_lex= &thd->lex->select_lex;
query_plan->table= table;
#ifndef NO_EMBEDDED_ACCESS_CHECKS #ifndef NO_EMBEDDED_ACCESS_CHECKS
/* Force privilege re-checking for views after they have been opened. */ /* Force privilege re-checking for views after they have been opened. */
want_privilege= (table_list->view ? UPDATE_ACL : want_privilege= (table_list->view ? UPDATE_ACL :
...@@ -378,7 +379,7 @@ int mysql_update(THD *thd, ...@@ -378,7 +379,7 @@ int mysql_update(THD *thd,
if (cond_value == Item::COND_FALSE) if (cond_value == Item::COND_FALSE)
{ {
limit= 0; // Impossible WHERE limit= 0; // Impossible WHERE
query_plan.set_impossible_where(); query_plan->set_impossible_where();
if (thd->lex->describe) if (thd->lex->describe)
goto exit_without_my_ok; goto exit_without_my_ok;
} }
...@@ -411,7 +412,7 @@ int mysql_update(THD *thd, ...@@ -411,7 +412,7 @@ int mysql_update(THD *thd,
if (error || !limit || thd->is_error() || if (error || !limit || thd->is_error() ||
(select && select->check_quick(thd, safe_update, limit))) (select && select->check_quick(thd, safe_update, limit)))
{ {
query_plan.set_impossible_where(); query_plan->set_impossible_where();
if (thd->lex->describe) if (thd->lex->describe)
goto exit_without_my_ok; goto exit_without_my_ok;
...@@ -453,16 +454,16 @@ int mysql_update(THD *thd, ...@@ -453,16 +454,16 @@ int mysql_update(THD *thd,
if (select && select->quick && select->quick->unique_key_range()) if (select && select->quick && select->quick->unique_key_range())
{ // Single row select (always "ordered"): Ok to use with key field UPDATE { // Single row select (always "ordered"): Ok to use with key field UPDATE
need_sort= FALSE; need_sort= FALSE;
query_plan.index= MAX_KEY; query_plan->index= MAX_KEY;
used_key_is_modified= FALSE; used_key_is_modified= FALSE;
} }
else else
{ {
query_plan.index= get_index_for_order(order, table, select, limit, query_plan->index= get_index_for_order(order, table, select, limit,
&need_sort, &reverse); &need_sort, &reverse);
if (select && select->quick) if (select && select->quick)
{ {
DBUG_ASSERT(need_sort || query_plan.index == select->quick->index); DBUG_ASSERT(need_sort || query_plan->index == select->quick->index);
used_key_is_modified= (!select->quick->unique_key_range() && used_key_is_modified= (!select->quick->unique_key_range() &&
select->quick->is_keys_used(table->write_set)); select->quick->is_keys_used(table->write_set));
} }
...@@ -470,11 +471,11 @@ int mysql_update(THD *thd, ...@@ -470,11 +471,11 @@ int mysql_update(THD *thd,
{ {
if (need_sort) if (need_sort)
{ // Assign table scan index to check below for modified key fields: { // Assign table scan index to check below for modified key fields:
query_plan.index= table->file->key_used_on_scan; query_plan->index= table->file->key_used_on_scan;
} }
if (query_plan.index != MAX_KEY) if (query_plan->index != MAX_KEY)
{ // Check if we are modifying a key that we are used to search with: { // Check if we are modifying a key that we are used to search with:
used_key_is_modified= is_key_used(table, query_plan.index, table->write_set); used_key_is_modified= is_key_used(table, query_plan->index, table->write_set);
} }
} }
} }
...@@ -484,10 +485,11 @@ int mysql_update(THD *thd, ...@@ -484,10 +485,11 @@ int mysql_update(THD *thd,
- Save the decisions in the query plan - Save the decisions in the query plan
- if we're running EXPLAIN UPDATE, get out - if we're running EXPLAIN UPDATE, get out
*/ */
query_plan.select= select; query_plan->select= select;
query_plan.possible_keys= table->quick_keys; query_plan->possible_keys= table->quick_keys;
query_plan.table_rows= table->file->stats.records; query_plan->table_rows= table->file->stats.records;
thd->lex->upd_del_plan= &query_plan; thd->lex->query_plan_footprint= new QPF_query;
thd->lex->query_plan_footprint->upd_del_plan= query_plan;
/* /*
Ok, we have generated a query plan for the UPDATE. Ok, we have generated a query plan for the UPDATE.
...@@ -496,7 +498,8 @@ int mysql_update(THD *thd, ...@@ -496,7 +498,8 @@ int mysql_update(THD *thd,
*/ */
if (thd->lex->describe) if (thd->lex->describe)
goto exit_without_my_ok; goto exit_without_my_ok;
query_plan->save_query_plan_footprint();
thd->apc_target.enable(); thd->apc_target.enable();
apc_target_enabled= true; apc_target_enabled= true;
DBUG_EXECUTE_IF("show_explain_probe_update_exec_start", DBUG_EXECUTE_IF("show_explain_probe_update_exec_start",
...@@ -515,8 +518,8 @@ int mysql_update(THD *thd, ...@@ -515,8 +518,8 @@ int mysql_update(THD *thd,
DBUG_ASSERT(table->read_set == &table->def_read_set); DBUG_ASSERT(table->read_set == &table->def_read_set);
DBUG_ASSERT(table->write_set == &table->def_write_set); DBUG_ASSERT(table->write_set == &table->def_write_set);
if (query_plan.index < MAX_KEY && old_covering_keys.is_set(query_plan.index)) if (query_plan->index < MAX_KEY && old_covering_keys.is_set(query_plan->index))
table->add_read_columns_used_by_index(query_plan.index); table->add_read_columns_used_by_index(query_plan->index);
else else
table->use_all_columns(); table->use_all_columns();
...@@ -582,13 +585,13 @@ int mysql_update(THD *thd, ...@@ -582,13 +585,13 @@ int mysql_update(THD *thd,
Full index scan must be started with init_read_record_idx Full index scan must be started with init_read_record_idx
*/ */
if (query_plan.index == MAX_KEY || (select && select->quick)) if (query_plan->index == MAX_KEY || (select && select->quick))
{ {
if (init_read_record(&info, thd, table, select, 0, 1, FALSE)) if (init_read_record(&info, thd, table, select, 0, 1, FALSE))
goto err; goto err;
} }
else else
init_read_record_idx(&info, thd, table, 1, query_plan.index, reverse); init_read_record_idx(&info, thd, table, 1, query_plan->index, reverse);
thd_proc_info(thd, "Searching rows for update"); thd_proc_info(thd, "Searching rows for update");
ha_rows tmp_limit= limit; ha_rows tmp_limit= limit;
...@@ -1002,6 +1005,12 @@ err: ...@@ -1002,6 +1005,12 @@ err:
if (apc_target_enabled) if (apc_target_enabled)
thd->apc_target.disable(); thd->apc_target.disable();
if (thd->lex->query_plan_footprint)
{
delete thd->lex->query_plan_footprint;
thd->lex->query_plan_footprint= NULL;
}
delete select; delete select;
free_underlaid_joins(thd, select_lex); free_underlaid_joins(thd, select_lex);
table->disable_keyread(); table->disable_keyread();
...@@ -1010,22 +1019,25 @@ err: ...@@ -1010,22 +1019,25 @@ err:
exit_without_my_ok: exit_without_my_ok:
DBUG_ASSERT(!apc_target_enabled); DBUG_ASSERT(!apc_target_enabled);
thd->lex->upd_del_plan= &query_plan; query_plan->save_query_plan_footprint();
thd->lex->query_plan_footprint->upd_del_plan= query_plan;
select_send *result; select_send *result;
bool printed_anything;
if (!(result= new select_send())) if (!(result= new select_send()))
return 1; /* purecov: inspected */ return 1; /* purecov: inspected */
List<Item> dummy; /* note: looked in 5.6 and they too use a dummy list like this */ List<Item> dummy; /* note: looked in 5.6 and they too use a dummy list like this */
result->prepare(dummy, &thd->lex->unit); result->prepare(dummy, &thd->lex->unit);
thd->send_explain_fields(result); thd->send_explain_fields(result);
int err2= thd->lex->print_explain(result, 0 /* explain flags*/, &printed_anything); int err2= thd->lex->query_plan_footprint->print_explain(result, 0);
if (err2) if (err2)
result->abort_result_set(); result->abort_result_set();
else else
result->send_eof(); result->send_eof();
delete thd->lex->query_plan_footprint;
thd->lex->query_plan_footprint= NULL;
delete select; delete select;
free_underlaid_joins(thd, select_lex); free_underlaid_joins(thd, select_lex);
DBUG_RETURN((error >= 0 || thd->is_error()) ? 1 : 0); DBUG_RETURN((error >= 0 || thd->is_error()) ? 1 : 0);
......
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