IN vs EXISTS
The book seems to imply that EXISTS is faster than a join (p. 556, because SQL can stop when it gets a hit) and a join is faster than IN (p. 544). In other words, from reading the book, I would think that the following queries on Pubs are arranged from fastest to slowest:
select stor_id,stor_name from stores s where
exists (select stor_id from discounts where stor_id = s.stor_id)
select s.stor_id,stor_name from stores s join discounts d on s.stor_id = d.stor_id
select stor_id,stor_name from stores where stor_id in (select stor_id from discounts)
This is not what I would expect, because IN and EXIST seem to be doing the same thing in this case. If one is faster because SQL can stop when it gets a hit, then the other should be too.
The query plans (dangerous on a small DB like Pubs) tell me that the join is fastest and the other two take the same time and have the same query plan.
Am I missing something? Is there something special about EXISTS?
|