I feel a little silly asking this, because its probably realy simple.
Select DISTINCT ID from tbl1
where col2='abc'
...
and ID not in (select distinct tbl2.ID from tbl2 where tbl2.Dtm > '2010-01-01')
This is part of my upsert in TSQL so Id need to run the tbl2 where clause again (I used to use merge but its not available in this DB). The problem here is tbl2 has millions of rows and tbl1 has tens of thousands. Obviously running like this as tb1xtbl2 itd be extremely inefficient. Ive thought of using cursors and temp tables but I dont see that running much better ?
Anyone got other ideas?
There are several different ways to define this type of query.
Please create queries with Left Join, Not In and Not Exists and check the execution plan, Only then you will know, which one is better suited for your case.
Left Outer Join Way
Reference:
Similar Discussion Thread