-1

I have a large list of IDs that I need to check against a table to see wich onec that are not in my table.

I wrote this query:

SELECT id 
FROM ( VALUES (1,2,3,4...) AS Checking (id)
       WHERE id NOT IN (SELECT UniqueActivityID 
                          FROM UserActivity
                         WHERE CONVERT(DATE,[Date]) > CONVERT(DATE,'2015-06-24')
     )

The problem is that the query times out because of the large amount of numbers (20'000) and/or table size (+2 million rows). Can I do this in a better, less damanding way?

paparazzo
  • 44,497
  • 23
  • 105
  • 176
Johan Ketels
  • 167
  • 1
  • 9

1 Answers1

0

The except keyword is your friend.

 where somefield in 
 (select somefield
 from whereever
 where some conditions are met

 except 

 select somefield
 from whereever
 where the same conditions are met
 and more conditions are met that make you want to exclude the record)
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43