转自 http://topic.youkuaiyun.com/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