From 91ffdc8380ab48a2d57dc81472beb80af115318f Mon Sep 17 00:00:00 2001 From: Monty Date: Mon, 6 Apr 2020 15:41:33 +0300 Subject: [PATCH] Don't try to open temporary tables if there are no temporary tables. This was done to increase performance when not using temporary tables as checking if a table is a temporary table involves a lot of code. --- sql/sql_base.cc | 15 ++++++++------- sql/sql_class.h | 2 +- sql/sql_cte.cc | 4 +++- sql/temporary_tables.cc | 13 +++++++++---- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 1b6d8834e7e..13dc9aca177 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -3691,9 +3691,9 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags, The problem is that since those attributes are not set in merge children, another round of PREPARE will not help. */ - error= thd->open_temporary_table(tables); - - if (!error && !tables->table) + if (!thd->has_temporary_tables() || + (!(error= thd->open_temporary_table(tables)) && + !tables->table)) error= open_table(thd, tables, ot_ctx); thd->pop_internal_handler(); @@ -3710,9 +3710,9 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags, Repair_mrg_table_error_handler repair_mrg_table_handler; thd->push_internal_handler(&repair_mrg_table_handler); - error= thd->open_temporary_table(tables); - - if (!error && !tables->table) + if (!thd->has_temporary_tables() || + (!(error= thd->open_temporary_table(tables)) && + !tables->table)) error= open_table(thd, tables, ot_ctx); thd->pop_internal_handler(); @@ -3727,7 +3727,8 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags, still might need to look for a temporary table if this table list element corresponds to underlying table of a merge table. */ - error= thd->open_temporary_table(tables); + if (thd->has_temporary_tables()) + error= thd->open_temporary_table(tables); } if (!error && !tables->table) diff --git a/sql/sql_class.h b/sql/sql_class.h index 816935ba2ce..08a1a4cf2c8 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -4851,6 +4851,7 @@ public: TMP_TABLE_ANY }; bool has_thd_temporary_tables(); + bool has_temporary_tables(); TABLE *create_and_open_tmp_table(LEX_CUSTRING *frm, const char *path, @@ -4889,7 +4890,6 @@ private: /* Whether a lock has been acquired? */ bool m_tmp_tables_locked; - bool has_temporary_tables(); uint create_tmp_table_def_key(char *key, const char *db, const char *table_name); TMP_TABLE_SHARE *create_temporary_table(LEX_CUSTRING *frm, diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc index 93344956468..1d3226fef34 100644 --- a/sql/sql_cte.cc +++ b/sql/sql_cte.cc @@ -834,6 +834,7 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd, st_select_lex_unit *res= NULL; Query_arena backup; Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup); + bool has_tmp_tables; if (!(lex= (LEX*) new(thd->mem_root) st_lex_local)) { @@ -879,11 +880,12 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd, spec_tables= lex->query_tables; spec_tables_tail= 0; + has_tmp_tables= thd->has_temporary_tables(); for (TABLE_LIST *tbl= spec_tables; tbl; tbl= tbl->next_global) { - if (!tbl->derived && !tbl->schema_table && + if (has_tmp_tables && !tbl->derived && !tbl->schema_table && thd->open_temporary_table(tbl)) goto err; spec_tables_tail= tbl; diff --git a/sql/temporary_tables.cc b/sql/temporary_tables.cc index 407072a7b49..9491a23793b 100644 --- a/sql/temporary_tables.cc +++ b/sql/temporary_tables.cc @@ -338,9 +338,11 @@ bool THD::open_temporary_table(TABLE_LIST *tl) have invalid db or table name. Instead THD::open_tables() should be used. */ - DBUG_ASSERT(!tl->derived && !tl->schema_table); + DBUG_ASSERT(!tl->derived); + DBUG_ASSERT(!tl->schema_table); + DBUG_ASSERT(has_temporary_tables()); - if (tl->open_type == OT_BASE_ONLY || !has_temporary_tables()) + if (tl->open_type == OT_BASE_ONLY) { DBUG_PRINT("info", ("skip_temporary is set or no temporary tables")); DBUG_RETURN(false); @@ -452,10 +454,13 @@ bool THD::open_temporary_table(TABLE_LIST *tl) */ bool THD::open_temporary_tables(TABLE_LIST *tl) { + TABLE_LIST *first_not_own; DBUG_ENTER("THD::open_temporary_tables"); - TABLE_LIST *first_not_own= lex->first_not_own_table(); + if (!has_temporary_tables()) + DBUG_RETURN(0); + first_not_own= lex->first_not_own_table(); for (TABLE_LIST *table= tl; table && table != first_not_own; table= table->next_global) { @@ -868,7 +873,7 @@ void THD::restore_tmp_table_share(TMP_TABLE_SHARE *share) @return false Temporary tables exist true No temporary table exist */ -inline bool THD::has_temporary_tables() +bool THD::has_temporary_tables() { DBUG_ENTER("THD::has_temporary_tables"); bool result= (rgi_slave