zoukankan      html  css  js  c++  java
  • SQL Server 分页

     1 WITH Results AS (
     2     SELECT *,ROW_NUMBER () OVER (ORDER BY a.CreateDate DESC) RowIndex
     3     FROM (
     4     (SELECT * FROM Remark_User_1 WITH (NOLOCK))
     5     UNION ALL
     6     (SELECT * FROM Remark_SYSLog_1 WITH (NOLOCK))
     7     UNION ALL
     8     (SELECT * FROM Remark_SYSLog_2 WITH (NOLOCK))
     9     UNION ALL
    10     (SELECT * FROM Remark_User_2 WITH (NOLOCK))
    11     UNION ALL
    12     (SELECT * FROM Remark_SYSLog_3 WITH (NOLOCK))
    13     UNION ALL
    14     (SELECT * FROM Remark_User_3 WITH (NOLOCK))
    15     UNION ALL
    16     (SELECT * FROM Remark_SYSLog_0 WITH (NOLOCK))
    17     UNION ALL
    18     (SELECT * FROM Remark_User_0 WITH (NOLOCK))
    19     ) AS a
    20 ),
    21 ResultsCount AS (
    22     SELECT isnull(MAX(RowIndex), 0) ResultCount FROM Results
    23 )
    24 SELECT a.*, ResultsCount.ResultCount,rd.Name AS TypeName
    25 FROM Results a
    26 LEFT JOIN ResultsCount ON 1 = 1
    27 LEFT JOIN RemarkDataDictionary rd ON rd.ID = a.Type
    28 WHERE RowIndex BETWEEN 1 AND 20

    优化点:

    1、避免使用*

    2、主键、order by 后面的字段添加索引

    3、封装为存储过程,消除sql编译的时间

    4、先where后row_number

  • 相关阅读:
    如何安装mysql
    07 登录接口开发
    06 跨域问题
    05 实体校验
    04 异常处理
    03 整合shiro+jwt 会话共享
    02 统一结果封装
    01 新建SpringBoot项目 整合Mybatis Plus(Spring Boot 前后端分离)
    结合Scikit-learn介绍几种常用的特征选择方法
    Set_ML
  • 原文地址:https://www.cnblogs.com/BurtBlog/p/6293695.html
Copyright © 2011-2022 走看看