五种SQL Server分页存储过程的方法及性能比较

所属分类: 数据库 / MsSql 阅读数: 879
收藏 0 赞 0 分享

在SQL Server数据库操作中,我们常常会用到存储过程对实现对查询的数据的分页处理,以方便浏览者的浏览。本文我们总结了五种SQL Server分页存储过程的方法,并对其性能进行了比较,接下来就让我们来一起了解一下这一过程。

创建数据库data_Test :

create database data_Test  
 
GO  
 
use data_Test  
 
GO  
 
create table tb_TestTable  --创建表  
 
(  
 
id int identity(1,1) primary key,  
 
userName nvarchar(20) not null,  
 
userPWD nvarchar(20) not null,  
 
userEmail nvarchar(40) null  
 
)  
 
GO 

插入数据

set identity_insert tb_TestTable on  
 
declare @count int  
 
set@count=1  
 
while @count<=2000000  
 
begin  
 
insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn')  
 
set @count=@count+1  
 
end  
 
set identity_insert tb_TestTable off 

1、利用select top 和select not in进行分页

具体代码如下:

create procedure proc_paged_with_notin --利用select top and select not in  
 
(  
 
@pageIndex int, --页索引  
 
@pageSize int  --每页记录数  
 
)  
 
as  
 
begin  
 
set nocount on;  
 
declare @timediff datetime --耗时  
 
declare @sql nvarchar(500)  
 
select @timediff=Getdate()  
 
set @sql='select top '+str(@pageSize)+' * from tb_TestTable where(ID not in(select top '+str(@pageSize*@pageIndex)+' id from tb_TestTable order by ID ASC)) order by ID'  
 
execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql  
 
select datediff(ms,@timediff,GetDate()) as 耗时  
 
set nocount off;  
 
end 

2、利用select top 和 select max(列键)

create procedure proc_paged_with_selectMax --利用select top and select max(列)  
 
(  
 
@pageIndex int, --页索引  
 
@pageSize int  --页记录数  
 
)  
 
as  
 
begin  
 
set nocount on;  
 
declare @timediff datetime  
 
declare @sql nvarchar(500)  
 
select @timediff=Getdate()  
 
set @sql='select top '+str(@pageSize)+' * From tb_TestTable where(ID>(select max(id) From (select top '+str(@pageSize*@pageIndex)+' id From tb_TestTable order by ID) as TempTable)) order by ID'  
 
execute(@sql)  
 
select datediff(ms,@timediff,GetDate()) as 耗时  
 
set nocount off;  
 
end 

3、利用select top和中间变量

create procedure proc_paged_with_Midvar --利用ID>最大ID值和中间变量  
 
(  
 
@pageIndex int,  
 
@pageSize int  
 
)  
 
as  
 
declare @count int  
 
declare @ID int  
 
declare @timediff datetime  
 
declare @sql nvarchar(500)  
 
begin  
 
set nocount on;  
 
select @count=0,@ID=0,@timediff=getdate()  
 
select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id  
 
set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID)  
 
execute(@sql)  
 
select datediff(ms,@timediff,getdate()) as 耗时  
 
set nocount off;  
 
end 

4、利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引

create procedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number()  
 
(  
 
@pageIndex int,  
 
@pageSize int  
 
)  
 
as  
 
declare @timediff datetime  
 
begin  
 
set nocount on;  
 
select @timediff=getdate()  
 
select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1)  
 
select datediff(ms,@timediff,getdate()) as 耗时  
 
set nocount off;  
 
end 

5、利用临时表及Row_number

create procedure proc_CTE --利用临时表及Row_number  
 
(  
 
@pageIndex int, --页索引  
 
@pageSize int  --页记录数  
 
)  
 
as  
 
set nocount on;  
 
declare @ctestr nvarchar(400)  
 
declare @strSql nvarchar(400)  
 
declare @datediff datetime  
 
begin  
 
select @datediff=GetDate()  
 
set @ctestr='with Table_CTE as  
 
(select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)';  
 
set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex)  
 
end  
 
begin  
 
execute sp_executesql @strSql  
 
select datediff(ms,@datediff,GetDate())  
 
set nocount off;  
 
end 

以上的五种方法中,网上说第三种利用select top和中间变量的方法是效率最高的。

关于SQL Server数据库分页的存储过程的五种方法及性能比较的知识就介绍到这里了,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

SQL 提权 常用命令

SQL 提权 常用命令,大家可以考虑下将sqlserver的服务运行权限设置为普通用户,即可防止下面的提权。
收藏 0 赞 0 分享

数据转换冲突及转换过程中大对象的处理

数据转换冲突及转换过程中大对象的处理方法,大家可以参考下。
收藏 0 赞 0 分享

SQLServer 数据库开发顶级技巧

无论你的专业水平如何,从其他IT专家那里学习新的技巧与最佳实践常常都是有益的。本文包含了我遇到过的SQL Server开发的高级技巧。希望其中的一些技巧能够对您的数据库开发及管理工作有所帮助。
收藏 0 赞 0 分享

远程连接SQLSERVER 2000服务器方法

需求如下:需要远程连接外地的SQL Server 2000服务器。
收藏 0 赞 0 分享

SQLserver2000 企业版 出现"进程51发生了严重的异常"错误的处理方法

SQL2000 企业版 出现“进程51发生了严重的异常”错误的解决方法,利用了微软官方的工具。
收藏 0 赞 0 分享

SQLServer 触发器 数据库进行数据备份

首先,你需要建立测试数据表,一个用于插入数据:test3,另外一个作为备份:test3_bak
收藏 0 赞 0 分享

SQLServer 数据库备份过程中经常遇到的九种情况

SQLServer 数据库备份过程中经常遇到各种问题,大家可以参照下面的问题,来分析下,快速的解决问题。
收藏 0 赞 0 分享

SQL 截取字符串应用代码

字符串截取函数,只限单字节字符使用(对于中文的截取时遇上奇数长度是会出现乱码,需另行处理),本函数可截取字符串指定范围内的字符。
收藏 0 赞 0 分享

除MSSQL数据库text字段中恶意脚本的删方法

删除MSSQL数据库text字段的替换处理示例--全表替换,看到有人提问,所以整理了一个好久以前的处理方法,以供大家参考
收藏 0 赞 0 分享

sql 普通行列转换

说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。
收藏 0 赞 0 分享
查看更多