The answer depends first on which database manager (sgbd) the internal implementations may not be the same for NOT IN and NOT EXISTS.
In the case of SQL Server I recently published the article "Which is faster: NOT IN or NOT EXISTS?" dealing with the issue of "how to know which elements are in set A but are not in set B". In addition to NOT IN and NOT EXISTS they are treated of other options like EXCEPT, LEFT OUTER JOIN and OUTER APPLY.
First, it is always recommended review the implementation plan. And it is necessary to keep in mind that the execution plan generated varies according to several factors, including the characteristics of the data.
In my opinion there is no way to define a priori which method is the fastest.
When analyzing other articles on the subject, it can be seen that in some cases the situation is optimized to obtain a certain conclusion. For example, in the article "Should I use NOT IN, OUTER APPLY, LEFT OUTER JOIN, EXCEPT, or NOT EXISTS?", by Aaron Bertrand, he states early in the article that "Instead of NOT IN, use a Correlated NOT EXISTS for this query Pattern. Always". That is, according to him NOT EXISTS is always faster. However, he quotes "for this query Pattern". What standard of consultation? Direct consultation between only two tables and that have indexes that allow the direct joining between the tables. Well, there is papaya with sugar, right?
In the article "Consider using [NOT] EXISTS Instead of [NOT] IN with a subquery"by Phil Streiff, "It used to be that the EXISTS Logical Operator was Faster than IN, when Comparing data sets using a subquery. (…) However, the query Optimizer now Treats EXISTS and IN the same way, Whenever it can, so you’re unlikely to see any significant performance Differences”.
This generated a debate here at the company yesterday same rs, the DBA commenting on the use of
not in
and ofnot exist
in a script we created– MarceloBoni
@Marcelobonifazio I usually use not in, but I don’t know which two is more efficient.
– Marconi
I use the
not in
also. I can’t answer if you perform better or not.– emanuelsn
I use NOT EXISTS. This reply in Soen explains well what may be different in the use of each of them.
– ramaral
Here is another reason to consider avoiding the use of NOT IN (or IN): http://stackoverflow.com/questions/4722220/sql-in-clause-1000-item-limit
– Marcelo Bezerra bovino
Personal thank you.
– Marconi
that article in the Mysql manual (in English) describes some conditions under which it is possible to optimize queries with Subqueries using
EXISTS
– Pedro Sanção
@Penalty Thank you.
– Marconi
Each case is a case. I suggest reading the article Which is faster: NOT IN or NOT EXISTS?: https://portosql.wordpress.com/2020/05/02/not-in-x-not-exists/
– José Diz