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 9162347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:11:21+00:00 2026-06-17T14:11:21+00:00

I am a little confused about how SQL server achieves less reads and performance

  • 0

I am a little confused about how SQL server achieves less reads and performance improvements using common table expressions and ROW_NUMBER. Why doesn’t the table actualized in the expression have to perform all the reads which a normal query would have to perform to allow the query to order using ROW_NUMBER?

  • 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-06-17T14:11:23+00:00Added an answer on June 17, 2026 at 2:11 pm

    The CTE is not (necessarily) “actualized”. It’s not that it will inevitably copy all rows elsewhere and will perform other operations over the copy (though it may behave so it the optimizer decides it’s better).

    If we take this simple query:

    SELECT  *
    FROM    (
            SELECT  *,
                    ROW_NUMBER() OVER (ORDER BY id) rn
            FROM    mytable
            ) q
    WHERE   rn BETWEEN 101 AND 110
    

    and look at its plan we’ll see something like this:

      |--Filter(WHERE:([Expr1003]>=(101) AND [Expr1003]<=(110)))
           |--Top(TOP EXPRESSION:(CASE WHEN (110) IS NULL OR (110)<(0) THEN (0) ELSE (110) END))
                |--Sequence Project(DEFINE:([Expr1003]=row_number))
                     |--Segment
                          |--Clustered Index Scan(OBJECT:([ee].[dbo].[mytable].[PK__mytable__3213E83F29C2D227]), ORDERED FORWARD)
    

    Here, the records are scanned (in id order as the table is clustered on id), assigned the ROW_NUMBER (this is what Sequence Project does) and passed on to TOP which just halts execution when a certain threshold is reached (110 records in our case).

    Those 110 records are passed to Filter which only passes the records with rn greater than 100.

    The query itself only scans 110 records:

    SQL Server parse and compile time: 
       CPU time = 0 ms, elapsed time = 1 ms.
    
    (строк обработано: 10)
    Table 'mytable'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
     SQL Server Execution Times:
       CPU time = 0 ms,  elapsed time = 0 ms.
    

    in 3 pages.

    Now let’s see the unpaginated query:

    SELECT  *
    FROM    mytable
    ORDER BY
            id
    

    This one is pretty simple: read everything from the table and spit it out.

      |--Clustered Index Scan(OBJECT:([ee].[dbo].[mytable].[PK__mytable__3213E83F29C2D227]), ORDERED FORWARD)
    

    However, looking easily does not mean done easily. The table is quite large and we need to do many a read to return all records:

    SQL Server parse and compile time: 
       CPU time = 0 ms, elapsed time = 0 ms.
    
    (строк обработано: 1310720)
    Table 'mytable'. Scan count 1, logical reads 2765, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
     SQL Server Execution Times:
       CPU time = 266 ms,  elapsed time = 11690 ms.
    

    So, in a nutshell, the pagination query just knows when to stop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little confused about the roles of a java application server and
I'm a little confused about using release vs. debug versions of a dll for
I'm a little confused about something. I am using Visual Studio 2010 and am
I am a little confused about when do I have to serialize objects using
I am a little confused about Accept-Encoding . I have Web Service which would
I'm a little confused about something. I wrote an app and tested it on
I'm a little confused about the HTTP protocol, from what i know HTTP was
I am a little confused about where to use strong and where to use
I am a little confused about what happens when I copy functors and/or initializers.
I'm a little confused about render, not quite sure which path it chooses to

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.