目录SQLServer分页方式1.Offset and Fetch 方式分页2.row_number() 方式分页3.row_number()变种4.not in/top方式分页5.not exists方式分页6.max/top方式分页
附带50万数据分页时间[本机访问|已重启SQL服务|无其他程序干扰][非索引排序]
环境 WIN7 SQL服务12.04 SQL管理器SSMS14.01
[SqlServer2012+] OFFSET 起始值 ROWS FETCH NEXT 每页条数 ROWS ONLY
1.分页条件不能重复,建议使用 分页条件+主键 一起作为 分页参数
2.分页语句中 FIRST=NEXT ROWS=ROW
3.不使用排序可以用
使用 OFFSET FETCH 方式分页必须带order by key 其中key 必须是唯一值 不然会导致错误
select * from (
select * from sysrizhi
) table_temp order by riqi desc,sysrizhiid desc offset 10 rows fetch next 10 rows only
select * from sysrizhi
) table_temp order by riqi desc,sysrizhiid desc offset 10 rows fetch next 10 rows only
50万行执行时间 770毫秒 索引排序10毫秒
select * from tcpliushui order by riqi OFFSET 100 ROWS FETCH NEXT 20 ROWS ONLY
[SqlServer2005+] row_number()over(order by 已有排序字段)rownumber 然后根据rownumber筛选
使用方式
select * from (select *, ROW_NUMBER() Over( order by riqi desc,sysrizhiid desc ) as rowNum from (
select * from sysrizhi
) as table_1 ) as t where t.rowNum>10 and t.rowNum<=20
select * from sysrizhi
) as table_1 ) as t where t.rowNum>10 and t.rowNum<=20
速度测试 50万行执行时间 774毫秒 索引排序10毫秒
select * from
(select row_number()over(order by riqi)rownumber,* from tcpliushui )a
where rownumber>100 and rownumber<121
(select row_number()over(order by riqi)rownumber,* from tcpliushui )a
where rownumber>100 and rownumber<121
速度测试 50万行执行时间 777毫秒 索引排序13毫秒
select top 20 * from
(select row_number()over(order by riqi)rownumber,* from tcpliushui)a
where rownumber>100
(select row_number()over(order by riqi)rownumber,* from tcpliushui)a
where rownumber>100
速度测试 50万行执行时间 775毫秒 索引排序15毫秒
select * from
(select row_number()over(order by riqi)rownumber,* from tcpliushui)a
where rownumber between 101 and 120
(select row_number()over(order by riqi)rownumber,* from tcpliushui)a
where rownumber between 101 and 120
不基于已有字段 [SqlServer2005+]
速度测试 50万行执行时间 779毫秒 索引排序15毫秒
select *
from (
select row_number()over(order by tempColumn)rownumber,*
from (select top 120 tempColumn=0,* from tcpliushui where 1=1 order by riqi)a
)b
where rownumber>100
from (
select row_number()over(order by tempColumn)rownumber,*
from (select top 120 tempColumn=0,* from tcpliushui where 1=1 order by riqi)a
)b
where rownumber>100
速度测试 50万行执行时间 858毫秒 索引排序51毫秒
select top 20 * from tcpliushui
where tcpliushuiid not in (select top 100 tcpliushuiid from tcpliushui order by riqi)
order by riqi
where tcpliushuiid not in (select top 100 tcpliushuiid from tcpliushui order by riqi)
order by riqi
速度测试 50万行执行时间 852毫秒 索引排序22毫秒
select top 20 * from tcpliushui
where not exists
(select 1 from (select top 100 tcpliushuiid from tcpliushui order by riqi)a where a.tcpliushuiid=tcpliushui.tcpliushuiid)
order by riqi
where not exists
(select 1 from (select top 100 tcpliushuiid from tcpliushui order by riqi)a where a.tcpliushuiid=tcpliushui.tcpliushuiid)
order by riqi
速度测试 50万行执行时间 819毫秒 索引排序12毫秒
select top 20 * from tcpliushui
where tcpliushuiid>(select max(tcpliushuiid) from (select top 100 tcpliushuiid from tcpliushui order by riqi)a)
order by riqi
where tcpliushuiid>(select max(tcpliushuiid) from (select top 100 tcpliushuiid from tcpliushui order by riqi)a)
order by riqi
到此这篇关于SqlServer 多种分页方式 (含简单速度测试)的文章就介绍到这了,更多相关SqlServer 分页方式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:SQL server分页的四种方法思路详解(最全面教程)浅析Mysql和Oracle分页的区别c#常用表格控件dataGridView的分页显示Spring Data JPA实现排序与分页查询超详细流程讲解MybatisPlus分页查询与多条件查询介绍及查询过程中空值问题的解决DeveloperSharp?高效分页使用详解
© 版权声明
文章版权归作者所有,未经允许请勿转载。