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

简介:   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在实际情况中,要具体分析。
目录
相关文章
|
8月前
|
存储 SQL 数据库连接
C#程序调用Sql Server存储过程异常处理:调用存储过程后不返回、不抛异常的解决方案
本文分析了C#程序操作Sql Server数据库时偶发的不返回、不抛异常问题,并提出了解决思路。首先解析了一个执行存储过程的函数`ExecuteProcedure`,其功能是调用存储过程并返回影响行数。针对代码执行被阻塞但无异常的情况,文章总结了可能原因,如死锁、无限循环或网络问题等。随后提供了多种解决方案:1) 增加日志定位问题;2) 使用异步操作提升响应性;3) 设置超时机制避免阻塞;4) 利用线程池分离主线程;5) 通过信号量同步线程;6) 监控数据库连接状态确保可用性。这些方法可有效应对数据库操作中的潜在问题,保障程序稳定性。
654 11
|
11月前
|
SQL 关系型数据库 MySQL
基于SQL Server / MySQL进行百万条数据过滤优化方案
对百万级别数据进行高效过滤查询,需要综合使用索引、查询优化、表分区、统计信息和视图等技术手段。通过合理的数据库设计和查询优化,可以显著提升查询性能,确保系统的高效稳定运行。
575 9
|
11月前
|
SQL 存储 关系型数据库
【SQL技术】不同数据库引擎 SQL 优化方案剖析
不同数据库系统(MySQL、PostgreSQL、Doris、Hive)的SQL优化策略。存储引擎特点、SQL执行流程及常见操作(如条件查询、排序、聚合函数)的优化方法。针对各数据库,索引使用、分区裁剪、谓词下推等技术,并提供了具体的SQL示例。通用的SQL调优技巧,如避免使用`COUNT(DISTINCT)`、减少小文件问题、慎重使用`SELECT *`等。通过合理选择和应用这些优化策略,可以显著提升数据库查询性能和系统稳定性。
529 9
|
SQL 存储 PHP
解决高版本laravel/framework中SQLServer2008分页报错问题
【11月更文挑战第15天】在高版本的Laravel框架中,使用SQLServer 2008数据库进行分页操作时可能会遇到兼容性问题,导致报错。本文提供了两种解决方案:一是升级数据库版本至2012或更高,以提高对复杂查询的支持;二是通过自定义分页查询构建器,手动调整分页逻辑,使其适应SQLServer 2008的特性。具体实施步骤包括备份数据、安装新数据库版本、恢复数据,或创建自定义分页查询类并在模型中使用。这些方法能有效解决分页报错问题。
176 3
|
SQL PHP 数据库
解决高版本laravel/framework中SQLServer2008分页报错问题
【11月更文挑战第6天】在高版本的 `laravel/framework` 中使用 SQL Server 2008 进行数据库操作时,可能会出现分页报错。这是由于 `laravel` 的分页机制与 SQL Server 2008 的某些特性不兼容所致。解决方法包括:1. 升级数据库版本;2. 自定义分页查询语句;3. 使用兼容包或插件;4. 修改 `laravel` 的分页逻辑。
157 3
|
存储 SQL 缓存
SQL Server存储过程的优缺点
【10月更文挑战第22天】存储过程具有代码复用性高、性能优化、增强数据安全性、提高可维护性和减少网络流量等优点,但也存在调试困难、移植性差、增加数据库服务器负载和版本控制复杂等缺点。
545 1
|
存储 SQL 数据库
Sql Server 存储过程怎么找 存储过程内容
Sql Server 存储过程怎么找 存储过程内容
751 1
|
11月前
|
存储 Java 关系型数据库
java调用mysql存储过程
在 Java 中调用 MySQL 存储过程主要借助 JDBC(Java Database Connectivity)。其核心原理是通过 JDBC 与 MySQL 建立连接,调用存储过程并处理结果。具体步骤包括:加载 JDBC 驱动、建立数据库连接、创建 CallableStatement 对象、设置存储过程参数并执行调用。此过程实现了 Java 程序与 MySQL 数据库的高效交互。
|
10月前
|
存储 关系型数据库 MySQL
【YashanDB知识库】MySQL返回结果集的存储过程的改写方法
本文介绍了将MySQL存储过程改写至YashanDB的解决方案。由于MySQL存储过程可直接返回结果集,而YashanDB需通过返回SYS_REF_CURSOR的函数实现类似功能,因此需要对代码进行转换。示例中展示了如何将MySQL存储过程`proc1`改写为YashanDB函数,并调整JDBC应用代码以适配REF_CURSOR输出参数,从而正确获取查询结果。此方法确保了跨数据库场景下的兼容性与功能性。
|
存储 SQL NoSQL