SQL Server几种分页方式

转自 http://topic.csdn.net/u/20100617/04/80d1bd99-2e1c-4083-ad87-72bf706cb536.html?15304

 

--插入测试数据200w条,可能会很久
create table Student(
  Id
int PRIMARY KEY identity(1,1),
  Name
nvarchar(50),
  Age
int
)
insert Student(Name,Age)values('Name',18)
while (select count(*) from Student)<2000000
 
insert Student select Name,Age from Student

 

--开始测试查询
declare @now datetime
--max方案
select 'max'方案
select @now=getdate()
--begin
select top 10 * from Studentwhere Id>(
 
select max(Id)
 
from (
   
select top 1999990 Id from Student order by Id)tt)
--end
declare @maxDiff int
select @maxDiff=datediff(ms,@now,getdate())

--top方案
select 'top'方案
select @now=getdate()
--begin
select top 10 * from Student
where Id not in(select top  1999990 Id from Student) --end
declare @topDiff int
select @topDiff=datediff(ms,@now,getdate())

--row方案
select 'row'方案
select @now=getdate()
--begin
select *
from (
select row_number()over(order by tc)rn,*
from (select top 2000000 0 tc,* from Student)t
)tt
where rn>1999990
--end
declare @rowDiff int
select @rowDiff=datediff(ms,@now,getdate())

--row_number方案
select 'row_number'方案
select @now=getdate()
--begin
select *
from(
select top 2000000 row_number()over(order by Id)rn,* from Student
)t
where rn>1999990
--end
declare @row_numberDiff int
select @row_numberDiff=datediff(ms,@now,getdate())

--记录结果
select '第20万页'页码,@maxDiff max方案,@topDiff top方案,@rowDiff row方案,@row_numberDiff row_number方案

 

 

比较了3种分页方式,分别是max方案,top方案,row方案

效率:
  第1:row
  第2:max
  第3:top

缺点:
  max:必须用户编写复杂Sql,不支持非唯一列排序
  top:必须用户编写复杂Sql,不支持复合主键
  row:不支持sqlServer2000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值