SQL Server multiple paging query efficiency

Source: Internet
Author: User

About the SQL statement paging, there are many online, I posted part of it, and summed up their known pages to the following, convenient for future inspection.

Method 1

Applies to any version of SQL Server

    1. SELECT TOP Page size *
    2. From table1
    3. WHERE ID not in
    4. (
    5. SELECT TOP Page Size * (pages-1) ID from table1 ORDER by id
    6. )
    7. ORDER by ID

Method 2

Applies to any version of SQL Server

  1. --Sequential notation:
  2. SELECT TOP Page size *
  3. From table1
  4. WHERE ID >=
  5. (
  6. SELECT ISNULL(MAX(ID),0)
  7. From
  8. (
  9. SELECT TOP (page Size * (pages-1) +1) ID from table1 ORDER by id
  10. ) A
  11. )
  12. ORDER by ID
  13. --Descending notation:
  14. SELECT TOP Page size *
  15. From table1
  16. WHERE ID <=
  17. (
  18. SELECT ISNULL(MIN(ID),0)
  19. From
  20. (
  21. SELECT TOP (page Size * (pages-1) +1) ID from table1 ORDER by id Desc
  22. ) A
  23. )
  24. ORDER by ID Desc

Method 3

Applies to SQL Server 2005

    1. select TOP page size *
    2. from
    3. (
    4. SELECT Row_number () over (order by Id as rownumber,* from table1
    5. A
    6. WHERE rownumber > page Size * (page-1

Description, Page size: Number of rows per page; pages: pages. When using, replace "page size" and "page size * (number of pages-1)" with numbers.

Other scenarios: If you don't have a primary key, you can use a temporary table, or you can do it with scenario three, but the efficiency will be low. When tuning is recommended, the query efficiency increases with the primary key and index.

Using SQL Query Analyzer, show comparison: My conclusion is: Paging Scenario two: (using the ID is greater than how much and select top paging) The most efficient, need to splice SQL statement paging Scheme one: (with not in and select top paging) efficiency, the need to splice SQL statements Paging Scenario Three: (Paging with SQL cursor stored procedures) is the least efficient, but the most common

Http://www.wxzzz.com/457.html

Http://www.cnblogs.com/morningwang/archive/2009/01/02/1367277.html

SQL Server multiple paging query efficiency

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.