最快的存储过程分页 50W

简介:

过程一:
select top N条记录 * from 文章表 where id not in(select top M条记录 id from 文章表 order by id desc ) order by id desc

过程二:
select top N条记录 * from 文章表 where id <(select min(id) from (select top M条记录 id from 文章表 order by id desc ) as tblTmp) order by id desc

--简单通用
CREATE PROCEDURE [dbo].[PageView]
    @select VARCHAR(max),
    @CurrentPage INT,
    @PageSize INT
AS
BEGIN
    declare @sql NVARCHAR(max)
    DECLARE @RecordCurrent INT
    DECLARE @PageCount INT
    DECLARE @RecordCount INT
    SET NOCOUNT ON
    set @sql='select @RecordCount=count(*) from ('+@select+') a'
    exec sp_executesql @sql,N'@RecordCount int output',@RecordCount output
    SET @PageCount=(@RecordCount+@PageSize-1)/@PageSize
    IF ISNULL(@CurrentPage,0)<1
        SET @CurrentPage=1
    ELSE if ISNULL(@CurrentPage,0)>@PageCount
        SET @CurrentPage=@PageCount
    SELECT @CurrentPage AS CurrentPage,@RecordCount AS RecordCount,@PageSize AS PageSize,@PageCount AS PageCount
    set @sql='select * from ('+@select+') a where rownumber between '+cast((@CurrentPage-1)*@PageSize+1 as varchar)+' and '+cast(@CurrentPage*@PageSize as varchar)
    exec (@sql)
END
--使用not in 方式的存储过程
create PROCEDURE GetPageDataByNotIn
@PageIndex int,/**//*当前页数*/ 
@PageSize int/**//*每页大小*/ 
AS
declare @starttime datetime
    set @starttime=getdate()
IF @PageIndex > 0
BEGIN
set nocount on;
DECLARE @PageLowerBound int
DECLARE @StartID int
DECLARE @sql varchar(225)
SET @PageLowerBound = @PageSize * (@PageIndex-1)
IF @PageLowerBound<1
   SET @PageLowerBound=1
    print @PageLowerBound
select top (@PageSize) * from table where [ar_id] not in (select top ((@PageSize)*(@PageIndex-1)) ar_id from table )
EXEC(@sql)
set nocount off; 
END
print '耗时='+convert(varchar(30),datediff(ms,@starttime,getdate()))
-------------------------------------------------------------------------------------------------------
--使用ROWCOUNT的分页存储过程
create PROCEDURE GetPageData
@PageIndex int,/**//*当前页数*/ 
@PageSize int/**//*每页大小*/ 
AS
declare @starttime datetime
    set @starttime=getdate()
IF @PageIndex > 0
BEGIN
set nocount on;
DECLARE @PageLowerBound int
DECLARE @StartID int
DECLARE @sql varchar(225)
SET @BeginIndex = @PageSize * (@PageIndex-1)
IF @BeginIndex<1
   SET @BeginIndex=1
SET ROWCOUNT @BeginIndex
SELECT @StartID = [ar_id] FROM table ORDER BY ar_id 
print @StartID
SET ROWCOUNT 0
SET @sql='select top '+str(@PageSize) +' * from table where [ar_id]>='+ str(@StartID) +' ORDER BY [ar_id] '
EXEC(@sql)
set nocount off; 
END
print '耗时='+convert(varchar(30),datediff(ms,@starttime,getdate()))

-----------------------------------------------------------------------------------------------------
测试查询一张有100W条数据的表table,每页显示10条数据
存储过程                        第1页   第10页   第100页   第1000页   第5000页   
GetPageDataByNotIn    0           0            126           13530       等了2分多钟没耐性了....
GetPageData                0           0            0               16              76

------------------------------------------------------------------------------------------------------------
最后使用的存储过程(推荐):
create PROCEDURE GetPageData
(
@TableName varchar(30),--表名称
@IDName varchar(20),--表主键名称
@PageIndex int,--当前页数 
@PageSize int--每页大小 
)
AS
IF @PageIndex > 0
BEGIN
set nocount on
   DECLARE @PageLowerBound int,@StartID int,@sql nvarchar(225)
   SET @PageLowerBound = @PageSize * (@PageIndex-1)
   IF @PageLowerBound<1
    SET @PageLowerBound=1
   SET ROWCOUNT @PageLowerBound
   SET @sql=N'SELECT @StartID = ['+@IDName+'] FROM '+@TableName+' ORDER BY '+@IDName
     exec sp_executesql @sql,N'@StartID int output',@StartID output
   SET ROWCOUNT 0
   SET @sql='select top '+str(@PageSize) +' * from '+@TableName+' where ['+@IDName+']>='+ str(@StartID) +' ORDER BY ['+@IDName+'] '
   EXEC(@sql)
set nocount off
END




















本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366654,如需转载请自行联系原作者


相关文章
|
6月前
|
存储
SQLServer分页获取数据的存储过程
SQLServer分页获取数据的存储过程
24 0
|
6月前
|
存储 开发框架 前端开发
基于MySQL 实现通用分页存储过程(下篇-超详细)(上)
基于MySQL 实现通用分页存储过程(下篇-超详细)
65 0
|
6月前
|
存储 XML 安全
基于MySQL 实现通用分页存储过程(下篇-超详细)(下)
基于MySQL 实现通用分页存储过程(下篇-超详细)
37 0
|
9月前
|
存储
【干货】SqlServer 总结几种存储过程分页的使用
【干货】SqlServer 总结几种存储过程分页的使用
|
11月前
|
存储 SQL Perl
PL/SQL编程—分页功能(存储过程)
PL/SQL编程—分页功能(存储过程)
87 0
|
存储
通用分页存储过程,干货无污染
通用分页存储过程,干货无污染