Join is faster?

  • I am using SQL server 2008.

    Is JOIN  always faster than sub query ?

  • The second one: "it depends".

    Overall, you cannot say this or that construct is faster. SQL is a declarative language. This means that you state what result you want, and the optimizer figures out the best way to compute that result. Ideally, you should be able to express you query in the clumsiest of ways, and the optimizer may still find the fast and simple plan. Reality is different, and there many things you can do in a query to make it go slow. But that is more subtle than JOIN vs. subquery.

    When you write your query, focus on expressing what result you want. That may or may not give the best performance, but it helps you or someone else who is looking at the query two months later.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • No.

    In particular, for example, often an EXISTS() check is more efficient that an INNER JOIN when you just to need to verify that a matching row exists in another table.

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • Scott has given an example, and Erland is right. It depends. Test both ways for your situation.

     

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply