title: SQL Offset total row count slow with IN Clause
body: <p>I am using the below SQL code based on another answer. However when including the massive in clause, getting the total count takes too long. If I remove the total count, then the query takes less than 1 second. Is there a more efficient way to get the total row count? The answers I saw were based off of 2013 SQL queries.</p>
  
  <pre><code>DECLARE 
      @PageSize INT = 10, 
      @PageNum  INT = 1;
  
  WITH TempResult AS(
      SELECT ID, Name
      FROM Table
       Where ID in ( 1 ,2 3, 4, 5, 6, 7, 8, 9 ,10)
  ), TempCount AS (
      SELECT COUNT(*) AS MaxRows FROM TempResult
  )
  SELECT *
  FROM TempResult, 
   TempCount    &lt;----- this is what is slow. Removing this and the query is super fast
  ORDER BY TempResult.Name
      OFFSET (@PageNum-1)*@PageSize ROWS
      FETCH NEXT @PageSize ROWS ONLY
  </code></pre>