Inner join vs Where

后端 未结 19 1045
庸人自扰
庸人自扰 2020-11-22 05:20

Is there a difference in performance (in oracle) between

Select * from Table1 T1 
Inner Join Table2 T2 On T1.ID = T2.ID

And



        
相关标签:
19条回答
  • 2020-11-22 05:59

    The performance should be identical, but I would suggest using the join-version due to improved clarity when it comes to outer joins.

    Also unintentional cartesian products can be avoided using the join-version.

    A third effect is an easier to read SQL with a simpler WHERE-condition.

    0 讨论(0)
  • 2020-11-22 06:00

    I don't know about Oracle but I know that the old syntax is being deprecated in SQL Server and will disappear eventually. Before I used that old syntax in a new query I would check what Oracle plans to do with it.

    I prefer the newer syntax rather than the mixing of the join criteria with other needed where conditions. In the newer syntax it is much clearer what creates the join and what other conditions are being applied. Not really a big problem in a short query like this, but it gets much more confusing when you have a more complex query. Since people learn on the basic queries, I would tend to prefer people learn to use the join syntax before they need it in a complex query.

    And again I don't know Oracle specifically, but I know the SQL Server version of the old style left join is flawed even in SQL Server 2000 and gives inconsistent results (sometimes a left join sometimes a cross join), so it should never be used. Hopefully Oracle doesn't suffer the same issue, but certainly left and right joins can be mcuh harder to properly express in the old syntax.

    Plus it has been my experience (and of course this is strictly a personal opinion, you may have differnt experience) that developers who use the ANSII standard joins tend to have a better understanding of what a join is and what it means in terms of getting data out of the database. I belive that is becasue most of the people with good database understanding tend to write more complex queries and those seem to me to be far easier to maintain using the ANSII Standard than the old style.

    0 讨论(0)
  • 2020-11-22 06:00

    Although the identity of two queries seems obvious sometimes some strange things happens. I have come accros the query wich has different execution plans when moving join predicate from JOIN to WHERE in Oracle 10g (for WHERE plan is better), but I can't reproduce this issue in simplified tables and data. I think it depends on my data and statistics. Optimizer is quite complex module and sometimes it behaves magically.

    Thats why we can't answer to this question in general because it depends on DB internals. But we should know that answer has to be 'no differences'.

    0 讨论(0)
  • 2020-11-22 06:01

    Don’t forget that in Oracle, provided the join key attributes are named the same in both tables, you can also write this as:

    select *
    from Table1 inner join Table2 using (ID);
    

    This also has the same query plan, of course.

    0 讨论(0)
  • 2020-11-22 06:01

    It is true that, functionally, both queries should be processed the same way. However, experience has shown that if you are selecting from views that use the new join syntax, it is important to structure your queries using it as well. Oracle's optimizer can get confused if a view uses a "join" statement, but a query accessing the view uses the traditional method of joining in the "where" clause.

    0 讨论(0)
  • 2020-11-22 06:03

    As kiewik said, the execution plan is the same.

    The JOIN statement is only more easy to read, making it easier not to forget the ON condition and getting a cartesian product. These errors can be quite hard to detect in long queries using multiple joins of type : SELECT * FROM t1, t2 WHERE t1.id=t2.some_field.

    If you forget only one join condition, you get a very long to execute query returning too many records... really too many. Some poeple use a DISTINCT to patch the query, but it's still very long to execute.

    That's accurately why, using JOIN statement is surely the best practice : a better maintainability, and a better readability.

    Further more, if I well remember, JOIN is optimized concerning memory usage.

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