I’ve been really enjoying the DBA StackExchange site recently. Not only can you see what challenges and hurdles people have, the site construction gives people a great way to contribute to an ever expanding library of database solutions. Questions range from the very simple to the highly esoteric, but in all cases the community comes together to groom both questions and answers in such a way that a comprehensive knowledge base is built for future use.
One of these questions that recently came up was: Which performs better, creating your joins in the FROM clause or the WHERE clause? Most people have been using the ANSI-92 syntax, so this question may seem a little odd, but I still see a lot of SQL code out there that uses the prior syntax where the joins are declared in the WHERE portion of your query. If you want to read more, Mike Walsh(@Mike_Walsh) has a great post on how the syntax has evolved and how its changed in SQL Server 2012.
Back to the question, though. Does it really make any difference? Well, I could tell you straight out, but what sort of blog post would that make? Instead, let’s test it out ourselves. Using a basic schema, I’ve put together two very basic queries:
SELECT s_id ,s_desc ,b_desc ,f_desc FROM snafu s ,bar b ,foo f WHERE s.b_id = b.b_id AND b.f_id = f.f_id; SELECT s_id ,s_desc ,b_desc ,f_desc FROM snafu s INNER JOIN bar b ON (s.b_id = b.b_id) INNER JOIN foo f ON (b.f_id = f.f_id);
As you can see, the only real difference here is that in the first query we have our joins in the WHERE clause. The second follows ANSI-92 syntax and places the joins in the FROM clause. Now how do we tell if they perform differently? Query plans, of course!
Query 1 (WHERE clause)
Query 2 (FROM clause)
Notice how both queries have exactly the same plan. This is because our friend, the Optimizer, understands the two approaches and will build the plan accordingly. Want to play with it yourself? You can check out the full example over at SQL Fiddle.
There are three things I’d like you to take with you after this brief exercise:
- Functionally, it doesn’t matter if you declare your JOINs in your FROM or your WHERE clause, the optimizer will treat both as the same. However, if you read Mike Walsh’s blog post, you really should be using the ANSI-92 standard. The “old” syntax only works if you have your database in SQL 2000 compatibility mode (which means it doesn’t work at all in SQL 2012).
- Query plans will answer most of your performance questions regarding SQL syntax. If you haven’t been looking at them, I strongly suggest you pick up Grant’s book and start checking those plans out.
- I’ve only recently discovered SQL Fiddle, but this is a great tool for mocking up and testing concepts for databases. I haven’t built anything larger than 2-3 tables, but for basic test cases and examples to demonstrate something, it’s really cool(it even lets you look at query plans!). Check it out.