几种常见SQL分页方式效率比较几种常见SQL分页方式效率比较
1.创建测试环境,(插入100万条数据大概耗时5分钟)。
create database DBTest
use DBTest
--创建测试表
create table pagetest
(
id int identity(1,1) not null,
col01 int null,
col02 nvarchar(50) null,
col03 datetime null
)
--1万记录集
declare @i int
set @i=0
while(&#64;i<10000)
begin
insert into pagetest select cast(floor(rand()*10000) as int),left(newid(),10),getdate()
set &#64;i&#61;&#64;i&#43;1
end
2.几种典型的分页sql&#xff0c;下面例子是每页50条&#xff0c;198*50&#61;9900&#xff0c;取第199页数据。
--写法1&#xff0c;not in/top
select top 50 * from pagetest
where id not in (select top 9900 id from pagetest order by id)
order by id
--写法2&#xff0c;not exists
select top 50 * from pagetest
where not exists
(select 1 from (select top 9900 id from pagetest order by id)a where a.id&#61;pagetest.id)
order by id
--写法3&#xff0c;max/top
select top 50 * from pagetest
where id>(select max(id) from (select top 9900 id from pagetest order by id)a)
order by id
--写法4&#xff0c;row_number()
select top 50 * from
(select row_number()over(order by id)rownumber,* from pagetest)a
where rownumber>9900
select * from
(select row_number()over(order by id)rownumber,* from pagetest)a
where rownumber>9900 and rownumber<9951
select * from
(select row_number()over(order by id)rownumber,* from pagetest)a
where rownumber between 9901 and 9950
--写法5&#xff0c;在csdn上一帖子看到的&#xff0c;row_number() 变体&#xff0c;不基于已有字段产生记录序号&#xff0c;先按条件筛选以及排好序&#xff0c;再在结果集上给一常量列用于产生记录序号
select *
from (
select row_number()over(order by tempColumn)rownumber,*
from (select top 9950 tempColumn&#61;0,* from pagetest where 1&#61;1 order by id)a
)b
where rownumber>9900
2.分别在1万&#xff0c;10万(取1990页)&#xff0c;100(取19900页&#xff09;记录集下测试。
测试sql:
declare &#64;begin_date datetime
declare &#64;end_date datetime
select &#64;begin_date &#61; getdate()
<.....YOUR CODE.....>
select &#64;end_date &#61; getdate()
select datediff(ms,&#64;begin_date,&#64;end_date) as &#39;毫秒&#39;
1万&#xff1a;基本感觉不到差异。
10万&#xff1a;
100万&#xff1a;
结论&#xff1a;
1.max/top,ROW_NUMBER()都是比较不错的分页方法。相比ROW_NUMBER()只支持sql2005及以上版本&#xff0c;max/top有更好的可移植性&#xff0c;能同时适用于sql2000,access。
2.not exists感觉是要比not in效率高一点点。
3.ROW_NUMBER()的3种不同写法效率看起来差不多。
4.ROW_NUMBER() 的变体基于我这个测试效率实在不好。原帖在这里 http://topic.csdn.net/u/20100617/04/80d1bd99-2e1c-4083-ad87-72bf706cb536.html
PS.上面的分页排序都是基于自增字段id的。测试环境还提供了int,nvarch,datetime类型字段&#xff0c;也可以试试。不过对于非主键没索引的大数据量排序效率肯定是很坑爹的。