Explicit JOINs vs Implicit joins?

后端 未结 3 2028
别跟我提以往
别跟我提以往 2020-12-19 09:07

My Database Professor told us to use:

SELECT A.a1, B.b1 FROM A, B WHERE A.a2 = B.b2;

Rather than:

SELECT A.a1, B.b1 FROM A          


        
3条回答
  •  时光说笑
    2020-12-19 09:27

    As with many non-trivial things there is no simple yes / no answer.

    The first thing is, for trivial queries (as yours example in the question) it doesn’t matter which syntax you use. The classic syntax is even more compact for simple queries.

    First for non-trivial queries (say more than five joins) you will learn the benefits of the ANSII syntax. The main benefit is that the join predicates are separated and divided from the WHERE condition.

    Simple example – this is a complete valid query in the pre-ANSII syntax

    SELECT A.a1, B.b1 
    FROM A, B 
    WHERE A.a1 = B.b1 and
          A.a1 = B.b1(+);
    

    Is it inner or outer join? Furthermore if this construct is scattered in a predicate with 10 other join condition in the WHERE clause, it is even very easy to misread it.

    Anyway, it would be very naïve to assume that those two syntax options are only a syntax sugar and that the resulting execution plan is for all queries, any data and all Oracle versions identical.

    Yes, and there were times (about Oracle 10) you should be careful. But in times of 12 and 18 versions I do not see a reason to be defensive and I convonced it is safe to use the ANSII syntax from the above reason of better overview and readability.

    Final remark for your professor: if you get in the position of optimizing the WHERE restriction of the Cartesian Product you typically encounters a performance problem. Make a thought experiment with a Cartesian Product of four tables with 1.000 rows each…

提交回复
热议问题