Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:45:32+00:00 2026-05-13T10:45:32+00:00

I’m working on returning a recordset from SQL Server 2008 to do some pagination.

  • 0

I’m working on returning a recordset from SQL Server 2008 to do some pagination. I’m only returning 15 records at a time, but I need to have the total number of matches along with the subset of records. I’ve used two different queries with mixed results depending on where in the larger group I need to pull the subset. Here’s a sample:

SET NOCOUNT ON;
WITH tempTable AS (
  SELECT
     FirstName
     , LastName
     , ROW_NUMBER() OVER(ORDER BY FirstName ASC) AS RowNumber 
   FROM People
   WHERE 
      Active = 1
)

SELECT 
   tempTable.*     
   , (SELECT Max(RowNumber) FROM tempTable) AS Records    
FROM tempTable     
WHERE
   RowNumber >= 1
   AND RowNumber <= 15
ORDER BY
   FirstName

This query works really fast when I’m returning items on the low end of matches, like records 1 through 15. However, when I start returning records 1000 – 1015, the processing will go from under a second to more than 15 seconds.

So I changed the query to the following instead:

SET NOCOUNT ON;
WITH tempTable AS (
  SELECT * FROM (
     SELECT
        FirstName
        , LastName
        , ROW_NUMBER() OVER(ORDER BY FirstName ASC) AS RowNumber 
        , COUNT(*) OVER(PARTITION BY NULL) AS Records
      FROM People
      WHERE 
         Active = 1
   ) derived
   WHERE RowNumber >= 1 AND RowNumber <= 15
)

SELECT 
   tempTable.*     
FROM tempTable     
ORDER BY
   FirstName

That query runs the high number returns in 2-3 seconds, but also runs the low number queries in 2-3 seconds as well. Because it’s doing the count for each of 70,000+ rows, it makes every request take longer instead of just the large row numbers.

So I need to figure out how to get a good row count, as well as only return a subset of items at any point in the resultset without suffering such a huge penalty. I could handle a 2-3 second penalty for the high row numbers, but 15 is too much, and I’m not willing to suffer slow loads on the first few pages a person views.

NOTE: I know that I don’t need the CTE in the second example, but this is just a simple example. In production I’m doing further joins on the tempTable after I’ve filtered it down to the 15 rows I need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:45:32+00:00Added an answer on May 13, 2026 at 10:45 am

    Here is what I have done (and its just as fast, no matter which records I return):

    --Parameters include:
    @pageNum int = 1,
    @pageSize int = 0,
    
    
    
    DECLARE 
        @pageStart int,
        @pageEnd int
    
    SELECT
        @pageStart = @pageSize * @pageNum - (@pageSize - 1),
        @pageEnd = @pageSize * @pageNum;
    
    
    SET NOCOUNT ON;
    WITH tempTable AS (
        SELECT
            ROW_NUMBER() OVER (ORDER BY FirstName ASC) AS RowNumber,
            FirstName
            , LastName
        FROM People
        WHERE Active = 1
    )
    
    SELECT
        (SELECT COUNT(*) FROM tempTable) AS TotalRows,
        *
    FROM tempTable
    WHERE @pageEnd = 0
    OR RowNumber BETWEEN @pageStart AND @pageEnd
    ORDER BY RowNumber
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Give this a try; procedure VertCenterLine(RichEdit: TRichEdit; LineNum: Integer); //… May 14, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer UITabBarController provides a delegate method called tabBarController:shouldSelectViewController:. Override that and… May 14, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer There are no "officially supported mimetypes for Android" except to… May 14, 2026 at 9:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.