几种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 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

时间: 2024-09-07 13:26:24

几种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数据库操作中,我们常常会用到存储过程对实现对查询的数据的分页处理,以方便浏览者的浏览.本文我们总结了五种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 分页查询存储过程代码_MsSql

复制代码 代码如下: CREATE PROCEDURE [dbo].[up_Pager] @table varchar(2000), --表名 @col varchar(50), --按该列来进行分页 @orderby bit, --排序,0-顺序,1-倒序 @collist varchar(800),--要查询出的字段列表,*表示全部字段 @pagesize int, --每页记录数 @page int, --指定页 @condition varchar(800) --查询条件 AS DECL

SQL Server 分页查询存储过程代码

  EXEC up_Pager '(SELECT * FROM 表名)aa','要排序的列名',0-顺序或1-倒序,'显示列',每页记录数,指定页,'条件' EXEC up_Pager '(SELECT * FROM T_Gather_Page)aa','SaveTime',1,'*',40,3,''   CREATE PROCEDURE [dbo].[up_Pager] @table varchar(2000), --表名 @col varchar(50), --按该列来进行分页 @order

sql server平台用存储过程进行分页的两种方法

server|存储过程|分页 killergo的专栏 最近因为稍微有点空闲时间,所以想了下在sql server平台用存储过程的分页方式,现在列示在下面. 实际测试时,在15000条数据情况下两者性能大体相当,在20000-30000条数据的情况下前者明显比后者性能更佳.更大数据量没有进行测试了. 注意,数据表里面是否有 键和索引 对性能的影响相当大-----------------------------------------------------第一种: /*第一个参数是每页条数,第二个

浅谈基于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 分页存储过程的演进

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

关于sql server查询语句的写法。

问题描述 关于sql server查询语句的写法. 怎样写一个查询语句select distinct ID from TrainTime order by ID select Station from TrainTime where S_No='1'order by ID select Stationfrom TrainTime where D_Time='-' order by ID 怎样把这3个查询语句写为一句啊,让查询查来的结果为这3列数据. 因为我想建一个表,为3列,列名为:列车车次.起

SQL Server基础之&lt;存储过程&gt;

原文:SQL Server基础之<存储过程>   简单来说,存储过程就是一条或者多条sql语句的集合,可视为批处理文件,但是其作用不仅限于批处理.本篇主要介绍变量的使用,存储过程和存储函数的创建,调用,查看,修改以及删除操作. 一:存储过程概述  SQL Server中的存储过程是使用T_SQL编写的代码段.它的目的在于能够方便的从系统表中查询信息,或者完成与更新数据库表相关的管理任务和其他的系统管理任务.T_SQL语句是SQL Server数据库与应用程序之间的编程接口.在很多情况下,一些代