diff options
-rw-r--r-- | mysql-test/r/ps.result | 1 | ||||
-rw-r--r-- | mysql-test/t/ps.test | 1 | ||||
-rw-r--r-- | sql/sql_explain.cc | 6 | ||||
-rw-r--r-- | sql/sql_explain.h | 47 | ||||
-rw-r--r-- | sql/sql_select.cc | 39 | ||||
-rw-r--r-- | sql/sql_select.h | 2 |
6 files changed, 79 insertions, 17 deletions
diff --git a/mysql-test/r/ps.result b/mysql-test/r/ps.result index 31fcea528aa..3217a10ed6d 100644 --- a/mysql-test/r/ps.result +++ b/mysql-test/r/ps.result @@ -1,5 +1,6 @@ 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 database if exists mysqltest1; drop database if exists client_test_db; create table t1 ( diff --git a/mysql-test/t/ps.test b/mysql-test/t/ps.test index 0233b2e428b..491594a3045 100644 --- a/mysql-test/t/ps.test +++ b/mysql-test/t/ps.test @@ -7,6 +7,7 @@ call mtr.add_suppression('Unsafe statement written to the binary log using state --disable_warnings drop table if exists t1,t2,t3,t4; +drop database if exists mysqltest1; # Avoid wrong warnings if mysql_client_test fails drop database if exists client_test_db; --enable_warnings diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc index 47581898e42..1402c76db01 100644 --- a/sql/sql_explain.cc +++ b/sql/sql_explain.cc @@ -542,7 +542,7 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai /* `r_rows` */ 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, MY_INT64_NUM_DECIMAL_DIGITS)); } @@ -562,8 +562,8 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai if (is_analyze) { double r_filtered; - if (r_rows > 0) - r_filtered= 100.0 * (double)r_rows_after_table_cond / r_rows; + if (tracker.r_rows > 0) + r_filtered= 100.0 * (double)tracker.r_rows_after_table_cond / tracker.r_rows; else r_filtered= 100.0; item_list.push_back(new Item_float(r_filtered, 2)); diff --git a/sql/sql_explain.h b/sql/sql_explain.h index a6b41414c5d..7970d778a21 100644 --- a/sql/sql_explain.h +++ b/sql/sql_explain.h @@ -14,6 +14,21 @@ along with this program; if not, write to the Free Software 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 @@ public: int print_explain(Explain_query *query, select_result_sink *output, 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 @@ public: const char *fake_select_type; 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; class Explain_delete; class Explain_insert; + /* Explain structure for a query (i.e. a statement). @@ -389,6 +425,7 @@ private: /* EXPLAIN data structure for a single JOIN_TAB. */ + class Explain_table_access : public Sql_alloc { public: @@ -467,15 +504,7 @@ public: bool using_temporary, bool using_filesort); /* ANALYZE members*/ - 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 */ - - Explain_table_access(): - r_scans(0), r_rows(0), r_rows_after_table_cond(0), - r_rows_after_where(0) - {} + Table_access_tracker tracker; private: void append_tag_name(String *str, enum explain_extra_tag tag); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 6b8fda7bd82..6090eb31487 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1032,6 +1032,9 @@ int JOIN::optimize() subquery), returns 1 - another JOIN::optimize() call made, and now join->optimize() will 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) { @@ -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, 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) join_tab->read_first_record= join_init_read_record; join_tab->join= this; 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)); temp_table->status=0; temp_table->null_row=0; @@ -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); DBUG_RETURN(nls); } - join_tab->explain->r_scans++; + join_tab->tracker->r_scans++; int error; enum_nested_loop_state rc= NESTED_LOOP_OK; @@ -17669,7 +17700,7 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */ } - join_tab->explain->r_rows++; + join_tab->tracker->r_rows++; if (join_tab->table->vfield) update_virtual_fields(join->thd, join_tab->table); @@ -17689,7 +17720,7 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, There is no select condition or the attached pushed down 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; while (join_tab->first_unmatched && found) { @@ -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->quick_info= NULL; - tab->explain= eta; + tab->tracker= &eta->tracker; /* id */ if (tab->bush_root_tab) diff --git a/sql/sql_select.h b/sql/sql_select.h index 66d7b007bbb..e31767f8835 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -251,7 +251,7 @@ typedef struct st_join_table { /* Special content for EXPLAIN 'Extra' column or NULL if none */ 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' column, or 0 if there is no info. |