I have two tables and I'm looking for the rows in one table where a time column is not near any of the values in another table's time column. (Near is defined as within a minute).
Here's a code sample:
create table temp1
(
id int identity primary key,
value datetime not null
)
GO
create index ix_temp1 on temp1(value, id);
GO
set nocount on
insert temp1 (value) values (DATEADD(second, rand() * 1000000, '20100101'))
GO 15000
table temp2 is set up identical:
create table temp2
(
id int identity primary key,
value datetime not null
)
GO
create index ix_temp2 on temp2(value, id);
GO
set nocount on
insert temp2 (value) values (DATEADD(second, rand() * 1000000, '20100101'))
GO 15000
And here's my first crack at it (which is very inefficient)
SELECT t1.id, t1.value
FROM temp1 t1
LEFT JOIN temp2 t2
ON t1.value between DATEADD(MINUTE, -1, t2.value) and DATEADD(MINUTE, 1, t2.value)
WHERE t2.value is null
I'm looking for ways to do this more efficiently. All solutions will be considered (new indexes, SSIS solution, CLR solutions, temp tables, cursors etc...)