Analysis of efficiency of SQL paging query

Source: Internet
Author: User

The efficiency of the higher efficiency in 2 is selected: Row_number (), offset fetch

Table test has 1000 data, 2 fields: field1 (int), field2 (nvarchar)

--1000 data, query 500 times 第1-10 line, 39s
--1000 data, query 500 times 第500-550 line, 87s
--1000 data, query 500 times 第150-160 line, 88s
DECLARE @uId int
SET @uId =1 begin while @uId <=500 begin
SELECT *
From (
SELECT row_number () over (
ORDER by [dbo]. [Test]. [FILED1] desc) as rownum,* from [dbo]. [Test]) Temp
WHERE Temp.rownum between and 160;


SET @[email protected]+1 end end;

--1000 data, query 500 times 第1-10 line, 78s
--1000 data, query 500 times 第500-550 line, 78s
DECLARE @uId int
SET @uId =1 begin while @uId <=500 begin
SELECT *
from [dbo]. [Test]
ORDER by [dbo]. [Test]. [Filed1] DESC
OFFSET (* (15-1)) rows FETCH NEXT ten rows only
SET @[email protected]+1 End End

Test results:

Row_number () unstable, the record query before the test is faster, after the record query time will increase

OFFSET FETCH is stable, the query time before the test is basically the same



Analysis of efficiency of SQL paging query

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.