Commit 5621aa32 authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-406: ANALYZE $stmt

- Support tracking for UNIONs, temporary-table based ORDER BYs,
  and both.
parent 0925ab9d
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.'); call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
drop table if exists t1,t2,t3,t4; drop table if exists t1,t2,t3,t4;
drop database if exists mysqltest1;
drop database if exists client_test_db; drop database if exists client_test_db;
create table t1 create table t1
( (
......
...@@ -7,6 +7,7 @@ call mtr.add_suppression('Unsafe statement written to the binary log using state ...@@ -7,6 +7,7 @@ call mtr.add_suppression('Unsafe statement written to the binary log using state
--disable_warnings --disable_warnings
drop table if exists t1,t2,t3,t4; drop table if exists t1,t2,t3,t4;
drop database if exists mysqltest1;
# Avoid wrong warnings if mysql_client_test fails # Avoid wrong warnings if mysql_client_test fails
drop database if exists client_test_db; drop database if exists client_test_db;
--enable_warnings --enable_warnings
......
...@@ -542,7 +542,7 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai ...@@ -542,7 +542,7 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
/* `r_rows` */ /* `r_rows` */
if (is_analyze) if (is_analyze)
{ {
ha_rows avg_rows= r_scans ? round((double) r_rows / r_scans): 0; ha_rows avg_rows= tracker.r_scans ? round((double) tracker.r_rows / tracker.r_scans): 0;
item_list.push_back(new Item_int((longlong) (ulonglong) avg_rows, item_list.push_back(new Item_int((longlong) (ulonglong) avg_rows,
MY_INT64_NUM_DECIMAL_DIGITS)); MY_INT64_NUM_DECIMAL_DIGITS));
} }
...@@ -562,8 +562,8 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai ...@@ -562,8 +562,8 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
if (is_analyze) if (is_analyze)
{ {
double r_filtered; double r_filtered;
if (r_rows > 0) if (tracker.r_rows > 0)
r_filtered= 100.0 * (double)r_rows_after_table_cond / r_rows; r_filtered= 100.0 * (double)tracker.r_rows_after_table_cond / tracker.r_rows;
else else
r_filtered= 100.0; r_filtered= 100.0;
item_list.push_back(new Item_float(r_filtered, 2)); item_list.push_back(new Item_float(r_filtered, 2));
......
...@@ -14,6 +14,21 @@ ...@@ -14,6 +14,21 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* Data structures for ANALYZE */
class Table_access_tracker
{
public:
Table_access_tracker() :
r_scans(0), r_rows(0), r_rows_after_table_cond(0),
r_rows_after_where(0)
{}
ha_rows r_scans; /* How many scans were ran on this join_tab */
ha_rows r_rows; /* How many rows we've got after that */
ha_rows r_rows_after_table_cond; /* Rows after applying the table condition */
ha_rows r_rows_after_where; /* Rows after applying attached part of WHERE */
};
/************************************************************************************** /**************************************************************************************
...@@ -135,6 +150,13 @@ class Explain_select : public Explain_node ...@@ -135,6 +150,13 @@ class Explain_select : public Explain_node
int print_explain(Explain_query *query, select_result_sink *output, int print_explain(Explain_query *query, select_result_sink *output,
uint8 explain_flags, bool is_analyze); uint8 explain_flags, bool is_analyze);
Table_access_tracker *get_using_temporary_read_tracker()
{
return &using_temporary_read_tracker;
}
private:
Table_access_tracker using_temporary_read_tracker;
}; };
...@@ -176,6 +198,19 @@ class Explain_union : public Explain_node ...@@ -176,6 +198,19 @@ class Explain_union : public Explain_node
const char *fake_select_type; const char *fake_select_type;
bool using_filesort; bool using_filesort;
Table_access_tracker *get_fake_select_lex_tracker()
{
return &fake_select_lex_tracker;
}
Table_access_tracker *get_tmptable_read_tracker()
{
return &tmptable_read_tracker;
}
private:
Table_access_tracker fake_select_lex_tracker;
/* This one is for reading after ORDER BY */
Table_access_tracker tmptable_read_tracker;
}; };
...@@ -183,6 +218,7 @@ class Explain_update; ...@@ -183,6 +218,7 @@ class Explain_update;
class Explain_delete; class Explain_delete;
class Explain_insert; class Explain_insert;
/* /*
Explain structure for a query (i.e. a statement). Explain structure for a query (i.e. a statement).
...@@ -389,6 +425,7 @@ class Explain_quick_select : public Sql_alloc ...@@ -389,6 +425,7 @@ class Explain_quick_select : public Sql_alloc
/* /*
EXPLAIN data structure for a single JOIN_TAB. EXPLAIN data structure for a single JOIN_TAB.
*/ */
class Explain_table_access : public Sql_alloc class Explain_table_access : public Sql_alloc
{ {
public: public:
...@@ -467,15 +504,7 @@ class Explain_table_access : public Sql_alloc ...@@ -467,15 +504,7 @@ class Explain_table_access : public Sql_alloc
bool using_temporary, bool using_filesort); bool using_temporary, bool using_filesort);
/* ANALYZE members*/ /* ANALYZE members*/
ha_rows r_scans; /* How many scans were ran on this join_tab */ Table_access_tracker tracker;
ha_rows r_rows; /* How many rows we've got after that */
ha_rows r_rows_after_table_cond; /* Rows after applying the table condition */
ha_rows r_rows_after_where; /* Rows after applying attached part of WHERE */
Explain_table_access():
r_scans(0), r_rows(0), r_rows_after_table_cond(0),
r_rows_after_where(0)
{}
private: private:
void append_tag_name(String *str, enum explain_extra_tag tag); void append_tag_name(String *str, enum explain_extra_tag tag);
......
...@@ -1032,6 +1032,9 @@ int JOIN::optimize() ...@@ -1032,6 +1032,9 @@ int JOIN::optimize()
subquery), returns 1 subquery), returns 1
- another JOIN::optimize() call made, and now join->optimize() will - another JOIN::optimize() call made, and now join->optimize() will
return 0, even though we never had a query plan. return 0, even though we never had a query plan.
Can have QEP_NOT_PRESENT_YET for degenerate queries (for example,
SELECT * FROM tbl LIMIT 0)
*/ */
if (was_optimized != optimized && !res && have_query_plan != QEP_DELETED) if (was_optimized != optimized && !res && have_query_plan != QEP_DELETED)
{ {
...@@ -2350,6 +2353,20 @@ void JOIN::save_explain_data(Explain_query *output, bool can_overwrite, ...@@ -2350,6 +2353,20 @@ void JOIN::save_explain_data(Explain_query *output, bool can_overwrite,
} }
save_explain_data_intern(thd->lex->explain, need_tmp_table, need_order, save_explain_data_intern(thd->lex->explain, need_tmp_table, need_order,
distinct, message); distinct, message);
return;
}
/*
Can have join_tab==NULL for degenerate cases (e.g. SELECT .. UNION ... SELECT LIMIT 0)
*/
if (select_lex == select_lex->master_unit()->fake_select_lex && join_tab)
{
/*
This is fake_select_lex. It has no query plan, but we need to set up a
tracker for ANALYZE
*/
Explain_union *eu= output->get_union(select_lex->master_unit()->first_select()->select_number);
join_tab[0].tracker= eu->get_fake_select_lex_tracker();
} }
} }
...@@ -8958,6 +8975,20 @@ JOIN::make_simple_join(JOIN *parent, TABLE *temp_table) ...@@ -8958,6 +8975,20 @@ JOIN::make_simple_join(JOIN *parent, TABLE *temp_table)
join_tab->read_first_record= join_init_read_record; join_tab->read_first_record= join_init_read_record;
join_tab->join= this; join_tab->join= this;
join_tab->ref.key_parts= 0; join_tab->ref.key_parts= 0;
uint select_nr= select_lex->select_number;
if (select_nr == INT_MAX)
{
/* this is a fake_select_lex of a union */
select_nr= select_lex->master_unit()->first_select()->select_number;
join_tab->tracker= thd->lex->explain->get_union(select_nr)->
get_tmptable_read_tracker();
}
else
{
join_tab->tracker= thd->lex->explain->get_select(select_nr)->
get_using_temporary_read_tracker();
}
bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record)); bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
temp_table->status=0; temp_table->status=0;
temp_table->null_row=0; temp_table->null_row=0;
...@@ -17532,7 +17563,7 @@ sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records) ...@@ -17532,7 +17563,7 @@ sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
(*join_tab->next_select)(join,join_tab+1,end_of_records); (*join_tab->next_select)(join,join_tab+1,end_of_records);
DBUG_RETURN(nls); DBUG_RETURN(nls);
} }
join_tab->explain->r_scans++; join_tab->tracker->r_scans++;
int error; int error;
enum_nested_loop_state rc= NESTED_LOOP_OK; enum_nested_loop_state rc= NESTED_LOOP_OK;
...@@ -17669,7 +17700,7 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, ...@@ -17669,7 +17700,7 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */ DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */
} }
join_tab->explain->r_rows++; join_tab->tracker->r_rows++;
if (join_tab->table->vfield) if (join_tab->table->vfield)
update_virtual_fields(join->thd, join_tab->table); update_virtual_fields(join->thd, join_tab->table);
...@@ -17689,7 +17720,7 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, ...@@ -17689,7 +17720,7 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
There is no select condition or the attached pushed down There is no select condition or the attached pushed down
condition is true => a match is found. condition is true => a match is found.
*/ */
join_tab->explain->r_rows_after_table_cond++; join_tab->tracker->r_rows_after_table_cond++;
bool found= 1; bool found= 1;
while (join_tab->first_unmatched && found) while (join_tab->first_unmatched && found)
{ {
...@@ -23315,7 +23346,7 @@ int JOIN::save_explain_data_intern(Explain_query *output, bool need_tmp_table, ...@@ -23315,7 +23346,7 @@ int JOIN::save_explain_data_intern(Explain_query *output, bool need_tmp_table,
eta->key.set(thd->mem_root, NULL, (uint)-1); eta->key.set(thd->mem_root, NULL, (uint)-1);
eta->quick_info= NULL; eta->quick_info= NULL;
tab->explain= eta; tab->tracker= &eta->tracker;
/* id */ /* id */
if (tab->bush_root_tab) if (tab->bush_root_tab)
......
...@@ -251,7 +251,7 @@ typedef struct st_join_table { ...@@ -251,7 +251,7 @@ typedef struct st_join_table {
/* Special content for EXPLAIN 'Extra' column or NULL if none */ /* Special content for EXPLAIN 'Extra' column or NULL if none */
enum explain_extra_tag info; enum explain_extra_tag info;
Explain_table_access *explain; Table_access_tracker *tracker;
/* /*
Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra' Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra'
column, or 0 if there is no info. column, or 0 if there is no info.
......
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