Efficient way of getting @@rowcount from a query using row_number

后端 未结 4 1324
-上瘾入骨i
-上瘾入骨i 2020-12-04 08:37

I have an expensive query using the row_number over() functionality in SQL Server 2005. I return only a sub list of those records as the query is paginated. However, I wou

4条回答
  •  抹茶落季
    2020-12-04 09:31

    If count(*) is slow you really need to address that issue first by carefully examining your indexes and making sure your statistics are up to date.

    In my experience, there is nothing better than doing two separate queries, one to get the data page, and one to get the total count. Using a temporary table in order to get total counts is a losing strategy as your number of rows increases. E.g., the cost of inserting 10,000,000 million rows into a temp table simply to count them is obviously going to be excessive.

提交回复
热议问题