zoukankan      html  css  js  c++  java
  • SQLServer常用分页方式

      mysql的分页是基于limit关键字,oracle的分页是基于rownum行号,SQLserver的分页在下面进行研究,是基于SQLServer2012进行的测试。

    0.原来的SQL的所有数据

    下面的测试假设每页都是取5条数据。

    1.第一种-ROW_NUMBER() OVER()方式(over函数必须有)

     (1)取第一页数据

      select * from ( 
        select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user] 
      ) as b
    
          where RowId between 1 and 5;

     结果:

    (2)取第二页数据

      select * from ( 
        select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user] 
      ) as b
    
          where RowId between 6 and 10;

    结果:

     总结:  这种方式采用    RowId BETWEEN 当前页数-1*页大小+1  and 页数*页大小   ,而且包含起始值与结束值。

    补充:这种方式的通用写法如下:   原来SQL不能带order by ,但是可以带条件。

    原来SQL =     select * from [mydb].[dbo].[user] where name like 'name%'    

    拼接分页的模板如下: 

     select * from ( 
        select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from    (
                原来SQL
            ) AS A
    ) as B
    where RowId between 1 and 5;

    2.第二种-offset start fetch next page rows only

    (1)取第一页

    select * from [mydb].[dbo].[user]   order by ID offset 0 rows fetch next 5 rows only;

    结果:

     

    (2)取第二页

    select * from [mydb].[dbo].[user]   order by ID offset 5 rows fetch next 5 rows only;

    结果:

     总结:这种方式的起始值与结束值计算方式: offset 页号*页大小 rows fetch next 页大小 rows only  

    3.第三种: top 关键字

     (1)取第一页

    select top 5 * from [mydb].[dbo].[user] 
    where ID not in (select top 0 ID from [mydb].[dbo].[user]);

    结果:

    (2)取第二页

    select top 5 * from [mydb].[dbo].[user] 
    where ID not in (select top 5 ID from [mydb].[dbo].[user]);

    结果:

      总结:这种方式只用改内层的 top就可以了:  内层的top后面相当于起始值,计算方式为  (页号-1)*页大小。

      补充:这种分页方式的通用模板如下:  这个可以加order by和条件

    原来SQL = select * from [mydb].[dbo].[user] where name like 'name%'   

    select top 5 * from ( 
        原来SQL
    ) AS A where ID not in (select top 5 ID from [mydb].[dbo].[user]);

    4.  ROW_NUMBER() + top 相当于上面1和3的结合使用

     (1)取第一页

    select top (5) * from (select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]) as A where A.RowId>0;

    结果:

    (2)取第二页

    select top (5) * from (select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]) as A where A.RowId>5;

    结果:

      总结:这种方式比较通用, 第一个 top 里面的值 相当于 页大小,第二个rowID>起始值,起始值计算方式为  (页号-1)*页大小

    补充:这种分页方式的通用模板如下:    这种方式原来的SQL也不用加排序语句

    原来SQL = select * from [mydb].[dbo].[user] where name like 'name%'   

    select top (5) * from (
        select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from (
            原来SQL
            ) as A   
    ) as B where B.RowId>5;

    注意:文中SQLServer的AS A这些起别名不能省略。

  • 相关阅读:
    jMeter 里 CSV Data Set Config Sharing Mode 的含义详解
    如何使用 jMeter Parallel Controller
    使用 Chrome 开发者工具 coverage 功能分析 web 应用的渲染阻止资源的执行分布情况
    使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题
    关于 SAP 电商云首页加载时触发的 OCC API 请求
    SAP UI5 确保控件 id 全局唯一的实现方法
    SAP 电商云 Accelerator 和 Spartacus UI 的工作机制差异
    介绍一个好用的能让网页变成黑色背景的护眼 Chrome 扩展应用
    Chrome 开发者工具 performance 标签页的用法
    Client Side Cache 和 Server Side Cache 的区别
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/10305188.html
Copyright © 2011-2022 走看看