SQL Server分页的存储过程写法以及性能比较
------创建数据库data_Test -----create database data_TestGOuse data_TestGOcreate 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 ondeclare @count intset @count=1while @count<=2000000begininsert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn')set @count=@count+1endset 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 --每页记录数)asbeginset 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后不支技直接接参数,所以写成了字符串@sqlselect 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 --页记录数)asbeginset nocount on;declare @timediff datetimedeclare @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)asdeclare @count intdeclare @ID intdeclare @timediff datetimedeclare @sql nvarchar(500)beginset 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 idset @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)asdeclare @timediff datetimebeginset 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_numbercreate procedure proc_CTE --利用临时表及Row_number(@pageIndex int, --页索引@pageSize int --页记录数)asset nocount on;declare @ctestr nvarchar(400)declare @strSql nvarchar(400)declare @datediff datetimebeginselect @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)endbeginexecute sp_executesql @strSqlselect datediff(ms,@datediff,GetDate())set nocount off;end |
存储过程的5种分页写法,下面的代码是从忘了什么时候从别人那Ctrl+C来的,所以仅仅作为收藏,希望作者看到不要喷我.
SQL Server分页的存储过程写法以及性能比较的更多相关文章
- SQL Server分页查询存储过程
--分页存储过程create PROCEDURE [dbo].[commonPagination]@columns varchar(500), --要显示的列名,用逗号隔开 @tableName va ...
- 理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程
本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(1)--简介 本文介绍SQL Server如何编译存储过程并使用计划缓存 ...
- SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总
SQL Server游标 转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...
- SQL server分页的四种方法
SQL server分页的四种方法 1.三重循环: 2.利用max(主键); 3.利用row_number关键字: 4.offset/fetch next关键字 方法一:三重循环思路 先取前20页, ...
- SQL server分页的四种方法(算很全面了)
这篇博客讲的是SQL server的分页方法,用的SQL server 2012版本.下面都用pageIndex表示页数,pageSize表示一页包含的记录.并且下面涉及到具体例子的,设定查询第2 ...
- SQL Server基础之存储过程
简单来说,存储过程就是一条或者多条sql语句的集合,可视为批处理文件,但是其作用不仅限于批处理.本篇主要介绍变量的使用,存储过程和存储函数的创建,调用,查看,修改以及删除操作. 一:存储过程概述 ...
- SQL SERVER 临时表导致存储过程重编译(recompile)的一些探讨
SQLSERVER为了确保返回正确的值,或者处于性能上的顾虑,有意不重用缓存在内存里的执行计划,而重新编译执行计划的这种行为,被称为重编译(recompile).那么引发存储过程重编译的条件有哪一些呢 ...
- 【SQL Server】SQL Server基础之存储过程
SQL Server基础之存储过程 阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...
- SQL SERVER 分页方法
最近项目中需要在SQL SERVER中进行分页,需要编写分页查询语句.之前也写过一些关于分页查询的语句,但是性能不敢恭维.于是在业务时间,在微软社区Bing了一篇老外写的关于SQL SERVER分页的 ...
随机推荐
- python之判断和循环
计算机之所以能做很多自动化的任务,因为它可以自己做条件判断.比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = : print ('your age i ...
- c++11 移动语义move semantics
performance, expensive object copies move semantics, temporary objects implemented with rvalue refer ...
- 蓝牙App漏洞系列分析之一CVE-2017-0601
蓝牙App漏洞系列分析之一CVE-2017-0601 0x01 概要 2017年5月的 Android 安全公告修复了我们提交的一个蓝牙提权中危漏洞,这个漏洞尽管简单,但比较有意思,能够使本地恶意 A ...
- 2.Java NIO 简介
概述 Java NIO 是 JDK 1.4 发布的一套全新的IO API(New IO 简称 NIO),由于 JDK 1.7 对 NIO 的更新,目前 NIO 被广泛应用,以至于 将 JDK 1.7 ...
- 3.Https服务器的配置
1.前言: 所谓区块链,简而言之就是一种数据结构,每一个区块都像账本的每一页纸记录了该网络上的交易信息,而众多区块在时间的基础上按照顺序连接起 来就形成了区块链.区块链能够以数字方式识别和跟踪交易,并 ...
- c++第三次作业:类的友元
C++第三次作业:类的友元 1.友元的关系提供了不同类或对象的成员函数之间.类的成员函数与一般函数之间进行数据共享的机制.通俗的说友元关系就是一个类主动声明其他函数是他的朋友,可以使其获得特殊访问权利 ...
- 19.8.28 flutter学习笔记
1:字符串的操作 length打印字符串的长度.“isEmpty”判断字符串是否为空. “contains()是否包含某个字符串”.”substring(startIndex,endIndex)截取一 ...
- 201871010101-陈来弟《面向对象程序设计(java)》第十七周学习总结
实验十七 线程同步控制 实验时间 2018-12-10 第一部分:理论知识 1.多线程并发执行中的问题 ◆多个线程相对执行的顺序是不确定的. ◆线程执行顺序的不确定性会产生执行结果的不确定性. ◆在 ...
- [Ahoi2009]self 同类分布
1799: [Ahoi2009]self 同类分布 Time Limit: 50 Sec Memory Limit: 64 MBSubmit: 2357 Solved: 1079[Submit][ ...
- luogu4366 [Code+#4]最短路[优化建边最短路]
显然这里的$n^2$级别的边数不能全建出来,于是盯住xor这个关键点去 瞎猜 探究有没有什么特殊性质可以使得一些边没有必要建出来. 发现一个点经过一次xor $x$,花费$x$这么多代价(先不看$C$ ...