I have a table with a column dt
that I use in a WHERE
clause, and another column identifier
that is used in a join. Both dt
and identifier
are indexed.
For all dates before today, the query below runs in less than a second:
select SRDtoday.*
from SRD as SRDtoday
left join (select * from SRD where convert(date, dt) = convert(date, dateadd(day, -1, getdate()))) as SRDyesterday ON (SRDtoday.Identifier = SRDyesterday.Identifier)
where convert(date, SRDtoday.dt) = convert(date, dateadd(day, -1, getdate()))
However, when I run it for today it takes around 15 minutes:
select SRDtoday.*
from SRD as SRDtoday
left join (select * from SRD where convert(date, dt) = convert(date, getdate())) as SRDyesterday ON (SRDtoday.Identifier = SRDyesterday.Identifier)
where convert(date, SRDtoday.dt) = convert(date, getdate())
When I look at the execution plan it looks like before today it is doing an index seek but when I use today in the query it is doing an index scan.
I've tried dropping and rebuilding the index but that doesn't help. Any ideas?