Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will ANSI JOIN vs. non-ANSI JOIN queries perform differently?

I have my business-logic in ~7000 lines of T-SQL stored procedures, and most of them has next JOIN syntax:

SELECT A.A, B.B, C.C FROM aaa AS A, bbb AS B, ccc AS C WHERE     A.B = B.ID AND B.C = C.ID AND C.ID = @param 

Will I get performance growth if I will replace such query with this:

SELECT A.A, B.B, C.C FROM aaa AS A JOIN bbb AS B    ON A.B = B.ID JOIN ccc AS C    ON B.C = C.ID    AND C.ID = @param 

Or they are the same?

like image 714
abatishchev Avatar asked Oct 21 '09 06:10

abatishchev


People also ask

What is the difference between ANSI and non-ANSI?

We can retrieve data from multiple tables using Joins. When we retrieve the data from multiple tables with on keyword condition then this is called as ANSI format joins. When we retrieve data from multiple tables based on where keyword condition then it is called as NON-ANSI format Joins.

Do not mix ANSI and non-ANSI join syntax in the same query?

ANSI syntax allow a clear separation between joins clause and the where clause restrictions. The ANSI notation makes the relations between the tables explicit, and saves you from coding equality tests for join conditions in the WHERE clause.

Which join has better performance?

If you dont include the items of the left joined table, in the select statement, the left join will be faster than the same query with inner join. If you do include the left joined table in the select statement, the inner join with the same query was equal or faster than the left join.

Which is the most efficient join in SQL?

TLDR: The most efficient join is also the simplest join, 'Relational Algebra'. If you wish to find out more on all the methods of joins, read further. Relational algebra is the most common way of writing a query and also the most natural way to do so.


1 Answers

The two queries are the same, except the second is ANSI-92 SQL syntax and the first is the older SQL syntax which didn't incorporate the join clause. They should produce exactly the same internal query plan, although you may like to check.

You should use the ANSI-92 syntax for several of reasons

  • The use of the JOIN clause separates the relationship logic from the filter logic (the WHERE) and is thus cleaner and easier to understand.
  • It doesn't matter with this particular query, but there are a few circumstances where the older outer join syntax (using + ) is ambiguous and the query results are hence implementation dependent - or the query cannot be resolved at all. These do not occur with ANSI-92
  • It's good practice as most developers and dba's will use ANSI-92 nowadays and you should follow the standard. Certainly all modern query tools will generate ANSI-92.
  • As pointed out by @gbn, it does tend to avoid accidental cross joins.

Myself I resisted ANSI-92 for some time as there is a slight conceptual advantage to the old syntax as it's a easier to envisage the SQL as a mass Cartesian join of all tables used followed by a filtering operation - a mental technique that can be useful for grasping what a SQL query is doing. However I decided a few years ago that I needed to move with the times and after a relatively short adjustment period I now strongly prefer it - predominantly because of the first reason given above. The only place that one should depart from the ANSI-92 syntax, or rather not use the option, is with natural joins which are implicitly dangerous.

like image 108
Cruachan Avatar answered Sep 22 '22 18:09

Cruachan