zoukankan      html  css  js  c++  java
  • Sql Server 常见的几种分页方式

    ⒈offset fetch next方式【SqlServer2012及以上版本支持】【推荐】

    1 select * from T_User
    2 order by id
    3 offset 5 rows            /*(页数-1) * 条数 */    
    4 fetch next 5 rows only    /* 条数 */

    ⒉row_number() over()方式【SqlServer2005以上版本支持】

    1 select * from 
    2 (select *,row_number() over(order by id) as orderId from T_User) as t
    3 where t.orderId between 11 and 15
    4 /* (页数-1)* 条数 + 1 */
    5 /* 页数 * 条数 */

     ⒊top not in方式【适用于SqlServer2012以下版本】

    1 select top 5 * from T_User
    2 where id not in (select top 10 id from T_User)
    3 
    4 /* top 条数 */
    5 /* top 条数 * 页数 */

     ⒋max(主键)方式【本质上还是top方式,适用于SqlServer2012以下版本】

    1 select top 5 * from T_User where id>=
    2 (select max(id) from (select top 6 id from T_User order by  id asc) a) 
    3 order by id;
    4 /*top 条数*/
    5 /*top(页数-1)* 条数 + 1*/

    分析:在数据量较大时

      top not in方式:查询靠前的数据速度较快

      ROW_NUMBER() OVER()方式:查询靠后的数据速度比上一种较快

      offset fetch next方式:速度稳定,优于前2种,但sql版本限制2012及以上才可使用

  • 相关阅读:
    【团队分享之二】IT团队绩效提升的一些见解
    我的ef连接mysql之旅
    Python3.5-20190501-廖老师的
    新装ubantu 18.04(自用)
    nginx配置url重写
    docker中crontab无法执行
    bootstrap
    mysql set
    mysqldump导出数据
    XGBoost 学习调参的例子
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10901646.html
Copyright © 2011-2022 走看看