Subject | Re: Group By performance in large tables. |
---|---|
Author | Svein Erling |
Post date | 2010-11-03T16:53:35Z |
> SELECT first 1 TRANSID, PointID FROM INT_VALUESThese are two very different queries, for one of them you select the record with the latest TRANSTIME, for the other the highest TRANSID. An index on TRANSTIME is of course useless for TRANSID.
> WHERE PointID = 1
> AND TransTime < '2009-10-22 15:20:42'
> ORDER BY TransTime Desc
>
> SELECT MAX(TRANSID), PointID FROM INT_VALUES
> WHERE PointID in (1.... etc)
> AND TransTime < '2009-10-22 15:20:42'
> Group By PointID
Is MIN(TRANSID) equally slow? If not, you could try to decrement TRANSID rather than increment it when adding new records (I assume there is an index on TRANSID that is used in the plan, at least for MIN(TRANSID)).
Another option you could try (haven't got a clue how it will work):
WITH MyTime(PointID, TransTime) AS
(SELECT PointID, MAX(TransTime) FROM INT_VALUES
WHERE PointID in (1.... etc)
AND TransTime < '2009-10-22 15:20:42'
GROUP BY 1)
SELECT MAX(IV.TransID), IV.PointID
FROM INT_VALUES IV
JOIN MyTime MT on IV.PointID = MT.PointID
AND IV.TransTime BETWEEN MT.TransTime - 0.00001
AND MT.TransTime + 0.00001
WHERE IV.TransTime < '2009-10-22 15:20:42'
GROUP BY 2
The second GROUP BY will be redundant unless several records may occur within about one second (there are 86400 seconds in one day, hence 0.00001 is slightly less than one second).
HTH,
Set