<p>Consider the following 2 queries:</p>

<pre><code>select tblA.a,tblA.b,tblA.c,tblA.d
from tblA
where tblA.a not in (select tblB.a from tblB)

select tblA.a,tblA.b,tblA.c,tblA.d
from tblA left outer join tblB
on tblA.a = tblB.a where tblB.a is null
</code></pre>

<p>Which will perform better?  My assumption is that in general the join will be better except in cases where the subselect returns a very small result set.</p>

[{'answer_id': 47439, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': '<p>From my observations, MSSQL server produces same query plan for these queries.</p>\n'}, {'answer_id': 47448, 'author': 'Mike Polen', 'author_id': 212, 'author_profile': 'https://Stackoverflow.com/users/212', 'pm_score': 0, 'selected': False, 'text': '<p>I created a simple query similar to the ones in the question on MSSQL2005 and the explain plans were different. The first query appears to be faster. I am not a SQL expert but the estimated explain plan had 37% for query 1 and 63% for the query 2. It appears that the biggest cost for query 2 is the join. Both queries had two table scans.</p>\n'}, {'answer_id': 47449, 'author': 'Tom', 'author_id': 3715, 'author_profile': 'https://Stackoverflow.com/users/3715', 'pm_score': 5, 'selected': True, 'text': '<p>RDBMSs "rewrite" queries to optimize them, so it depends on system you\'re using, and I would guess they end up giving the same performance on most "good" databases.</p>\n\n<p>I suggest picking the one that is clearer and easier to maintain, for my money, that\'s the first one.  It\'s much easier to debug the subquery as it can be run independently to check for sanity.</p>\n'}, {'answer_id': 48255, 'author': 'andy47', 'author_id': 2661, 'author_profile': 'https://Stackoverflow.com/users/2661', 'pm_score': 2, 'selected': False, 'text': "<p>I second Tom's answer that you should pick the one that is easier to understand and maintain.</p>\n\n<p>The query plan of any query in any database cannot be predicted because you haven't given us indexes or data distributions. The only way to predict which is faster is to run them against <em>your</em> database.</p>\n\n<p>As a rule of thumb I tend to use sub-selects when I do not need to include any columns from tblB in my select clause. I would definitely go for a sub-select when I want to use the 'in' predicate (and usually for the 'not in' that you included in the question), for the simple reason that these are easier to understand when you or someone else has come back and change them. </p>\n"}, {'answer_id': 57744, 'author': 'Martynnw', 'author_id': 5466, 'author_profile': 'https://Stackoverflow.com/users/5466', 'pm_score': 1, 'selected': False, 'text': '<p>The first query will be faster in SQL Server which I think is slighty counter intuitive - Sub queries <em>seem</em> like they should be slower. In some cases (as data volumes increase) an <code>exists</code> may be faster than an <code>in</code>.</p>\n'}, {'answer_id': 64443, 'author': 'Andy Irving', 'author_id': 8553, 'author_profile': 'https://Stackoverflow.com/users/8553', 'pm_score': 2, 'selected': False, 'text': "<p>non-correlated sub queries are fine. you should go with what describes the data you're wanting. as has been noted, this likely gets rewritten into the same plan, but isn't guaranteed to! what's more, if table A and B are not 1:1 you will get duplicate tuples from the join query (as the IN clause performs an implicit DISTINCT sort), so it's always best to code what you want and actually think about the outcome.</p>\n"}, {'answer_id': 64982, 'author': 'Amy B', 'author_id': 8155, 'author_profile': 'https://Stackoverflow.com/users/8155', 'pm_score': 1, 'selected': False, 'text': '<p>It should be noted that these queries will produce different results if TblB.a is not unique.</p>\n'}, {'answer_id': 70363, 'author': 'Piotr Anders', 'author_id': 11377, 'author_profile': 'https://Stackoverflow.com/users/11377', 'pm_score': 2, 'selected': False, 'text': "<p>Well, it depends on the datasets. From my experience, if You have small dataset then go for a NOT IN if it's large go for a LEFT JOIN. The NOT IN clause seems to be very slow on large datasets.</p>\n\n<p>One other thing I might add is that the explain plans might be misleading. I've seen several queries where explain was sky high and the query run under 1s. On the other hand I've seen queries with excellent explain plan and they could run for hours.</p>\n\n<p>So all in all do test on your data and see for yourself.</p>\n"}]
['https://Stackoverflow.com/questions/47433', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/292/']