Hi, I am using below query to get the minimum date to aggregate within a subquery. The problem I am having is that runs very slow, almost 10 times slower than without aggregation. I thought aggregating improves the performance, not slows down. Can anyone suggest why this happening please. Not sure if anything to do with the temp tables which have no unique keys.
SELECT t1.date, t1.cycleNo, t1.Value1, t1.Value2 FROM @tmpTable As t1 INNER JOIN @tmpTable2 As t2 ON t1.date = t2.date AND t1.cycleNo = t2.cycleNo INNER JOIN (SELECT MIN(t1.date) As firstDate, t2.cycleNo FROM @tmpTable1 As t1 INNER JOIN @tmpTable2 As t2 ON t1.date = t2.date AND t1.cycleNo = t2.cycleNo GROUP BY t2.cycleNo) As FirstDate ON t1.date = FirstDate.firstDate AND t1.cycleNo = FirstDate.cycleNo
Thank you