大数据量分页存储过程效率测试附代码(上)

发表于:2010-7-29 11:45

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:眼里无神(cnblogs)    来源:51Testing软件测试网采编

  在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下,哪种分页算法效率最优呢?我们不妨用事实说话。

  测试环境

  硬件:CPU 酷睿双核T5750 内存:2G

  软件:Windows server 2003 + sql server 2005

  OK,我们首先创建一数据库:data_Test,并在此数据库中创建一表:tb_TestTable

01    create database data_Test --创建数据库
02    data_Test 
03    GO
04    use data_Test
05    GO
06    create table tb_TestTable --创建表
07    (id int identity(1,1) primary key,
08     userName nvarchar(20) not null,
09     userPWD nvarchar(20) not null,
10     userEmail nvarchar(40) null)
11    GO

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

01    --插入数据
02    set identity_insert tb_TestTable on
03    declare @count int
04    set @count=1
05    while @count<=2000000
06    begin
07    insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn')
08    set @count=@count+1
09    end
10    set identity_insert tb_TestTable off

  我首先写了五个常用存储过程:

  1,利用select top 和select not in进行分页,具体代码如下:

01    create procedure proc_paged_with_notin --利用select top and select not in
02    (
03     @pageIndex int, --页索引
04     @pageSize int --每页记录数
05    )
06    as
07    begin
08    set nocount on;
09    declare @timediff datetime --耗时
10    declare @sql nvarchar(500)
11    select @timediff=Getdate()
12    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'
13    execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql
14    select datediff(ms,@timediff,GetDate()) as 耗时
15    set nocount off;
16    end

  2,利用select top 和 select max(列键)

01    create procedure proc_paged_with_selectMax --利用select top and select max(列)
02    (
03     @pageIndex int, --页索引
04     @pageSize int --页记录数
05    )
06    as
07    begin
08    set nocount on;
09     declare @timediff datetime
10     declare @sql nvarchar(500)
11     select @timediff=Getdate()
12     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'
13     execute(@sql)
14     select datediff(ms,@timediff,GetDate()) as 耗时
15     set nocount off;
16    end
 
31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号