An Explain Plan
would have shown you why exactly you should use Exists
. Usually the question comes Exists vs Count(*)
. Exists
is faster. Why?
-
With regard to challenges present by NULL: when subquery returns
Null
, for IN the entire query becomesNull
. So you need to handle that as well. But usingExist
, it’s merely afalse
. Much easier to cope. SimplyIN
can’t compare anything withNull
butExists
can. -
e.g.
Exists (Select * from yourtable where bla="blabla");
you get true/false the moment one hit is found/matched. -
In this case
IN
sort of takes the position of theCount(*)
to select ALL matching rows based on theWHERE
because it’s comparing all values.
But don’t forget this either:
EXISTS
executes at high speed againstIN
: when the subquery results is very large.IN
gets ahead ofEXISTS
: when the subquery results is very small.
Reference to for more details: