Performance Comparison of Three paging modes of sqlserver

Source: Internet
Author: User

Create a clustered index for the CreateTime column in The Liwu_Items table

First, the paging syntax unique to sqlserver2005
Copy codeThe Code is as follows:
Declare @ page int
Declare @ pagesize int
Set @ page = 2
Set @ pagesize = 12

Set statistics io on
SELECT a. * FROM (
SELECT ROW_NUMBER () OVER (order by B. CreateTime DESC) AS [ROW_NUMBER], B .*
FROM [dbo]. [Liwu_Items] AS B) AS
WHERE a. [ROW_NUMBER] BETWEEN @ pagesize + 1 AND (@ page * @ pagesize)
Order by a. [ROW_NUMBER]

Result:

(12 rows affected) Table 'liwu _ Items '. 1 scan count, 7 logical reads, 0 physical reads, 0 pre-reads, 0 lob logical reads, 0 physical reads, and 0 lob pre-reads.
Logical reads are 7 times

Execution Plan:

The main overhead is clustered index scanning.

Second, two top orders are arranged in the forward and reverse order, and a total of other subqueries are used to implement paging,
Copy codeThe Code is as follows:
Declare @ page int
Declare @ pagesize int
Set @ page = 2
Set @ pagesize = 12

Set statistics io on
Select * from (
Select top (@ pagesize) * from
(Select top (@ page * @ pagesize) * from Liwu_Items order by CreateTime desc)
Order by CreateTime asc) B
Order by CreateTime desc

Result

(12 rows affected) Table 'liwu _ Items '. 1 scan count, 7 logical reads, 0 physical reads, 317 pre-reads, 0 lob logical reads, 0 lob physical reads, and 0 lob pre-reads.
Execution Plan

The execution plan is similar to the first one, but the two orders occupy a large amount of resources.

The third is the most spam, which is implemented using the not in clause, as follows:
Copy codeThe Code is as follows:
Declare @ page int
Declare @ pagesize int
Set @ page = 2
Set @ pagesize = 12

Set statistics io on
Select top (@ pagesize) * from Liwu_Items
Where ItemId not in (
Select top (@ page-1) * @ pagesize) ItemId from Liwu_Items order by CreateTime desc)
Order by CreateTime Desc

Result

(12 rows affected) Table 'worktable '. 1 scan count, 70 logical reads, 0 physical reads, 0 pre-reads, 0 lob logical reads, 0 physical reads, and 0 lob pre-reads. Table 'liwu _ Items '. Scan count 2, logical read 18 times, physical read 0 times, pre-read 0 times, lob logical read 0 times, lob physical read 0 times, lob pre-read 0 times.
The performance is the worst. The two tables are processed with high logic reading and perspiration.

Execution Plan

This execution plan cannot be understood. nested loops and table off-line occupy a large amount of resources.

Conclusion: The second paging method is similar to the first paging method, but the second method can be used for SQL Server or access in earlier versions.

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.