it is collecting over 10,000 rows before the final trim
fetches 30,000 rows for one machine for the section
select line, code, eventTable.t_stamp as t_stamp
from eventTable
WHERE t_stamp >= coalesce((
SELECT TOP 1 t_stamp FROM eventTable
WHERE eventTable.t_stamp < DATEADD(hh,7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-60),0)) and code <260 and Line= 'B2'
ORDER BY t_stamp DESC), '2021-01-01') AND
t_stamp <= coalesce((
SELECT TOP 1 t_stamp FROM eventTable
WHERE eventTable.t_stamp > DATEADD(hh,7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) and code <260 and Line='B2'
ORDER BY eventTable.t_stamp), dateadd(hour,5, DATEADD(hh,7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) ))
500ms here for 30k+ rows
60ms the other subquery gets 200 rows
trims it all down to 38 rows, organized by day over 60 days
would have all 60days show, but they donāt run all days
I thought 1.2 seconds was doing really well for this
Though I also think I will set it as a query tag that gets driven once a day, when I figure out how in the next few hours.
Is there a way to make the subqueries much faster?