高效的SQLSERVER分页查询(推荐)
第一种方案、最简单、普通的方法:
select top 30 * from article where id not in(select top 45000 id from article order by year desc, id desc) order by year desc,id desc
平均查询100次所需时间:45s
第二种方案:
select * from ( select top 30 * from (select top 45030 * from article order by year desc, id desc) f order by f.year asc, f.id desc) s order by s.year desc,s.id desc
平均查询100次所需时间:138s
第三种方案:
select * from article w1,
(
select top 30 id from
(
select top 50030 id, year from article order by year desc, id desc
) w order by w.year asc, w.id asc
) w2 where w1.id = w2.id order by w1.year desc, w1.id desc
平均查询100次所需时间:21s
第四种方案:
select * from article w1
where id in
(
select top 30 id from
(
select top 45030 id, year from article order by year desc, id desc
) w order by w.year asc, w.id asc
)
order by w1.year desc, w1.id desc
平均查询100次所需时间:20s
第五种方案:
select w2.n, w1.* from article w1, ( select top 50030 row_number() over (order by year desc, id desc) n, id from article ) w2 where w1.id = w2.id and w2.n > 50000 order by w2.n asc
平均查询100次所需时间:15s
查询第1000-1030条记录
第一种方案:
select top 30 * from article where id not in(select top 1000 id from article order by year desc, id desc) order by year desc,id desc
平均查询100次所需时间:80s
第二种方案:
select * from ( select top 30 * from (select top 1030 * from article order by year desc, id desc) f order by f.year asc, f.id desc) s order by s.year desc,s.id desc
平均查询100次所需时间:30s
第三种方案:
select * from article w1,
(
select top 30 id from
(
select top 1030 id, year from article order by year desc, id desc
) w order by w.year asc, w.id asc
) w2 where w1.id = w2.id order by w1.year desc, w1.id desc
平均查询100次所需时间:12s
第四种方案:
select * from article w1
where id in
(
select top 30 id from
(
select top 1030 id, year from article order by year desc, id desc
) w order by w.year asc, w.id asc
)
order by w1.year desc, w1.id desc
平均查询100次所需时间:13s
第五种方案:
select w2.n, w1.* from article w1,( select top 1030 row_number() over (order by year desc, id desc) n, id from article) w2 where w1.id = w2.id and w2.n > 1000 order by w2.n asc
平均查询100次所需时间:14s
由此可见在查询页数靠前时,效率3>4>5>2>1,页码靠后时5>4>3>1>2,再根据用户习惯,一般用户的检索只看最前面几页,因此选择3 4 5方案均可,若综合考虑方案5是最好的选择,但是要注意sql2000不支持row_number()函数,由于时间和条件的限制没有做更深入、范围更广的测试,有兴趣的可以仔细研究下。
以下是根据第四种方案编写的一个分页存储过程:
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sys_page_v2]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[sys_page_v2]
go
create procedure [dbo].[sys_page_v2]
@pcount int output, --总页数输出
@rcount int output, --总记录数输出
@sys_table nvarchar(100), --查询表名
@sys_key varchar(50), --主键
@sys_fields nvarchar(500), --查询字段
@sys_where nvarchar(3000), --查询条件
@sys_order nvarchar(100), --排序字段
@sys_begin int, --开始位置
@sys_pageindex int, --当前页数
@sys_pagesize int --页大小
as
set nocount on
set ansi_warnings on
if @sys_pagesize < 0 or @sys_pageindex < 0
begin
return
end
declare @new_where1 nvarchar(3000)
declare @new_order1 nvarchar(100)
declare @new_order2 nvarchar(100)
declare @sql nvarchar(4000)
declare @sqlcount nvarchar(4000)
declare @top int
if(@sys_begin <=0)
set @sys_begin=0
else
set @sys_begin=@sys_begin-1
if isnull(@sys_where,'') = ''
set @new_where1 = ' '
else
set @new_where1 = ' where ' + @sys_where
if isnull(@sys_order,'') <> ''
begin
set @new_order1 = ' order by ' + replace(@sys_order,'desc','')
set @new_order1 = replace(@new_order1,'asc','desc')
set @new_order2 = ' order by ' + @sys_order
end
else
begin
set @new_order1 = ' order by id desc'
set @new_order2 = ' order by id asc'
end
set @sqlcount = 'select @rcount=count(1),@pcount=ceiling((count(1)+0.0)/'
+ cast(@sys_pagesize as nvarchar)+') from ' + @sys_table + @new_where1
exec sp_executesql @sqlcount,n'@rcount int output,@pcount int output',
@rcount output,@pcount output
if @sys_pageindex > ceiling((@rcount+0.0)/@sys_pagesize) --如果输入的当前页数大于实际总页数,则把实际总页数赋值给当前页数
begin
set @sys_pageindex = ceiling((@rcount+0.0)/@sys_pagesize)
end
set @sql = 'select '+ @sys_fields +' from ' + @sys_table + ' w1 '
+ ' where '+ @sys_key +' in ('
+'select top '+ ltrim(str(@sys_pagesize)) +' ' + @sys_key + ' from '
+'('
+'select top ' + ltrim(str(@sys_pagesize * @sys_pageindex + @sys_begin)) + ' ' + @sys_key + ' from '
+ @sys_table + @new_where1 + @new_order2
+') w ' + @new_order1
+') ' + @new_order2
print(@sql)
exec(@sql)
go