Explicit JOINs vs Implicit joins?

后端 未结 3 2029
别跟我提以往
别跟我提以往 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…

    0 讨论(0)
  • 2020-12-19 09:31

    An average sql query you will encounter in real business has 7-8 joins with 12-16 join conditions. One every 10 or 20 queries may involve nested joins or other more advanced cases. Explicit join syntax is simply far easier to maintain, debug and develop. And those factors are critical for business software - the faster and safer the better. Implicit join are somewhat easier to code if you create statements dynamically through application code. Perhaps there are other uses that i am unaware.

    0 讨论(0)
  • 2020-12-19 09:40

    Your professor should speak with Gordon Linoff, who is a computer science professor at Columbia University. Gordon, and most SQL enthusiasts on this site, will almost always tell you to use explicit join syntax. The reasons for this are many, including (but not limited to):

    • Explicit joins make it easy to see what the actual join logic is. Implicit joins, on the other hand, obfuscate the join logic, by spreading it out across both the FROM and WHERE clauses.
    • The ANSI 92 standard recommends using modern explicit joins, and in fact deprecated the implicit join which your professor seems to be pushing

    Regarding performance, as far as I know, both versions of the query you wrote would be optimized to the same thing under the hood. You can always check the execution plans of both, but I doubt you would see a significant difference very often.

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