What is difference between ANSI and non-ANSI joins, and which do you recommend? [duplicate]

不羁的心 提交于 2020-02-02 07:01:45

问题


I have come across many websites to find the answer about which one is better, ANSI or non- ANSI syntax. What is the difference between these two queries?

select a.name,a.empno,b.loc
from tab a, tab b
where a.deptno=b.deptno(+);

and:

select a.name,a.empno,b.loc
from tab a 
left outer join tab b on a.deptno=b.deptno;

The result is same in both the cases. The second query is also longer. Which one is better?

suppose if we have added another table Salgrade in the above query based on what conditions we need to join them?? .. can any one assume one table and give me explanation


回答1:


both syntaxes usually work without problems, but if you try to add a where condition you will see that with the second one is much simpler to understand which is the join condition and which is the where clause.

1)

  SELECT a.name,
         a.empno,
         b.loc 
    FROM tab a,
         tab b 
   WHERE a.deptno = b.deptno(+)
     AND a.empno = 190;

2)

         SELECT a.name,
                a.empno,
                b.loc 
           FROM tab a,
LEFT OUTER JOIN tab b 
             ON a.deptno = b.deptno
          WHERE a.empno = 190;

Also, it's much easier to recognize an outer join and do not forget to include the (+). Overall you can say it's just a question of taste, but the truth is that the second syntax is much more readable and less prone to errors.




回答2:


The first is a legacy Oracle specific way of writing joins, the second is ANSI SQL-92+ standard and is the preferred one.




回答3:


Extensively discussed many a times, including one by me.

Use explicit JOINs rather than implicit (regardless whether they are outer joins or not) is that it's much easier to accidently create a cartesian product with the implicit joins.

With explicit JOINs you cannot "by accident" create one. The more tables are involved the higher the risk is that you miss one join condition.

Basically (+) is severely limited compared to ANSI joins. Furthermore it is only available in Oracle whereas the ANSI join syntax is supported by all major DBMS

SQL will not start to perform better after migration to ANSI syntax - it's just different syntax.

Oracle strongly recommends that you use the more flexible FROM clause join syntax shown in the former example. In the past there were some bugs with ANSI syntax but if you go with latest 11.2 or 12.1 that should be fixed already.

Using the JOIN operators ensure your SQL code is ANSI compliant, and thus would allow a front-end application to be more easily ported for other database platforms.

Join conditions have a very low selectivity on each table and a high selectivity on the tuples in the theoretical cross product. Conditions in the where statement usually have a much higher selectivity.

Oracle internally converts ANSI syntax to the (+) syntax, you can see this happening in the execution plan's Predicate Information section.

If you are using 11.2 I advise ANSI join. If you use 12C, there are some new bugs unearthed on OUTER JOINS.

I also remember some bugs in Oracle while using ANSI syntax, before 11.2 where it got fixed in 11.2.

In my opinion, I am not a big fan of ANSI syntax, though Oracle does confirm to the standards of ANSI, it is not totally bug free.




回答4:


please, read this article about joins. result of your example is not same, if you have data in B table and not in A table



来源:https://stackoverflow.com/questions/19905028/what-is-difference-between-ansi-and-non-ansi-joins-and-which-do-you-recommend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!