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.
This commit is contained in:
Monty 2020-04-06 15:41:33 +03:00
parent f9f33b85be
commit 91ffdc8380
4 changed files with 21 additions and 13 deletions

View File

@ -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 The problem is that since those attributes are not set in merge
children, another round of PREPARE will not help. children, another round of PREPARE will not help.
*/ */
error= thd->open_temporary_table(tables); if (!thd->has_temporary_tables() ||
(!(error= thd->open_temporary_table(tables)) &&
if (!error && !tables->table) !tables->table))
error= open_table(thd, tables, ot_ctx); error= open_table(thd, tables, ot_ctx);
thd->pop_internal_handler(); 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; Repair_mrg_table_error_handler repair_mrg_table_handler;
thd->push_internal_handler(&repair_mrg_table_handler); thd->push_internal_handler(&repair_mrg_table_handler);
error= thd->open_temporary_table(tables); if (!thd->has_temporary_tables() ||
(!(error= thd->open_temporary_table(tables)) &&
if (!error && !tables->table) !tables->table))
error= open_table(thd, tables, ot_ctx); error= open_table(thd, tables, ot_ctx);
thd->pop_internal_handler(); thd->pop_internal_handler();
@ -3727,6 +3727,7 @@ 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 still might need to look for a temporary table if this table
list element corresponds to underlying table of a merge table. list element corresponds to underlying table of a merge table.
*/ */
if (thd->has_temporary_tables())
error= thd->open_temporary_table(tables); error= thd->open_temporary_table(tables);
} }

View File

@ -4851,6 +4851,7 @@ public:
TMP_TABLE_ANY TMP_TABLE_ANY
}; };
bool has_thd_temporary_tables(); bool has_thd_temporary_tables();
bool has_temporary_tables();
TABLE *create_and_open_tmp_table(LEX_CUSTRING *frm, TABLE *create_and_open_tmp_table(LEX_CUSTRING *frm,
const char *path, const char *path,
@ -4889,7 +4890,6 @@ private:
/* Whether a lock has been acquired? */ /* Whether a lock has been acquired? */
bool m_tmp_tables_locked; bool m_tmp_tables_locked;
bool has_temporary_tables();
uint create_tmp_table_def_key(char *key, const char *db, uint create_tmp_table_def_key(char *key, const char *db,
const char *table_name); const char *table_name);
TMP_TABLE_SHARE *create_temporary_table(LEX_CUSTRING *frm, TMP_TABLE_SHARE *create_temporary_table(LEX_CUSTRING *frm,

View File

@ -834,6 +834,7 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
st_select_lex_unit *res= NULL; st_select_lex_unit *res= NULL;
Query_arena backup; Query_arena backup;
Query_arena *arena= thd->activate_stmt_arena_if_needed(&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)) 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= lex->query_tables;
spec_tables_tail= 0; spec_tables_tail= 0;
has_tmp_tables= thd->has_temporary_tables();
for (TABLE_LIST *tbl= spec_tables; for (TABLE_LIST *tbl= spec_tables;
tbl; tbl;
tbl= tbl->next_global) tbl= tbl->next_global)
{ {
if (!tbl->derived && !tbl->schema_table && if (has_tmp_tables && !tbl->derived && !tbl->schema_table &&
thd->open_temporary_table(tbl)) thd->open_temporary_table(tbl))
goto err; goto err;
spec_tables_tail= tbl; spec_tables_tail= tbl;

View File

@ -338,9 +338,11 @@ bool THD::open_temporary_table(TABLE_LIST *tl)
have invalid db or table name. have invalid db or table name.
Instead THD::open_tables() should be used. 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_PRINT("info", ("skip_temporary is set or no temporary tables"));
DBUG_RETURN(false); DBUG_RETURN(false);
@ -452,10 +454,13 @@ bool THD::open_temporary_table(TABLE_LIST *tl)
*/ */
bool THD::open_temporary_tables(TABLE_LIST *tl) bool THD::open_temporary_tables(TABLE_LIST *tl)
{ {
TABLE_LIST *first_not_own;
DBUG_ENTER("THD::open_temporary_tables"); 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; for (TABLE_LIST *table= tl; table && table != first_not_own;
table= table->next_global) table= table->next_global)
{ {
@ -868,7 +873,7 @@ void THD::restore_tmp_table_share(TMP_TABLE_SHARE *share)
@return false Temporary tables exist @return false Temporary tables exist
true No temporary table exist true No temporary table exist
*/ */
inline bool THD::has_temporary_tables() bool THD::has_temporary_tables()
{ {
DBUG_ENTER("THD::has_temporary_tables"); DBUG_ENTER("THD::has_temporary_tables");
bool result= (rgi_slave bool result= (rgi_slave