SQL Server Page OFFSET and FETCH clauses for paging

Source: Internet
Author: User

SQL Server 202 adds new features that add OFFSET and FETCH clauses to the ORDER BY clause to achieve a paged query effect.


The syntax in the ORDER BY clause is as follows: (reference: ORDER BY clause (Transact-SQL))

ORDER by Order_by_expression    [COLLATE collation_name]     [ASC | DESC]     [,... n] [<offset_fetch>]<offset_fetch>:: ={     offset {integer_constant | offset_row_count_e Xpression} {ROW | ROWS}    [      FETCH {First | NEXT} {integer_constant | fetch_row_count_expression} {ROW | ROWS} only    ]}

In the <offset_fetch> clause:

First and NEXT are synonyms, and are provided for compatibility with ANSI.
ROW and ROWS are synonyms, and are provided for compatibility with ANSI.


Simulation test:

--drop table dbo. Testtabcreate table dbo. Testtab (id int identity () NOT NULL PRIMARY key clustered,dtime datetime default (GETDATE ())) Goset Nocount OnInsert into Dbo. Testtab default Valuesgo 50000set nocount offselect Count (*) from dbo. Testtab (NOLOCK)

a common paging query for distribution comparison:

Compare queries for 1~100,20001~20100,49001~49100 rows.

SELECT id,dtime from dbo. Testtaborder by idoffset 1 rows FETCH NEXT to Rows Onlyselect id,dtime from (SELECT row_number () over (ORDER by ID ASC) as ORDERID,ID,DTIMEFROM dbo. Testtab) Tabwhere OrderID between 1 and 100ORDER by OrderID

the results are as follows: IO is the same (because the query time is 0, estimates are not allowed, temporarily not statistics. )

 

1~100

5001~5100 Line

9900~10000 Line

Estimated number of rows

OFFSET FETCH

Cost-to-account ratio

49%

84%

80D

100

Row_number

Cost-to-account ratio

51%

16%

10%

9


Row_number is compiling memory, CPU is more than OFFSET FETCH.

In the above statistics: the record of the OFFSET FETCH query is more in the table, the overhead is greater, and the estimated number of rows is accurate.


One of the benefits of OFFSET FETCH is the simplified paging query statement! Other tests are pending! ~



SQL Server Page OFFSET and FETCH clauses for paging

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.