SQL Server几种分页方式
2010-07-28 09:56
316 查看
转自 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<
aedb
/span> 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
--插入测试数据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<
aedb
/span> 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
相关文章推荐
- SQL Server分页查询的几种方式
- sql server 分页几种方式
- sql server 中的几种join连接方式
- 常用的几种 SQLServer 分页查询方式实现
- 利用Sql Server的top分页查询的通用构造方式
- [转]SQL Server几种分页方法实际测试
- sql server,mysql,oracle 三中数据库分页方式,很有必要的。
- SQL分页查询的几种方式
- [置顶] SSH hibernate 分页的几种实现方式
- SQL 分页查询的几种方式
- SQL Server常用的几种启动方式
- 几种分页方式分析.
- Sql Server 三种分页方式及测试
- SQL SERVER 的几种安装方式
- 服务器端几种分页方式的性能分析
- SQL分页的几种方式
- 几种常见SQL分页方式效率比较
- SQL Server 分页编号的另一种方式【推荐】
- SQL server分页方式
- 分页的几种方式(逻辑分页和物理分页)