views:

117

answers:

4

Hi all,

I wish to know if I have a join query something like this -

Select E.Id,E.Name from Employee E join Dept D on E.DeptId=D.Id

and a subquery something like this -

Select E.Id,E.Name from Employee Where DeptId in (Select Id from Dept)

When I consider performance which of the two queries would be faster and why ?

Also is there a time when I should prefer one over the other ?

Sorry if this is too trivial and asked before but I am confused about it. Also, it would be great if you guys can suggest me tools i should use to measure performance of two queries. Thanks a lot!

+2  A: 

The performance should be the same; it's much more important to have the correct indexes and clustering applied on your tables (there exist some good resources on that topic).

(Edited to reflect the updated question)

Lucero
I updated my question to change the queries little...thanks for your reply.
Misnomer
+5  A: 

I would EXPECT the first query to be quicker, mainly because you have an equivalence and an explicit JOIN. In my experience IN is a very slow operator, since SQL normally evaluates it as a series of WHERE clauses separated by "OR" (WHERE x=Y OR x=Z OR...).

As with ALL THINGS SQL though, your mileage may vary. The speed will depend a lot on indexes (do you have indexes on both ID columns? That will help a lot...) among other things.

The only REAL way to tell with 100% certainty which is faster is to turn on performance tracking (IO Statistics is especially useful) and run them both. Make sure to clear your cache between runs!

JNK
A: 

You can use an Explain Plan to get an objective answer.

For your problem, an Exists filter would probably perform the fastest.

Snekse
"an Exists filter would probably perform the fastest" - probably not, I think, although a definitive answer would require testing against the actual data. Exists filters are likely to be faster where there are multiple rows with the same lookup values - so an exists filter might run faster if the query was checking whether other employees had been recorded from the same department, but probably not when looking up against a department table.
Mark Bannister
Would it run slower in that last scenario?
Snekse
It would depend on the optimiser - under certain circumstances, it might, but normally I would expect very similar performance.
Mark Bannister
+3  A: 

Start to look at the execution plans to see the differences in how the SQl Server will interpret them. You can also use Profiler to actually run the queries multiple times and get the differnce.

I would not expect these to be so horribly different, where you can get get real, large performance gains in using joins instead of subqueries is when you use correlated subqueries.

EXISTS is often better than either of these two and when you are talking left joins where you want to all records not in the left join table, then NOT EXISTS is often a much better choice.

HLGEM
+1 for `EXISTS`...short circuiting is your friend!
JNK