从大型ID

时间:2015-06-25 16:34:57

标签: sql sql-server tsql query-optimization

我有一个很大的ID列表,我需要检查一个表,以查看我的表中没有的onec。

我写了这个查询:

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')
     )

问题是查询因超大数字(20' 000)和/或表格大小(+2百万行)而超时。 我可以用更好,不那么苛刻的方式做到这一点吗?

1 个答案:

答案 0 :(得分:0)

except关键字是您的朋友。

 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)