------创建数据库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 whereIDRank>@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

  

存储过程的5种分页写法,下面的代码是从忘了什么时候从别人那Ctrl+C来的,所以仅仅作为收藏,希望作者看到不要喷我.

SQL Server分页的存储过程写法以及性能比较的更多相关文章

  1. SQL Server分页查询存储过程

    --分页存储过程create PROCEDURE [dbo].[commonPagination]@columns varchar(500), --要显示的列名,用逗号隔开 @tableName va ...

  2. 理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程

    本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(1)--简介 本文介绍SQL Server如何编译存储过程并使用计划缓存 ...

  3. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

  4. SQL server分页的四种方法

    SQL server分页的四种方法 1.三重循环: 2.利用max(主键); 3.利用row_number关键字: 4.offset/fetch next关键字 方法一:三重循环思路  先取前20页, ...

  5. SQL server分页的四种方法(算很全面了)

      这篇博客讲的是SQL server的分页方法,用的SQL server 2012版本.下面都用pageIndex表示页数,pageSize表示一页包含的记录.并且下面涉及到具体例子的,设定查询第2 ...

  6. SQL Server基础之存储过程

      简单来说,存储过程就是一条或者多条sql语句的集合,可视为批处理文件,但是其作用不仅限于批处理.本篇主要介绍变量的使用,存储过程和存储函数的创建,调用,查看,修改以及删除操作. 一:存储过程概述 ...

  7. SQL SERVER 临时表导致存储过程重编译(recompile)的一些探讨

    SQLSERVER为了确保返回正确的值,或者处于性能上的顾虑,有意不重用缓存在内存里的执行计划,而重新编译执行计划的这种行为,被称为重编译(recompile).那么引发存储过程重编译的条件有哪一些呢 ...

  8. 【SQL Server】SQL Server基础之存储过程

    SQL Server基础之存储过程  阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...

  9. SQL SERVER 分页方法

    最近项目中需要在SQL SERVER中进行分页,需要编写分页查询语句.之前也写过一些关于分页查询的语句,但是性能不敢恭维.于是在业务时间,在微软社区Bing了一篇老外写的关于SQL SERVER分页的 ...

随机推荐

  1. python3 之configparser 模块

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近[db]db_count = 31 = passwd2 = dat ...

  2. Java学习笔记【十一、序列化】

    序列化的条件 实现Serializable接口 所有属性必须是可序列化的,或标记为transient(不做序列化) 序列化-将对象输出为序列化文件 ObjectOutputStream 反序列化-将序 ...

  3. 关于api接口以及页面数据通信域名,缓存cdn设置优化

    以B站为例: 主域名:www.bilibili.com 账户登录注册相关域名,包括app登录:passport.bilibili.com 静态资源css,js,img等相关域名:static.bili ...

  4. 1、Bash Shell

    一.什么是Bash shell BashShell是一个命令解释器,它在操作系统的最外层,负责用户程序与内核进行交互操作的一种接口,将用户输入的命令翻译给操作系统,并将处理后的结果输出至屏幕. 当我们 ...

  5. python3之POST请求URL

    方法一:使用requests模块 import requests as rq import json def funcpost(): url = 'http://www.***.com/' # 需要请 ...

  6. git 学习使用记录

    一.一个小时学会git:https://www.cnblogs.com/best/p/7474442.html 二.fetch fatal: Refusing to fetch into curren ...

  7. 浅谈浏览器存储(cookie、localStorage、sessionStorage)

    今天我们从前端的角度了解一下浏览器存储,我们常见且常用的存储方式主要由两种:cookie.webStorage(localStorage和sessionStorage).下面我们来一一认识它们. Co ...

  8. mybatis整合Spring(一)

    DAO层:数据访问层 对远程存储系统做操作的一些程序统一放在DAO层 DAO层工作演变为:接口设计+SQL编写 DAO拼接等逻辑在Service层完成

  9. 《Python基础教程》第五章:条件、循环和其他语句

    在Python中赋值运算和比较运算是可以连接的,运算符可以连在一起使用,如:0<age<100 ==运算符判定两个对象是否相等,is判定两者是否等同(同一个对象) 断言,在错误条件出现时直 ...

  10. kettle案例实现

    案例一.把stu1的数据按id同步到stu2,stu2有相同id则更新数据 在kettle中新建转换 点击左上角文件—新建—转换到核心对象界面,点击输入,找到表输入拖拽到中间 双击表输入,在数据库连接 ...