As you may or may not know, it is possible to use LEFT JOIN (and thus RIGHT JOIN) to filter out tables, just as it is with the NOT IN.
However, how do they perform against each other?
Well, using similar data as created in the “In versus INNER JOIN” example, I made two simple queries to see:

SELECT *

FROM TestOne

WHERE ID1 NOT IN

(

SELECT ID1 FROM TestTwo

)

GO

SELECT T1.*

FROM TestOne T1

LEFT JOIN TestTwo T2 ON T1.ID1 = T2.ID1

WHERE T2.ID1 IS NULL

GO

This gave the following execution plan:
(click to enlarge)
Estimated Execution Plan For NOT IN versus LEFT JOIN for first and second query
As can be seen the margin of difference – 46% versus 54% – is much smaller then with the IN syntax as linked above.
So I tried more complex queries where I just took the ones I made for the In versus INNER JOIN article, and modified them to be NOT IN and LEFT JOINS. Like this:

SELECT T1.*

FROM TestOne T1

WHERE T1.ID1 NOT IN (

SELECT ID1 FROM TestTwo T2

WHERE ID2 NOT IN (

SELECT ID2 FROM TestFour T4

WHERE ID4 NOT IN (

SELECT ID4 FROM TestFive T5

)

)

)

GO

SELECT T1.*

FROM TestOne T1

LEFT JOIN TestTwo T2 ON T1.ID1 = T2.ID1

LEFT JOIN TestFour T4 ON T2.ID2 = T4.ID2

LEFT JOIN TestFive T5 ON T4.ID4 = T5.ID4

WHERE T2.ID1 IS NULL

AND T4.ID2 IS NULL

AND T5.ID4 IS NULL

GO

This gave a queryplan where the LEFT JOIN actually performened better, not by much but still 52 versus 48%. (I’ve minimized the query plans because I was only interested in the numbers, and not in the execution plan itself.)
(click to enlarge)
Estimated Execution Plan For NOT IN versus LEFT JOIN for third and fourth query
So where the case was more in favore of using IN instead of INNER JOINs when filtering inclusive, then exclusive looks to be more evenly matched when it comes to NOT IN versus the LEFT JOIN syntax.
But as always, use a case by case decision, by looking at your own querys details. This is just an example and guideline numbers.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>