I have 1 table contains about 4 millions entries structure like below:
Now I want to :
[Alarm History]
(
AlarmID int,
SetTime datetime
)
SELECT all the AlarmID that happened during Jan 2008 and no such AlarmID during Dec 2007. I used:
My querry take very slow since there are more than 20,000 entries during Dec 2007 and 15,000 entries during Jan 2008,
SELECT * FROM [Alarm History]
WHERE SetTime BETWEEN '1-jan-2008' AND '31-jan-2008'
AND AlarmID NOT IN
(
SELECT AlarmID FROM [Alarm History]
WHERE SetTime BETWEEN '1-dec-2007' AND '31-DEC-2007'
)
"NOT IN" operation is like an Cartesian Product : Compare each AlarmID during December 2007 with each AlarmID during Jan 2008. The results is very slow performance.
Anybody can help me find an alternatives way to do this ? Thanks a lot.