diff --git a/mysql-test/r/subquery_sj_all_bka_nixbnl.result b/mysql-test/r/subquery_sj_all_bka_nixbnl.result index cedb2a6734e..d5549e8890d 100644 --- a/mysql-test/r/subquery_sj_all_bka_nixbnl.result +++ b/mysql-test/r/subquery_sj_all_bka_nixbnl.result @@ -10731,15 +10731,16 @@ FROM t1 AS subquery3_t1 ) ; id select_type table partitions type possible_keys key key_len ref rows filtered Extra -1 PRIMARY subquery3_t1 NULL index col_varchar_key col_varchar_key 9 NULL 0 0.00 Using where; Using index -1 PRIMARY NULL ALL NULL NULL NULL NULL NULL 100.00 NULL -1 PRIMARY NULL ref 9 .7,test.subquery3_t1.col_varchar_key 2 100.00 Using where +1 PRIMARY NULL ALL NULL NULL NULL NULL NULL 0.00 Using where +1 PRIMARY NULL ref 9 const,.subquery3_field1 2 100.00 Using where 1 PRIMARY table2 NULL index NULL PRIMARY 4 NULL 1 100.00 Using index +1 PRIMARY NULL eq_ref 4 table1.col_int_key 1 100.00 NULL +4 MATERIALIZED subquery3_t1 NULL index col_varchar_key col_varchar_key 9 NULL 0 0.00 Using index 3 MATERIALIZED t3 NULL ALL NULL NULL NULL NULL 0 0.00 NULL 2 DERIVED subquery1_t1 NULL ALL PRIMARY NULL NULL NULL 1 100.00 NULL 2 DERIVED subquery1_t2 NULL eq_ref PRIMARY PRIMARY 4 test.subquery1_t1.pk 1 100.00 Using index Warnings: -Note 1003 /* select#1 */ select `table1`.`pk` AS `field1` from (/* select#2 */ select `test`.`subquery1_t1`.`pk` AS `pk`,`test`.`subquery1_t1`.`col_int_key` AS `col_int_key`,`test`.`subquery1_t1`.`col_varchar_key` AS `col_varchar_key`,`test`.`subquery1_t1`.`col_varchar_nokey` AS `col_varchar_nokey` from `test`.`t2` `subquery1_t1` join `test`.`t2` `subquery1_t2` where (`test`.`subquery1_t2`.`pk` = `test`.`subquery1_t1`.`pk`)) `table1` semi join (`test`.`t3`) semi join (`test`.`t1` `subquery3_t1`) straight_join `test`.`t2` `table2` where ((`table1`.`col_int_key` = 7) and (`table1`.`col_varchar_nokey` = `test`.`subquery3_t1`.`col_varchar_key`)) +Note 1003 /* select#1 */ select `table1`.`pk` AS `field1` from (/* select#2 */ select `test`.`subquery1_t1`.`pk` AS `pk`,`test`.`subquery1_t1`.`col_int_key` AS `col_int_key`,`test`.`subquery1_t1`.`col_varchar_key` AS `col_varchar_key`,`test`.`subquery1_t1`.`col_varchar_nokey` AS `col_varchar_nokey` from `test`.`t2` `subquery1_t1` join `test`.`t2` `subquery1_t2` where (`test`.`subquery1_t2`.`pk` = `test`.`subquery1_t1`.`pk`)) `table1` semi join (`test`.`t3`) semi join (`test`.`t1` `subquery3_t1`) straight_join `test`.`t2` `table2` where ((`table1`.`col_int_key` = 7) and (`table1`.`col_varchar_nokey` = ``.`subquery3_field1`)) SELECT table1.pk AS field1 FROM ( SELECT subquery1_t1. * FROM t2 AS subquery1_t1 diff --git a/sql/sql_planner.cc b/sql/sql_planner.cc index 3979d696d5d..32dc6bd1fe1 100644 --- a/sql/sql_planner.cc +++ b/sql/sql_planner.cc @@ -4320,12 +4320,24 @@ void Optimize_table_order::advance_sj_state( The simple way to model this is to remove SJM-SCAN(...) fanout once we reach the point #2. */ - pos->sjm_scan_need_tables= - emb_sj_nest->sj_inner_tables | - emb_sj_nest->nested_join->sj_depends_on; - pos->sjm_scan_last_inner= idx; - Opt_trace_object(trace).add_alnum("strategy", "MaterializeScan"). - add_alnum("choice", "deferred"); + if (pos->sjm_scan_need_tables && + emb_sj_nest != NULL && + emb_sj_nest != + join->positions[pos->sjm_scan_last_inner].table->emb_sj_nest) + /* + Prevent that inner tables of different semijoin nests are + interleaved for MatScan. + */ + pos->sjm_scan_need_tables= 0; + else + { + pos->sjm_scan_need_tables= + emb_sj_nest->sj_inner_tables | + emb_sj_nest->nested_join->sj_depends_on; + pos->sjm_scan_last_inner= idx; + Opt_trace_object(trace).add_alnum("strategy", "MaterializeScan"). + add_alnum("choice", "deferred"); + } } else if (sjm_strategy == SJ_OPT_MATERIALIZE_LOOKUP) {