No equality substitution for references out of the subqueries. == Test dataset == create table t10 (a int, b int, c int); insert into t10 select seq, seq, seq from seq_1_to_10; create table t11 like t10; insert into t11 select seq, seq, seq from seq_1_to_20; create table t20 as select * from t10; create table t21 as select * from t11; == Example 1 == - "t11.a > (SELECT ... )" refers to t11 - The subquery refers to t10. Note that "t11.a > (SELECT...)" became "t10.a > (SELECT...)" which allowed to attach the subquery to table t10. explain format=json select * from t10, t11 where t10.a=t11.a and t11.a > (select max(t21.a) from t20, t21 where t20.b=t21.b and t20.c >=t10.a)\G EXPLAIN: { "query_block": { "select_id": 1, "cost": 0.04578146, "nested_loop": [ { "table": { "table_name": "t10", "access_type": "ALL", "loops": 1, "rows": 10, "cost": 0.0124848, "filtered": 100, "attached_condition": "t10.a > (subquery#2)" } }, { "block-nl-join": { "table": { "table_name": "t11", "access_type": "ALL", "loops": 10, "rows": 20, "cost": 0.03329666, "filtered": 100 }, "buffer_type": "flat", "buffer_size": "173", "join_type": "BNL", "attached_condition": "t11.a = t10.a" } } ], "subqueries": [ { "expression_cache": { "state": "uninitialized", "query_block": { "select_id": 2, "cost": 0.04578146, "nested_loop": [ { "table": { "table_name": "t20", "access_type": "ALL", "loops": 1, "rows": 10, "cost": 0.0124848, "filtered": 100, "attached_condition": "t20.c >= t10.a" } }, { "block-nl-join": { "table": { "table_name": "t21", "access_type": "ALL", "loops": 10, "rows": 20, "cost": 0.03329666, "filtered": 100 }, "buffer_type": "flat", "buffer_size": "119", "join_type": "BNL", "attached_condition": "t21.b = t20.b" } } ] } } } ] } } 1 row in set (0,001 sec) == Example 2 == - "t11.a > (SELECT ... )" refers to t11 - The subquery refers to t11. In EXPLAIN, note that - "t11.a > (SELECT...)" became "t10.a > (SELECT...)" - But the subquery still has t21.c >= t11.a. It did NOT became "t21.c >= t10.a"... explain format=json select * from t10, t11 where t10.a=t11.a and t11.a > (select max(t21.a) from t20, t21 where t20.b=t21.b and t21.c >=t11.a)\G EXPLAIN: { "query_block": { "select_id": 1, "cost": 0.04578146, "nested_loop": [ { "table": { "table_name": "t10", "access_type": "ALL", "loops": 1, "rows": 10, "cost": 0.0124848, "filtered": 100 } }, { "block-nl-join": { "table": { "table_name": "t11", "access_type": "ALL", "loops": 10, "rows": 20, "cost": 0.03329666, "filtered": 100 }, "buffer_type": "flat", "buffer_size": "173", "join_type": "BNL", "attached_condition": "t11.a = t10.a and t10.a > (subquery#2)" } } ], "subqueries": [ { "expression_cache": { "state": "uninitialized", "query_block": { "select_id": 2, "cost": 0.04578146, "nested_loop": [ { "table": { "table_name": "t20", "access_type": "ALL", "loops": 1, "rows": 10, "cost": 0.0124848, "filtered": 100 } }, { "block-nl-join": { "table": { "table_name": "t21", "access_type": "ALL", "loops": 10, "rows": 20, "cost": 0.03329666, "filtered": 100 }, "buffer_type": "flat", "buffer_size": "65", "join_type": "BNL", "attached_condition": "t21.b = t20.b and t21.c >= t11.a" } } ] } } } ] } } 1 row in set (0,001 sec)