SQL Data Paging Technology

Source: Internet
Author: User
Tags getdate

Data layer data paging three common methods:

 Use AdventureWorks2008

1, Top*top

1 -UseTop*Top2 DECLARE @Start datetime,@end datetime;3 SET @Start=getdate();4 5 DECLARE @PageNumber INT,@PageSize INT,@Sql varchar(Max);6 SET @PageNumber= the;7 SET @Pt=Ten;8 SET @Sql='SELECT t2.* from (9 SELECT TOP ten t1.* fromTen (SELECT TOP' + STR(@PageNumber*@PageSize)+'* from Production.transactionhistoryarchive One ORDER by Referenceorderid ASC) As T1 A ORDER by Referenceorderid DESC) As T2 - ORDER by Referenceorderid ASC'; - EXEC(@sql); the  - SET @end=getdate(); - PRINT Datediff(Millisecond,@Start,@end);

2. Table variables

1 --Working with Table variables2 DECLARE @Start datetime,@end datetime;3 SET @Start=getdate();4 DECLARE @PageNumber INT,@PageSize INT,@Sql varchar(Max);5 SET @PageNumber= the;6 SET @PageSize=Ten;7 8 DECLARE @local_variable Table(RowNumberint Identity(1,1),[TransactionID] [int],9     [ProductID] [int],Ten     [Referenceorderid] [int], One     [Referenceorderlineid] [int], A     [transactiondate] [datetime], -     [Transactiontype] [nchar](1), -     [Quantity] [int], the     [ActualCost] [ Money], -     [ModifiedDate] [datetime]); - Insert  into @local_variable(TransactionID, ProductID, Referenceorderid, Referenceorderlineid, TransactionDate, Transactiontype, Quantity, ActualCost, ModifiedDate) - SELECT TOP 50000TransactionID, ProductID, Referenceorderid, Referenceorderlineid, TransactionDate, Transactiontype, Quantity, ActualCost, ModifiedDate fromProduction.transactionhistoryarchiveORDER  byReferenceorderidASC + Select *  from @local_variable whereRowNumber>(@PageNumber-1)*@PageSize  andRowNumber<= @PageNumber*@PageSize -  + SET @end=getdate(); A PRINT Datediff(Millisecond,@Start,@end);

3. Temporary table

--working with temporary tablesDECLARE @Start datetime,@end datetime;SET @Start=getdate();DECLARE @PageNumber INT,@PageSize INT,@Sql varchar(Max);SET @PageNumber= the;SET @PageSize=Ten;Create Table#local_variable (RowNumberint Identity(1,1),[TransactionID] [int],    [ProductID] [int],    [Referenceorderid] [int],    [Referenceorderlineid] [int],    [transactiondate] [datetime],    [Transactiontype] [nchar](1),    [Quantity] [int],    [ActualCost] [ Money],    [ModifiedDate] [datetime]);Insert  into#local_variable (TransactionID, ProductID, Referenceorderid, Referenceorderlineid, TransactionDate, Transactiontype, Quantity, ActualCost, ModifiedDate)SELECT TOP 50000TransactionID, ProductID, Referenceorderid, Referenceorderlineid, TransactionDate, Transactiontype, Quantity, ActualCost, ModifiedDate fromProduction.transactionhistoryarchiveORDER  byReferenceorderidASCSelect *  from#local_variablewhereRowNumber>(@PageNumber-1)*@PageSize  andRowNumber<= @PageNumber*@PageSizeSET @end=getdate();PRINT Datediff(Millisecond,@Start,@end);

4, Row_number

 

--Using Row_number
DECLARE @Start datetime, @end datetime;
SET @Start =getdate ();

DECLARE @PageNumber int, @PageSize int, @Sql varchar (max);
SET @PageNumber = 5000;
SET @PageSize = 10;
SELECT * FROM
(SELECT row_number ()
Over (ORDER by Referenceorderid) as RowNumber,
*
From production.transactionhistoryarchive) as T
WHERE t.rownumber<[email protected]* @PageSize and T.rownumber> (@PageNumber-1) * @PageSize;

SET @end =getdate ();
PRINT Datediff (Millisecond, @Start, @end);

SQL Data Paging Technology

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.