Join statement order of operation

前端 未结 1 1338
执念已碎
执念已碎 2020-12-29 04:26

Given the following 3 way join

select t1.* from t1
left join t2 on t1.fk = t2.pk
join t3 on t2.fk = t3.pk

If the join between t2 and t3 fa

相关标签:
1条回答
  • 2020-12-29 04:57

    The placement of the ON clauses controls the logical order of evaluation.

    So first the t1 LEFT JOIN t2 ON t1.fk = t2.pk happens. The result of this join is a virtual table containing all the matching rows from t1, t2 and (because it is a left outer join) any non matched t1 rows are also preserved with null values for the t2 columns.

    This virtual table then participates in the next join. JOIN t3 ON t2.fk = t3.pk

    Any t2 records that do not match rows in t1 are not part of the virtual table output from the first stage so won't appear in the final result. Additionally this inner join on t2.fk = t3.pk will lose any NULL values of t2.fk effectively turning your whole thing back into inner joins.

    Logical Query Processing is explained well by Itzik Ben Gan here

    0 讨论(0)
提交回复
热议问题