Should I use IN or NOT IN clause in a query with a huge amount of records?

Anonymous
2022-12-01T22:03:20.263+00:00

I need to fetch about 10 Million Records and avoid some of them passing a blacklist as parameter, I was thinking to use a NOT IN clause but since this kind of clauses makes the query so expensive which will be the best approach, the blacklist will contain about 20 records?

Developer technologies | Transact-SQL
{count} votes

Accepted answer
  1. LiHongMSFT-4306 31,566 Reputation points
    2022-12-02T02:28:08.663+00:00

    Hi @Garcia, Luis (NonEmp)
    There are different ways that can be used to retrieve such data: NOT IN, NOT EXISTS and LEFT JOIN.
    For me, I prefer NOT EXISTS. Unlike 'EXISTS and IN', 'NOT EXISTS and NOT IN' are not equivalent in all cases. Specifically, when NULLs are involved, they will return different results. To be totally specific, when the subquery returns even one null, NOT IN will not match any rows.
    Please refer to this similar thread: NOT IN vs NOT EXISTS

    Regarding performance, you could refer to this article for more details: T-SQL commands performance comparison – NOT IN vs SQL NOT EXISTS vs SQL LEFT JOIN vs SQL EXCEPT

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 121.5K Reputation points MVP Volunteer Moderator
    2022-12-01T22:29:11.763+00:00

    Your query is not very clear, if your query is

       SELECT ...  
       FROM  tbl  
       WHERE somecol NOT IN (SELECT item FROM Blacklist)  
    

    That is about as good as it gets. It does help, though, if the Blacklist table is indexed with item as the primary key.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.