zoukankan      html  css  js  c++  java
  • 数据库优化之:SQL语句优化之二

    1 分页存储过程

    CREATE procedure pagination1

    (@pagesize int,  --页面大小,如每页存储20条记录

    @pageindex int   --当前页码

    )

    as

    set nocount on

    begin

    declare @indextable table(id int identity(1,1),nid int)  --定义表变量

    declare @PageLowerBound int  --定义此页的底码

    declare @PageUpperBound int  --定义此页的顶码

    set @PageLowerBound=(@pageindex-1)*@pagesize

    set @PageUpperBound=@PageLowerBound+@pagesize

    set rowcount @PageUpperBound

    insert into @indextable(nid) select gid from TGongwen where fariqi >dateadd(day,-365,getdate()) order by fariqi desc

    select O.gid,O.mid,O.title,O.fadanwei,O.fariqi from TGongwen O,@indextable t where O.gid=t.nid

    and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id

    end

    set nocount off

    2 取出第n-m条的记录(m>n)

    一般这样实现:

    SELECT TOP m-n+1 *
    FROM publish
    WHERE (id NOT IN
        (SELECT TOP n-1 id
         FROM publish))

    id 为publish 表的关键字

    3 分页语句(推荐使用)

    select top PageSize *

    from table1

    where id>

          (select max (id) from

          (select top ((PageIndex-1)*PageSize ) id from table1 order by id) as T

           )    

      order by id

  • 相关阅读:
    Explain用法
    轻量快速的 Python ASGI 框架 uvicorn
    conda常用命令
    ubuntu 安装并配置zsh
    ubuntu安装zsh终端
    /etc/profile、/etc/bashrc、.bash_profile、.bashrc
    python用List的内建函数list.sort进行排序
    python对象排序
    修改python接口返回给前端的格式封装
    linux设置uwsgi开机自启
  • 原文地址:https://www.cnblogs.com/zjwei55/p/2135568.html
Copyright © 2011-2022 走看看