欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

大数据量分页存储过程效率测试附测试代码与结果

程序员文章站 2023-12-16 20:24:34
测试环境 硬件:cpu 酷睿双核t5750 内存:2g 软件:windows server 2003 + sql server 2005 ok,我们首先创建一数据库:dat...
测试环境
硬件:cpu 酷睿双核t5750 内存:2g
软件:windows server 2003 + sql server 2005
ok,我们首先创建一数据库:data_test,并在此数据库中创建一表:tb_testtable
复制代码 代码如下:

create database data_test --创建数据库
data_test 
go
use data_test
go
create table tb_testtable --创建表
(id int identity(1,1) primary key,
username nvarchar(20) not null,
userpwd nvarchar(20) not null,
useremail nvarchar(40) null)
go

然后我们在数据表中插入2000000条数据:

复制代码 代码如下:

--插入数据
set identity_insert tb_testtable on
declare @count int
set @count=1
while @count<=2000000
begin
insert into tb_testtable(id,username,userpwd,useremail) values(@count,'admin','admin888','lli0077@yahoo.com.cn')
set @count=@count+1
end
set identity_insert tb_testtable off

我首先写了五个常用存储过程:
1,利用select top 和select not in进行分页,具体代码如下:
复制代码 代码如下:
create procedure proc_paged_with_notin --利用select top and select not in
(
@pageindex int, --页索引
@pagesize int --每页记录数
)
as
begin
set nocount on;
declare @timediff datetime --耗时
declare @sql nvarchar(500)
select @timediff=getdate()
set @sql='select top '+str(@pagesize)+' * from tb_testtable where(id not in(select top '+str(@pagesize*@pageindex)+' id from tb_testtable order by id asc)) order by id'
execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql
select datediff(ms,@timediff,getdate()) as 耗时
set nocount off;
end

2,利用select top 和 select max(列键)
复制代码 代码如下:
create procedure proc_paged_with_selectmax --利用select top and select max(列)
(
@pageindex int, --页索引
@pagesize int --页记录数
)
as
begin
set nocount on;
declare @timediff datetime
declare @sql nvarchar(500)
select @timediff=getdate()
set @sql='select top '+str(@pagesize)+' * from tb_testtable where(id>(select max(id) from (select top '+str(@pagesize*@pageindex)+' id from tb_testtable order by id) as temptable)) order by id'
execute(@sql)
select datediff(ms,@timediff,getdate()) as 耗时
set nocount off;
end

3,利用select top和中间变量--此方法因网上有人说效果最佳,所以贴出来一同测试
复制代码 代码如下:
create procedure proc_paged_with_midvar --利用id>最大id值和中间变量
(
@pageindex int,
@pagesize int
)
as
declare @count int
declare @id int
declare @timediff datetime
declare @sql nvarchar(500)
begin
set nocount on;
select @count=0,@id=0,@timediff=getdate()
select @count=@count+1,@id=case when @count<=@pagesize*@pageindex then id else @id end from tb_testtable order by id
set @sql='select top '+str(@pagesize)+' * from tb_testtable where id>'+str(@id)
execute(@sql)
select datediff(ms,@timediff,getdate()) as 耗时
set nocount off;
end

4,利用row_number() 此方法为sql server 2005中新的方法,利用row_number()给数据行加上索引
复制代码 代码如下:
create procedure proc_paged_with_rownumber --利用sql 2005中的row_number()
(
@pageindex int,
@pagesize int
)
as
declare @timediff datetime
begin
set nocount on;
select @timediff=getdate()
select * from (select *,row_number() over(order by id asc) as idrank from tb_testtable) as idwithrownumber where idrank>@pagesize*@pageindex and idrank<@pagesize*(@pageindex+1)
select datediff(ms,@timediff,getdate()) as 耗时
set nocount off;
end

5,利用临时表及row_number
复制代码 代码如下:
create procedure proc_cte --利用临时表及row_number
(
@pageindex int, --页索引
@pagesize int --页记录数
)
as
set nocount on;
declare @ctestr nvarchar(400)
declare @strsql nvarchar(400)
declare @datediff datetime
begin
select @datediff=getdate()
set @ctestr='with table_cte as
(select ceiling((row_number() over(order by id asc))/'+str(@pagesize)+') as page_num,* from tb_testtable)';
set @strsql=@ctestr+' select * from table_cte where page_num='+str(@pageindex)
end
begin
execute sp_executesql @strsql
select datediff(ms,@datediff,getdate())
set nocount off;
end

ok,至此,存储过程创建完毕,我们分别在每页10条数据的情况下在第2页,第1000页,第10000页,第100000页,第199999页进行测试,耗时单位:ms 每页测试5次取其平均值
存过 第2页耗时 第1000页耗时 第10000页耗时 第100000页耗时 第199999页耗时 效率排行
1用not in 0ms 16ms 47ms 475ms 953ms 3
2用select max 5ms 16ms 35ms 325ms 623ms 1
3中间变量 966ms 970ms 960ms 945ms 933ms 5
4row_number 0ms 0ms 34ms 365ms 710ms 2
4临时表 780ms 796ms 798ms 780ms 805ms 4

测试结果显示:select max >row_number>not in>临时表>中间变量
于是我对效率最高的select max方法用2分法进行了扩展,代码取自互联网,我修改了asc排序时取不到值的bug,测试结果:
2分法 156ms 156ms 180ms 470ms 156ms 1*
从测试结果来看,使用2分法确实可以提高效率并使效率更为稳定,我又增加了第159999页的测试,用时仅296ms,效果相当的不错!
下面是2分法使用select max的代码,已相当完善。

复制代码 代码如下:

--/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/
--/*-----存储过程 分页处理 浪尘 2008-9-1修改----------*/
--/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/

alter procedure proc_paged_2part_selectmax
(
@tblname nvarchar(200), ----要显示的表或多个表的连接
@fldname nvarchar(500) = '*', ----要显示的字段列表
@pagesize int = 10, ----每页显示的记录个数
@page int = 1, ----要显示那一页的记录
@fldsort nvarchar(200) = null, ----排序字段列表或条件
@sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' sorta asc,sortb desc,sortc ')
@strcondition nvarchar(1000) = null, ----查询条件,不需where
@id nvarchar(150), ----主表的主键
@dist bit = 0, ----是否添加查询字段的 distinct 默认0不添加/1添加
@pagecount int = 1 output, ----查询结果分页后的总页数
@counts int = 1 output ----查询到的记录数
)
as
set nocount on
declare @sqltmp nvarchar(1000) ----存放动态生成的sql语句
declare @strtmp nvarchar(1000) ----存放取得查询结果总数的查询语句
declare @strid nvarchar(1000) ----存放取得查询开头或结尾id的查询语句

declare @strsorttype nvarchar(10) ----数据排序规则a
declare @strfsorttype nvarchar(10) ----数据排序规则b

declare @sqlselect nvarchar(50) ----对含有distinct的查询进行sql构造
declare @sqlcounts nvarchar(50) ----对含有distinct的总数查询进行sql构造

declare @timediff datetime --耗时测试时间差
select @timediff=getdate()

if @dist = 0
begin
set @sqlselect = 'select '
set @sqlcounts = 'count(*)'
end
else
begin
set @sqlselect = 'select distinct '
set @sqlcounts = 'count(distinct '+@id+')'
end


if @sort=0
begin
set @strfsorttype=' asc '
set @strsorttype=' desc '
end
else
begin
set @strfsorttype=' desc '
set @strsorttype=' asc '
end



--------生成查询语句--------
--此处@strtmp为取得查询结果数量的语句
if @strcondition is null or @strcondition='' --没有设置显示条件
begin
set @sqltmp = @fldname + ' from ' + @tblname
set @strtmp = @sqlselect+' @counts='+@sqlcounts+' from '+@tblname
set @strid = ' from ' + @tblname
end
else
begin
set @sqltmp = + @fldname + 'from ' + @tblname + ' where (1>0) ' + @strcondition
set @strtmp = @sqlselect+' @counts='+@sqlcounts+' from '+@tblname + ' where (1>0) ' + @strcondition
set @strid = ' from ' + @tblname + ' where (1>0) ' + @strcondition
end

----取得查询结果总数量-----
exec sp_executesql @strtmp,n'@counts int out ',@counts out
declare @tmpcounts int
if @counts = 0
set @tmpcounts = 1
else
set @tmpcounts = @counts

--取得分页总数
set @pagecount=(@tmpcounts+@pagesize-1)/@pagesize

/**//**//**//**当前页大于总页数 取最后一页**/
if @page>@pagecount
set @page=@pagecount

--/*-----数据分页2分处理-------*/
declare @pageindex int --总数/页大小
declare @lastcount int --总数%页大小

set @pageindex = @tmpcounts/@pagesize
set @lastcount = @tmpcounts%@pagesize
if @lastcount > 0
set @pageindex = @pageindex + 1
else
set @lastcount = @pagesize

--//***显示分页
if @strcondition is null or @strcondition='' --没有设置显示条件
begin
if @pageindex<2 or @page<=@pageindex / 2 + @pageindex % 2 --前半部分数据处理
begin
if @page=1
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' order by '+ @fldsort +' '+ @strfsorttype
else
begin
if @sort=1
begin
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' <(select min('+ @id +') from ('+ @sqlselect+' top '+ cast(@pagesize*(@page-1) as varchar(20)) +' '+ @id +' from '+@tblname
+' order by '+ @fldsort +' '+ @strfsorttype+') as tbminid)'
+' order by '+ @fldsort +' '+ @strfsorttype
end
else
begin
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' >(select max('+ @id +') from ('+ @sqlselect+' top '+ cast(@pagesize*(@page-1) as varchar(20)) +' '+ @id +' from '+@tblname
+' order by '+ @fldsort +' '+ @strfsorttype+') as tbminid)'
+' order by '+ @fldsort +' '+ @strfsorttype
end
end
end
else
begin
set @page = @pageindex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@lastcount as varchar(4))+' '+ @fldname+' from '+@tblname
+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
else
if @sort=1
begin
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' >(select max('+ @id +') from('+ @sqlselect+' top '+ cast(@pagesize*(@page-2)+@lastcount as varchar(20)) +' '+ @id +' from '+@tblname
+' order by '+ @fldsort +' '+ @strsorttype+') as tbmaxid)'
+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
end
else
begin
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' <(select min('+ @id +') from('+ @sqlselect+' top '+ cast(@pagesize*(@page-2)+@lastcount as varchar(20)) +' '+ @id +' from '+@tblname
+' order by '+ @fldsort +' '+ @strsorttype+') as tbmaxid)'
+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
end
end
end

else --有查询条件
begin
if @pageindex<2 or @page<=@pageindex / 2 + @pageindex % 2 --前半部分数据处理
begin
if @page=1
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where 1=1 ' + @strcondition + ' order by '+ @fldsort +' '+ @strfsorttype
else if(@sort=1)
begin
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' <(select min('+ @id +') from ('+ @sqlselect+' top '+ cast(@pagesize*(@page-1) as varchar(20)) +' '+ @id +' from '+@tblname
+' where (1=1) ' + @strcondition +' order by '+ @fldsort +' '+ @strfsorttype+') as tbminid)'
+' '+ @strcondition +' order by '+ @fldsort +' '+ @strfsorttype
end
else
begin
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' >(select max('+ @id +') from ('+ @sqlselect+' top '+ cast(@pagesize*(@page-1) as varchar(20)) +' '+ @id +' from '+@tblname
+' where (1=1) ' + @strcondition +' order by '+ @fldsort +' '+ @strfsorttype+') as tbminid)'
+' '+ @strcondition +' order by '+ @fldsort +' '+ @strfsorttype
end
end
else
begin
set @page = @pageindex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@lastcount as varchar(4))+' '+ @fldname+' from '+@tblname
+' where (1=1) '+ @strcondition +' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
else if(@sort=1)
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' >(select max('+ @id +') from('+ @sqlselect+' top '+ cast(@pagesize*(@page-2)+@lastcount as varchar(20)) +' '+ @id +' from '+@tblname
+' where (1=1) '+ @strcondition +' order by '+ @fldsort +' '+ @strsorttype+') as tbmaxid)'
+' '+ @strcondition+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
else
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' <(select min('+ @id +') from('+ @sqlselect+' top '+ cast(@pagesize*(@page-2)+@lastcount as varchar(20)) +' '+ @id +' from '+@tblname
+' where (1=1) '+ @strcondition +' order by '+ @fldsort +' '+ @strsorttype+') as tbmaxid)'
+' '+ @strcondition+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
end
end

------返回查询结果-----
exec sp_executesql @strtmp
select datediff(ms,@timediff,getdate()) as 耗时
--print @strtmp
set nocount off
go

执行示例:exec proc_paged_2part_selectmax 'tb_testtable','id,username,userpwd,useremail',10,100000,'id',0,null,'id',0
这种测试只在单机进行,并且没有在实际开发web项目中分页测试,测试项也比较单一,所以不够全面系统,但从其效率相比上,我们可以在数据库分页算法上进行有效的控制。

上一篇:

下一篇: