热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

浅谈基于SQLServer分页存储过程五种方法及性能比较

本文由脚本之家小编给大家分享了五种sqlserver分页存储过程及性能比较,接下来我们跟着小编一起了解了解吧

在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()  
declare @strSql nvarchar()  
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分页存储过程五种方法及性能比较的全部内容就到此结束了,希望对大家有所帮助。


推荐阅读
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • 本文主要复习了数据库的一些知识点,包括环境变量设置、表之间的引用关系等。同时介绍了一些常用的数据库命令及其使用方法,如创建数据库、查看已存在的数据库、切换数据库、创建表等操作。通过本文的学习,可以加深对数据库的理解和应用能力。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
author-avatar
乐天小散_608
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有