zoukankan      html  css  js  c++  java
  • 常用的分页 上海

    1)NOT IN

    复制代码
    declare @timediff datetime
    declare @pageIndex int
    declare @pageSize int
    declare @sql varchar(500)
    set @pageIndex=1
    set @pageSize=10
    set @timediff=GetDATE()
    set @sql='select top ('+cast(@pageSize as varchar)+') * from testTable where (id not in (
    select top '+cast(@pageSize*(@pageIndex-1) as varchar)+' id from testTable order by id)) order by id'
    exec(@sql)
    select datediff(ms,@timediff,Getdate())
    复制代码

    @pageIndex=1时,运行:0ms(给力啊

    @pageIndex=50000时,运行:346ms(怎么50000页就不给力了)

    @pageIndex=100000时,运行:326ms(怎么比50000页时还少了?)

    2)MAX()

    复制代码
    declare @timediff datetime
    declare @pageIndex int
    declare @pageSize int
    declare @sql varchar(500)
    set @timediff=GetDATE()
    set @pageIndex=1
    set @pageSize=10
    set @sql='select top ('+cast(@pageSize as varchar)+') * from testTable where (id >= (select MAX(id) from (select top '+cast((@pageSize*(@pageIndex-1)+1) as varchar)+' id from testTable order by id) as a)) order by id'
    exec(@sql)
    select datediff(ms,@timediff,Getdate())
    复制代码

    @pageIndex=1时,运行:0ms(也是很给力啊

    @pageIndex=50000时,运行:123ms(不错)

    @pageIndex=100000时,运行:220ms(页数和查询时间成正比)

    3)Row_Number()

    复制代码
    declare @timediff datetime
    declare @pageIndex int
    declare @pageSize int
    declare @sql varchar(500)
    set @timediff=GetDATE()
    set @pageIndex=1
    set @pageSize=10
    set @sql='select * from (select *,row_number() over (order by id asc) as RowIndex from testTable) as IDWithRowNumber where RowIndex between '+cast(((@pageIndex-1)*@pageSize)+1 as varchar)+' and '+cast(@pageIndex*@pageSize as varchar)+''
    exec(@sql)
    select datediff(ms,@timediff,getdate())
    复制代码

    @pageIndex=1时,运行:0ms(好吧……数据量小的时候都是这尿性)

    @pageIndex=50000时,运行:280ms(略逊色)

    @pageIndex=100000时,运行:580ms(这货居然也是页数和查询时间成正比!坑爹吧!)

    4)临时表

    复制代码
    declare @timediff datetime
    declare @pageIndex int
    declare @pageSize int
    declare @sql varchar(500)
    declare @str varchar(500)
    set @timediff=GetDATE()
    set @pageIndex=1
    set @pageSize=10
    set @str='with tempTable as (select ceiling((Row_number() over (order by id asc))/'+cast(@pageSize as varchar)+') as page_num,* from testTable)'
    set @sql=@str+'select * from tempTable where page_num='+cast(@pageIndex-1 as varchar)+''
    exec(@sql)
    select datediff(ms,@timediff,getdate())
    复制代码

    @pageIndex=1时,运行:280ms(不咧个是吧!这非主流啊)

    @pageIndex=50000时,运行:280ms(这不科学……)

    @pageIndex=100000时,运行:280ms(好吧,这货不受页数的影响,永远都这速度)

    5)中间变量

    复制代码
    declare @timediff datetime
    declare @pageIndex int
    declare @pageSize int
    declare @count int
    declare @id int
    declare @sql varchar(500)
    set @pageIndex=1
    set @pageSize=10
    select @id=0,@count=0,@timediff=GetDATE()
    select @count=@count+1,@id=case when @count=(@pageIndex-1)*@pageSize then id else @id end from testTable order by id
    set @sql='select top '+cast(@pageSize as varchar)+' * from testTable where id>'+cast(@id as varchar)+''
    exec(@sql)
    select datediff(ms,@timediff,getdate())
    复制代码
  • 相关阅读:
    Socket Client & Server
    2台整机,多线程运行测试时间不稳定
    去了一趟宝马铁西工厂,才知道工厂已经在数字孪生
    窗帘电机测试系统调试心得
    AntDesignBlazor 学习笔记
    简单粗暴的实现 Blazor Server 登录鉴权
    Blazor 子组件与父组件通过 ChildEvents 传递数据的方法
    EasyExcel 列 固定下拉选项的做法
    Java代码在数据库存取某些敏感字段的加解密做法
    nacos 使用【一】 创建一个属于自己的配置空间
  • 原文地址:https://www.cnblogs.com/luozhai714/p/2874606.html
Copyright © 2011-2022 走看看