Tuesday, 6 August 2013

SQL Optimize query to get max value

SQL Optimize query to get max value

I have the following query. The part where I do the
select for max(Entered_On).....
takes the longest. If I comment out the where cause, it run fast.
Select dlp.ParamID, dp.ParamName
from data_LocP dlp
inner join data_In di on dlp.LocID = di.LocID
inner join data_Parms dp on dp.ParamID = di.ParamID
inner join map_Loc ml on ml.LocId = dlp.LocId
where di.Entered_On = (select max(Entered_On ) from data_In where
LocId = dlp.LocID
and ParamId = dlp.ParamID)
I was wondering if there is a way to optimize this query as the part where
I get the Max(Entered_On) takes the longest time. As a FYI, I need the
Entered_On to be the max date for a given LocId and ParamId.

No comments:

Post a Comment