[转]SQL Server 存储过程的分页方案比拼

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:   1SQL Server 存储过程的分页,这个问题已经讨论过几年了,很多朋友在问我,所以在此发表一下我的观点   2建立表:   3  4CREATE TABLE [TestTable] (   5 [ID] [int] IDENTITY (1, 1) NOT NULL ,   6...
 
  1 img_a6339ee3e57d1d52bc7d02b338e15a60.gif SQL Server 存储过程的分页,这个问题已经讨论过几年了,很多朋友在问我,所以在此发表一下我的观点
  2 img_a6339ee3e57d1d52bc7d02b338e15a60.gif建立表:
  3 img_a6339ee3e57d1d52bc7d02b338e15a60.gif
  4 img_a6339ee3e57d1d52bc7d02b338e15a60.gif CREATE   TABLE   [ TestTable ]  (
  5 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  [ ID ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
  6 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  [ FirstName ]   [ nvarchar ]  ( 100 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
  7 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  [ LastName ]   [ nvarchar ]  ( 100 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
  8 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  [ Country ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
  9 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  [ Note ]   [ nvarchar ]  ( 2000 ) COLLATE Chinese_PRC_CI_AS  NULL  
 10 img_a6339ee3e57d1d52bc7d02b338e15a60.gif ON   [ PRIMARY ]
 11 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  GO
 12 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 13 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
 14 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 插入数据:(2万条,用更多的数据测试会明显一些)
 15 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  SET   IDENTITY_INSERT  TestTable  ON
 16 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 17 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  declare   @i   int
 18 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  set   @i = 1
 19 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  while   @i <= 20000
 20 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  begin
 21 img_a6339ee3e57d1d52bc7d02b338e15a60.gif      insert   into  TestTable( [ id ] , FirstName, LastName, Country,Note)  values ( @i ' FirstName_XXX ' , ' LastName_XXX ' , ' Country_XXX ' , ' Note_XXX ' )
 22 img_a6339ee3e57d1d52bc7d02b338e15a60.gif      set   @i = @i + 1
 23 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  end
 24 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 25 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  SET   IDENTITY_INSERT  TestTable  OFF
 26 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 27 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
 28 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 29 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  -- -----------------------------------
 30 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
 31 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 分页方案一:(利用Not In和SELECT TOP分页)
 32 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 语句形式:
 33 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  SELECT   TOP   10   *
 34 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  FROM  TestTable
 35 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  WHERE  (ID  NOT   IN
 36 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           ( SELECT   TOP   20  id
 37 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           FROM  TestTable
 38 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           ORDER   BY  id))
 39 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  ORDER   BY  ID
 40 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 41 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 42 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  SELECT   TOP  页大小  *
 43 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  FROM  TestTable
 44 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  WHERE  (ID  NOT   IN
 45 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           ( SELECT   TOP  页大小 * 页数 id
 46 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           FROM  表
 47 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           ORDER   BY  id))
 48 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  ORDER   BY  ID
 49 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 50 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  -- -----------------------------------
 51 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
 52 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 分页方案二:(利用ID大于多少和SELECT TOP分页)
 53 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 语句形式:
 54 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  SELECT   TOP   10   *
 55 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  FROM  TestTable
 56 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  WHERE  (ID  >
 57 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           ( SELECT   MAX (id)
 58 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           FROM  ( SELECT   TOP   20  id
 59 img_a6339ee3e57d1d52bc7d02b338e15a60.gif                   FROM  TestTable
 60 img_a6339ee3e57d1d52bc7d02b338e15a60.gif                   ORDER   BY  id)  AS  T))
 61 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  ORDER   BY  ID
 62 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 63 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 64 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  SELECT   TOP  页大小  *
 65 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  FROM  TestTable
 66 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  WHERE  (ID  >
 67 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           ( SELECT   MAX (id)
 68 img_a6339ee3e57d1d52bc7d02b338e15a60.gif           FROM  ( SELECT   TOP  页大小 * 页数 id
 69 img_a6339ee3e57d1d52bc7d02b338e15a60.gif                   FROM  表
 70 img_a6339ee3e57d1d52bc7d02b338e15a60.gif                   ORDER   BY  id)  AS  T))
 71 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  ORDER   BY  ID
 72 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 73 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 74 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  -- -----------------------------------
 75 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
 76 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 分页方案三:(利用SQL的游标存储过程分页)
 77 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  create    procedure  XiaoZhengGe
 78 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  @sqlstr   nvarchar ( 4000 ),  -- 查询字符串
 79 img_a6339ee3e57d1d52bc7d02b338e15a60.gif   @currentpage   int -- 第N页
 80 img_a6339ee3e57d1d52bc7d02b338e15a60.gif   @pagesize   int   -- 每页行数
 81 img_a6339ee3e57d1d52bc7d02b338e15a60.gif   as
 82 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  set  nocount  on
 83 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  declare   @P1   int -- P1是游标的id
 84 img_a6339ee3e57d1d52bc7d02b338e15a60.gif    @rowcount   int
 85 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  exec  sp_cursoropen  @P1  output, @sqlstr , @scrollopt = 1 , @ccopt = 1 , @rowcount = @rowcount  output
 86 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  select   ceiling ( 1.0 * @rowcount / @pagesize as  总页数 -- ,@rowcount as 总行数,@currentpage as 当前页 
 87 img_a6339ee3e57d1d52bc7d02b338e15a60.gif   set   @currentpage = ( @currentpage - 1 ) * @pagesize + 1
 88 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  exec  sp_cursorfetch  @P1 , 16 , @currentpage , @pagesize  
 89 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  exec  sp_cursorclose  @P1
 90 img_a6339ee3e57d1d52bc7d02b338e15a60.gif  set  nocount  off
 91 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 92 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
 93 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 建议优化的时候,加上主键和索引,查询效率会提高。
 94 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
 95 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 通过SQL 查询分析器,显示比较:我的结论是:
 96 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
 97 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 分页方案一:(利用Not In和SELECT TOP分页)   效率次之,需要拼接SQL语句
 98 img_a6339ee3e57d1d52bc7d02b338e15a60.gif 分页方案三:(利用SQL的游标存储过程分页)    效率最差,但是最为通用
 99 img_a6339ee3e57d1d52bc7d02b338e15a60.gif
100 img_a6339ee3e57d1d52bc7d02b338e15a60.gif在实际情况中,要具体分析。
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
4月前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第18天】SQL Server 存储过程具有提高性能、增强安全性、代码复用和易于维护等优点。它可以减少编译时间和网络传输开销,通过权限控制和参数验证提升安全性,支持代码共享和复用,并且便于维护和版本管理。然而,存储过程也存在可移植性差、开发和调试复杂、版本管理问题、性能调优困难和依赖数据库服务器等缺点。使用时需根据具体需求权衡利弊。
110 1
|
3月前
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
|
3月前
|
SQL 存储 PHP
解决高版本laravel/framework中SQLServer2008分页报错问题
【11月更文挑战第15天】在高版本的Laravel框架中,使用SQLServer 2008数据库进行分页操作时可能会遇到兼容性问题,导致报错。本文提供了两种解决方案:一是升级数据库版本至2012或更高,以提高对复杂查询的支持;二是通过自定义分页查询构建器,手动调整分页逻辑,使其适应SQLServer 2008的特性。具体实施步骤包括备份数据、安装新数据库版本、恢复数据,或创建自定义分页查询类并在模型中使用。这些方法能有效解决分页报错问题。
|
3月前
|
SQL PHP 数据库
解决高版本laravel/framework中SQLServer2008分页报错问题
【11月更文挑战第6天】在高版本的 `laravel/framework` 中使用 SQL Server 2008 进行数据库操作时,可能会出现分页报错。这是由于 `laravel` 的分页机制与 SQL Server 2008 的某些特性不兼容所致。解决方法包括:1. 升级数据库版本;2. 自定义分页查询语句;3. 使用兼容包或插件;4. 修改 `laravel` 的分页逻辑。
|
4月前
|
存储 SQL 缓存
SQL Server存储过程的优缺点
【10月更文挑战第22天】存储过程具有代码复用性高、性能优化、增强数据安全性、提高可维护性和减少网络流量等优点,但也存在调试困难、移植性差、增加数据库服务器负载和版本控制复杂等缺点。
230 1
|
4月前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第17天】SQL Server 存储过程是预编译的 SQL 语句集,存于数据库中,可重复调用。它能提高性能、增强安全性和可维护性,但也有可移植性差、开发调试复杂及可能影响数据库性能等缺点。使用时需权衡利弊。
102 3
|
4月前
|
存储 SQL 数据库
Sql Server 存储过程怎么找 存储过程内容
Sql Server 存储过程怎么找 存储过程内容
272 1
|
5月前
|
SQL 数据库
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
SQL Server附加数据库出现错误823,附加数据库失败。数据库没有备份,无法通过备份恢复数据库。 SQL Server数据库出现823错误的可能原因有:数据库物理页面损坏、数据库物理页面校验值损坏导致无法识别该页面、断电或者文件系统问题导致页面丢失。
139 12
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
|
1月前
|
数据库 Windows
SqlServer数据恢复—SqlServer数据库所在分区损坏的数据恢复案例
一块硬盘上存放的SqlServer数据库,windows server操作系统+NTFS文件系统。由于误操作导致分区损坏,需要恢复硬盘里的SqlServer数据库数据。
|
3月前
|
SQL 存储 Linux
从配置源到数据库初始化一步步教你在CentOS 7.9上安装SQL Server 2019
【11月更文挑战第16天】本文介绍了在 CentOS 7.9 上安装 SQL Server 2019 的详细步骤,包括配置系统源、安装 SQL Server 2019 软件包以及数据库初始化,确保 SQL Server 正常运行。
165 4

热门文章

最新文章