title: SQL ROW_NUMBER() over performance problem
body: <p>I have this SQL that works fine.</p>
  
  <p>Want the my filter to return the LATEST unique SessionGuids with the highest UserSessionSequenceID.</p>
  
  <p>Problem is performance sucks - even though I have good indexes.
  How can I rewrite this - to omit the ROW_NUMBER line?</p>
  
  <pre><code>SELECT TOP(@resultCount) * FROM 
  (
      SELECT
          [UserSessionSequenceID]
          ,[SessionGuid]
          ,[IP]
          ,[Url]
          ,[UrlTitle]
          ,[SiteID]
          ,[BrowserWidth]
          ,[BrowserHeight]
          ,[Browser]
          ,[BrowserVersion]
          ,[Referer]
          ,[Timestamp]
          ,ROW_NUMBER() over (PARTITION BY [SessionGuid] 
                                      ORDER BY UserSessionSequenceID DESC) AS sort 
     FROM [tblSequence]
  ) AS t     
  WHERE ([Timestamp] &gt; DATEADD(mi, -@minutes, GETDATE())) 
    AND (SiteID = @siteID) 
    AND sort = 1
  ORDER BY [UserSessionSequenceID] DESC
  </code></pre>
  
  <p>Thanks a lot :-)</p>