title: SELECT MAX() too slow - any alternatives?
body: <p>I've inherited a SQL Server based application and it has a stored procedure that contains the following, but it hits timeout. I believe I've isolated the issue to the SELECT MAX() part, but I can't figure out how to use alternatives, such as ROW_NUMBER() OVER( PARTITION BY... </p>
  
  <p>Anyone got any ideas?</p>
  
  <h2>Here's the "offending" code:</h2>
  
  <pre><code>SELECT BData.*, B.* 
  FROM BData
  INNER JOIN
  (
      SELECT MAX( BData.StatusTime ) AS MaxDate, BData.BID
      FROM BData
      GROUP BY BData.BID
  ) qryMaxDates
  ON ( BData.BID = qryMaxDates.BID ) AND ( BData.StatusTime = qryMaxDates.MaxDate )
  INNER JOIN BItems B ON B.InternalID = qryMaxDates.BID
  WHERE  B.ICID = 2
  ORDER BY BData.StatusTime DESC;
  </code></pre>
  
  <p>Thanks in advance.</p>