diff --git a/sql/sql_optimizer.cc b/sql/sql_optimizer.cc index 03f6d3f5625..5265045c9ac 100644 --- a/sql/sql_optimizer.cc +++ b/sql/sql_optimizer.cc @@ -5178,6 +5178,8 @@ void JOIN::set_prefix_tables() { table_map saved_tables_map = (table_map)0; JOIN_TAB *last_non_sjm_tab = nullptr; // Track the last non-sjm table + // Track the last table of sj_nest, key is the idx of first sj inner tables. + std::unordered_map last_sj_tab; for (uint i = const_tables; i < tables; i++) { JOIN_TAB *const tab = best_ref[i]; @@ -5219,6 +5221,10 @@ void JOIN::set_prefix_tables() { current_tables_map |= tab->table_ref->map(); tab->set_prefix_tables(current_tables_map, prev_tables_map); prev_tables_map = current_tables_map; + + // If this is a sj_inner tab, update the last_sj_tab. + if (tab->get_sj_strategy() != SJ_OPT_NONE) + last_sj_tab[tab->first_sj_inner()] = tab; } } /* @@ -5227,6 +5233,14 @@ void JOIN::set_prefix_tables() { */ if (last_non_sjm_tab != nullptr) last_non_sjm_tab->add_prefix_tables(RAND_TABLE_BIT); + + /* + At the end of each semi-join nest, + add non-deterministic expressions to the last table of the nest: + */ + for (auto iter : last_sj_tab) { + iter.second->add_prefix_tables(RAND_TABLE_BIT); + } } /**