SQL Server - Select most recent record from each group when performance is critical
If you have a small-enough number of (StationID, ParameterID) pairs, then try a query like this:
select StationID, ParameterID, m.DateTime LastDate
from StationParameter sp
cross apply
(
select top 1 DateTime
from MyTable
where StationID = sp.StationID
and ParameterID = sp.ParameterID
order by DateTime desc
) m
To enable SQL Server to perform a lookup, seeking the latest DateTime
for each (StationID,ParameterID) pair.
With only a Clustered Index on (StationID, ParameterID, DateTime), there's no way for SQL Server to discover the distinct (StationID, ParameterID) pairs without scanning the leaf level of the index, and it can find the largest DateTime while it's scanning.
Also at 100M+ rows, this table might be better as a Clustered Columnstore instead of a BTree Clustered Index.