Test Menu

  • Commentary
  • Deals
  • Coupons

May 4, 2011

SQL - Keep Functions Out of the Where Clause

Taking functions out of the where clause improved stored procedure performance by 2300%.  In my original stored procedure with two functions in the where clause it used to take 46 seconds to complete, but by replacing the functions with the raw data value that they return, the time was reduced to just 2 seconds

Slow:


SELECT MAX(Temperature) 
FROM #tblStationStatistics 
WHERE StationID = A.StationID
    AND Temperature BETWEEN dbo.fnGetMinThreshold('Temperature') AND dbo.fnGetMaxThreshold('Temperature')


Fast:


SELECT MAX(Temperature) 
FROM #tblStationStatistics 
WHERE StationID = A.StationID
    AND Temperature BETWEEN -10 AND 60

No comments:

Post a Comment