CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo (.com)

Blog moved to www.jeffreypalermo.com

Oracle-style joins in Sql server. There is no performance difference - level 300

I recently came across some sql code that caught me off guard.  Here is is:

select something
from table1, table2
where table1.id = table2.id

I immediately thought that a Cartesian product was happening and the rows were being filtered afterward.  All my database experience has been with Microsoft databases, so I didn't know that this syntax used to be the way most people did sql..  Like any good engineer, I set out to find out for myself what was really going on.

I used the Northwind database to compare the following two queries:  the first with Sql server syntax, and the second with "old school" syntax (which Sql server 2000 suppports).
SELECT    *
FROM    Orders o
    INNER JOIN Customers c ON o.CustomerID = c.CustomerID
    INNER JOIN [Order Details] od ON o.OrderID = od.OrderID
    INNER JOIN Products p ON od.ProductID = p.ProductID

SELECT    *
FROM Customers c, Orders o, [Order Details] od, Products p
WHERE o.CustomerID = c.CustomerID
    AND o.OrderID = od.OrderID
    AND od.ProductID = p.ProductID

I ran these two queries many, many times together and in isolation, and I examined the execution plans, the client statistics as well as the Sql trace.  It appears that at a lower level, these two operations are identical.  Both queries took the same Duration, CPU cycles, and Reads to execute.  Here is the Execution plan.  Both queries have this same exact exectuion plan:




Personally, I like the INNER JOIN syntax.  It's very explicit, and it's easy to add RIGHT and LEFT to dictate OUTER joins.  A plus is that it is the ANSI standard and Microsoft's recommendation for Sql Server. 

The objective conclusion of this experiment is that the style picked for a query will not affect the speed at which that query runs.  The differences are subjective.  My advice, however, is that a single style be adopted as part of the team's coding standard. 


Comments

Jeremy Wiebe said:

AFAIK Oracle supports the SQL-92 syntax in Oracle 9 and newer.

Also, the ANSI SQL-92 join syntax is "more correct". In some instances the (especially when using outer joins with filter clauses) the DBMS may return incorrect results.

See Listing 5 on this page. http://www.microsoft.com/technet/prodtechnol/sql/70/deploy/migrat.mspx
# March 2, 2006 10:11 AM

Paul Wilson said:

The syntax that you are referring to as "Oracle" style was actually the closest thing to a standard syntax before there was an ANSI standard, and as such it has always worked in MS products also. I used it many years with Sql Server and Access, as well as Sybase, since it was the only syntax I knew and it was what was in all the books back then too. So I guess it would be more correct to call it the "Old School" syntax. :)

On the other hand, there never was anything close to an outer join standard before the ANSI standard, so while Oracle used the cryptic += and =+ syntax, the simple English "left join" and "right join" syntax that was used by Sybase (and later Microsoft) was definitely better. And when that simple syntax became the ANSI standard, it only made sense to also standardize on the similar simple "inner join" syntax.
# March 2, 2006 11:42 AM

Michael Ames said:

Agreed with the previous commenters that what you're referring to as Oracle-style is really just pre-JOIN style, and it's surprising that you haven't seen it. My experience with both databases has been that the older style is more common.

What is most *definitely* Oracle style is using += and =+ to specify left and right outer joins in the WHERE clause. But even SQL Server once supported something similar, with *= and =*.
# March 2, 2006 12:39 PM

Jeffrey Palermo said:

Great comments, all. I editted the post to say "old school" instead of Oracle. I've seen this syntax before, but it was always in bad queries where a cross join actually _was_ happening, so I was alarmed when I saw it in production code until I saw the WHERE clause that actually did the key joins.



# March 2, 2006 5:08 PM

Eber Irigoyen said:

I'm not sure if that has changed in SQL 2005 though, your test was on SQL 2000 right?

I think the syntax is still valid, but there are some restrictions
# March 3, 2006 7:57 PM

Stan the Man said:

You are correct that both styles of joins run the same way. But I believe that the "old school" of joins has been deprecated on SQL Server 2005. The only way they are supported is by telling it to be SQL Server 2000 (or older) compatible. Microsoft has been stating for some time that the "old school" of joins will not be supported in future versions of SQL Server. Well, it's here! Everyone should definitely be using the ANSI style joins. Sorry "old school" guys!
# March 13, 2006 4:13 PM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication

News

Headspring Systems

View Jeffrey Palermo's profile on LinkedIn

See my new blog at .jeffreypalermo.com