数据库分页存储过程集合

USE AdventureWorks2008
GO
SELECT COUNT(*) FROM Production.TransactionHistoryArchive
GO
SELECT TOP 50 * FROM Production.TransactionHistoryArchive
ORDER BY ReferenceOrderID ASC
GO

--Use Top*Top
DECLARE @Start datetime,@end datetime;
SET @Start=getdate();

DECLARE @PageNumber INT, @Count INT, @Sql varchar(max);
SET @PageNumber=5000;
SET @Count=10;
SET @Sql='SELECT T2.* FROM (
 SELECT TOP 10 T1.* FROM
  (SELECT TOP ' + STR(@PageNumber*@Count) +' * FROM Production.TransactionHistoryArchive
  ORDER BY ReferenceOrderID ASC) AS T1
 ORDER BY ReferenceOrderID DESC) AS T2
ORDER BY ReferenceOrderID ASC';
EXEC (@sql);

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

--USE table value sql2005
DECLARE @Start datetime,@end datetime;
SET @Start=getdate();
DECLARE @PageNumber INT, @Count INT, @Sql varchar(max);
SET @PageNumber=5000;
SET @Count=10;

DECLARE @local_variable table (RowNumber int 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 50000 TransactionID, ProductID, ReferenceOrderID, ReferenceOrderLineID, TransactionDate, TransactionType, Quantity, ActualCost, ModifiedDate from Production.TransactionHistoryArchive ORDER BY ReferenceOrderID ASC
select * from @local_variable where RowNumber > (@PageNumber-1)*@Count and RowNumber <= @PageNumber*@Count

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

--USE temp table
DECLARE @Start datetime,@end datetime;
SET @Start=getdate();
DECLARE @PageNumber INT, @Count INT, @Sql varchar(max);
SET @PageNumber=5000;
SET @Count=10;

create table #local_variable(RowNumber int 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 50000 TransactionID, ProductID, ReferenceOrderID, ReferenceOrderLineID, TransactionDate, TransactionType, Quantity, ActualCost, ModifiedDate from Production.TransactionHistoryArchive ORDER BY ReferenceOrderID ASC
select * from #local_variable where RowNumber > (@PageNumber-1)*@Count and RowNumber <= @PageNumber*@Count

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

--Use ROW_NUMBER
DECLARE @Start datetime,@end datetime;
SET @Start=getdate();

DECLARE @PageNumber INT, @Count INT, @Sql varchar(max);
SET @PageNumber=5000;
SET @Count=10;
SELECT * FROM
( SELECT ROW_NUMBER() 
   OVER(ORDER BY ReferenceOrderID) AS RowNumber,   
   *
 FROM Production.TransactionHistoryArchive) AS T
WHERE T.RowNumber<=@PageNumber*@Count AND T.RowNumber>(@PageNumber-1)*@Count;

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

你可能感兴趣的:(存储过程)