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

在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数据库分页的存储过程的五种方法及性能比较的知识就介绍到这里了,希望对大家的学习有所帮助。

时间: 2024-08-04 05:02:00

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

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

在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, userN

浅谈基于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, u

几种SQL Server分页的存储过程写法以及性能比较

几种SQL Server分页的存储过程写法以及性能比较 存储过程的5种分页写法,下面的代码是从忘了什么时候从别人那Ctrl+C来的,所以仅仅作为收藏,希望作者看到不要喷我.  ------创建数据库教程data_Test ----- create database data_Test  GO use data_Test GO create table tb_TestTable   --创建表 (     id int identity(1,1) primary key,     userName

简单谈基于SQL SERVER 分页存储过程的演进

server|存储过程|分页 简单谈基于SQL SERVER 分页存储过程的演进 作者:郑佐日期:2006-9-30 针对数据库数据在UI界面上的分页是老生常谈的问题了,网上很容易找到各种"通用存储过程"代码,而且有些还定制查询条件,看上去使用很方便.笔者打算通过本文也来简单谈一下基于SQL SERVER 2000的分页存储过程,同时谈谈SQL SERVER 2005下分页存储过程的演进. 在进行基于UI显示的数据分页时,常见的数据提取方式主要有两种.第一种是从数据库提取所有数据然后在

sql server分页存储过程三种实例

例1.用于通用数据分页  代码如下 复制代码 create procedure [dbo].[Pub_DataPager]     @pTableName nvarchar(1000), --表名     @pFieldNames nvarchar(max),--需要查询的字符串,以 ","分割,也可以是'*'     @pWhere nvarchar(max),  --查询条件     @pOrderby nvarchar(max),        --排序字符串,必需字段    

简析基于SQL SERVER分页存储过程的演进

针对数据库数据在UI界面上的分页是老生常谈的问题了,网上很容易找到各种"通用存储过程"代码,而且有些还定制查询条件,看上去使用很方便.笔者打算通过本文也来简单谈一下基于SQL SERVER 2000的分页存储过程,同时谈谈SQL SERVER 2005下分页存储过程的演进. 在进行基于UI显示的数据分页时,常见的数据提取方式主要有两种.第一种是从数据库提取所有数据然后在系统应用程序层进行数据分页,显示当前页数据.第二种分页方式为从数据库取出需要显示的一页数据显示在UI界面上. 以下是笔

Sql Server 分页存储过程

分页存储过程一: --/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/ --/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/ --/*-----存储过程 分页处理 孙伟 2005-04-21修改 添加Distinct查询功能-------*/ --/*-----存储过程 分页处理 孙伟 2005-05-18修改 多字段排序规则问题-------*/ --/*-----存储过程 分页处理 孙伟 2005-06-

SQL Server 优化存储过程的七种方法

原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会返回相应的影响行数.使用SET NOCOUNT ON后,除了数据集就不会返回额外的信息了,减小网络流量. 2.使用确定的Schema 在使用表,存储过程,函数等等时,最好加上确定的Schema.这样可以使SQL Server直接找到对应目标,避免去计划缓存中搜索.而且搜索会导致编译锁定,最终影响性能

SQL Server 2005存储过程编写指导

    本文让我们谈谈关于这种编写存储过程的新方法的五个常见问题.它们值得你一读的-- 在SQL Server 2000中,只有一种方法编写存储过程:使用T-SQL 语句.学习以前版本的SQL Server中存储过程的编写是一门课程.但是SQL Server 2005让你使用.NET语言家族(主要是VB.NET和C#)来编写存储过程(以及函数.触发器和其他的一些东西)成为可能. 1. 我为什么要使用CLR模型来写存储过程? 主要是因为数据.SQL CLR在一些方面执行较快:其中,字符串处理要比T