title: SQL MAX() function is very slow, is there a better method?
body: <p>I have a SQL database and ASP.NET web application and most of my queries involve the SQL max function.</p>
  
  <p>For example, the below query takes between approx. 36 seconds to execute (when using the profiler) on both the ASP.NET website and the SSMS.</p>
  
  <pre><code>SELECT MAX(CONVERT(FLOAT,ISNULL(Runhrs,Runho))) - 
         MIN(CONVERT(FLOAT,ISNULL(Runhrs,Runho))) AS ACTUALHOURSRUN 
  FROM REPORTINGSYSTEM.DBO.HL_LOGS 
  WHERE ID_LOCATION = @ID_LOCATION AND 
         CONVERT(VARCHAR,TIME_STAMP,102) 
         BETWEEN @STARTDATE AND @ENDDATE
  </code></pre>
  
  <p>The table in question has approx. 5,000,000 records and 45 columns.</p>
  
  <p>What is the best/fastest/most efficient way of executing the query to reduce the execution time?</p>
  
  <p>Thanks in advance...</p>