Another way to page numbering for SQL Server

Source: Internet
Author: User

Today read the T-SQL, see unbounded preceding, want to compare to the next row_number () running speed.

SQL and related results are as follows, the data in the database is 5w+.

Row_number ():

SET STATISTICS  on SELECT  over (ORDER by DESC) Rn,id from          Dbo. T_mycourse

Run results

unbounded preceding

SET STATISTICS  on SELECT    SUM (1over (ORDERbyDESCbetweenand current  ROW) rn,idfrom    dbo. T_mycourse

Run results

After the run, see the results, using Microsoft's official methods for numbering sorting, the speed significantly increased.

But when I use the above method to get the data paged, the results are a little different.

Paging for data:

Row_number () Paging Get data:

SET STATISTICSTime onSELECT    * from    (    SELECTrow_number () Over(ORDER  byIdDESC) Rn,id fromdbo. T_mycourse) AWHEREA.rnbetween  -  and 444

Execute SQL command: DBCC dropcleanbuffers, clearing results after database cache

Unbounded Paging for data:

SET STATISTICSTime onSELECT    * from    (    SELECT        SUM(1) Over(ORDER  byIdDESCROWSbetweenunbounded preceding and  CurrentROW) Rn,id fromdbo. T_mycourse) AWHEREA.rnbetween  A  and 444

Unbounded this mode executes the DBCC DROPCLEANBUFFERS clears the cached SQL and does not change the execution time.

through the above results, see row_number () Get paging data significantly faster, I guess is Microsoft to row_number () This method has been optimized, there may be cache, read the data in the cache and then paging.

If there are friends who know, please comment tell me, let me learn to learn.

Another way to page numbering for SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.