Tuesday, March 4, 2008

Avoid Using NOT IN Clause. Instead use NOT EXISTS

Hi
It is just my experience and preference to use NOT EXISTS clause instead of NOT IN clause in SQL query. There is dramatic change in preformance when NOT EXISTS is used as compared to NOT IN.

I am running a query where i have to select all the records which are not present in other table. The execution plan shown for both queries explain the result itself. Due to confedentiality, i am only writing query cost of each run instead of displaying estimated execution plan.

NOT IN Query Cost - 88%
NOT EXISTS Query Cost - 12%

There is huge difference in there.

No comments: