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

测验景况 硬件:CPU 酷睿双核T5750 内部存款和储蓄器:2G 软件:Windows server 2002 + sql server 2006OK,大家先是成立一数据库:data_Test,并在那数据库中开创一表:tb_TestTable 复制代码 代码如下: create database data_Test --创造数据库 data_Test GO use data_Test GO create table tb_TestTable --创设表 (id int identity(1,1State of Qatar primary key, userName nvarchar(20卡塔尔国 not null, userPWD nvarchar(20卡塔尔(قطر‎ not null, userEmail nvarchar(40卡塔尔(قطر‎ nullState of Qatar GO 然后我们在数据表中插入二〇〇一000条数据: 复制代码 代码如下: --插入数据 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 ASCState of Qatar)order by ID' execute(@sql卡塔尔(قطر‎ --因select top后不支技直接接参数,所以写成了字符串@sql select datediff(ms,@timediff,GetDate(State of Qatar卡塔尔国 as 耗费时间 set nocount off; end 2,利用select top 和 select max(列键State of Qatar 复制代码 代码如下: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(500State of Qatar select @timediff=Getdate(State of Qatar 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 二零零五中新的法门,利用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 --页记录数 State of Qatar as set nocount on; declare @ctestr nvarchar(400卡塔尔(قطر‎ declare @strSql nvarchar(400) declare @datediff datetime begin select @datediff=GetDate(State of Qatar 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页,第199699页进行测量检验,耗时单位:ms 每页测量检验5次取其平均值 存过 第2页耗费时间 第1000页耗费时间 第10000页耗时第100000页耗费时间 第壹玖玖捌99页耗费时间 效用排名 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_numbernot in偶尔表中间变量 于是本人对作用最高的select max方法用2分法进行了扩充,代码取自网络,笔者改良了ASC排序时取不到值的BUG,测量检验结果: 2分法 156ms 156ms 180ms 470ms 156ms 1* 从测验结果来看,使用2分法确实能够进步效能并使功用进一步牢固,笔者又增添了第159999页的测验,用时仅296ms,效果卓殊的不易! 上边是2分法使用select max的代码,已极其周详。 复制代码 代码如下: --/*-----存款和储蓄进度 分页管理孙伟 二〇〇七-03-28创立 -------*/ --/*-----存款和储蓄进度 分页管理 浪尘 二〇一〇-9-1校订----------*/ --/*----- 对数据开展了2分管理使查询前半局地数量与查询后半局地数目质量相像-------*/ alter PROCEDURE proc_paged_2part_selectMax ( @tblName nvarchar(200卡塔尔国, ----要呈现的表或七个表的连接 @fldName nvarchar(500卡塔尔(قطر‎ = '*', ----要显得的字段列表 @pageSize int = 10, ----每页展现的笔录个数 @page int = 1, ----要来得那一页的记录 @fldSort nvarchar(200State of Qatar = null, ----排序字段列表或标准 @Sort bit = 0, ----排序方法,0为升序,1为降序(倘若是多字段排列Sort指代最后四个排序字段的排列顺序(最终一个排序字段不加排序标识State of Qatar--程序传参如:' 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(*State of Qatar' 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 (10卡塔尔国 ' + @strCondition set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (10卡塔尔国 ' + @strCondition set @strID = ' From ' + @tblName + ' where (10卡塔尔 ' + @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 @pageIndex2 or @page=@pageIndex / 2 + @pageIndex % 2 --前半有的数目管理 begin if @page=1 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHA本田CR-V(4卡塔尔卡塔尔+' '+ @fldName+' from '+@tblName +' order by '+ @fldSort +' '+ @strFSortType else begin if @Sort=1 begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAWrangler(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 @pageIndex2 or @page=@pageIndex / 2 + @pageIndex % 2 --前半片段数据管理begin if @page=1 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHA传祺(4卡塔尔国卡塔尔国+' '+ @fldName+' from '+@tblName +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType else if(@Sort=1卡塔尔 begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHA大切诺基(4卡塔尔State of Qatar+' '+ @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(20State of Qatar卡塔尔(قطر‎ +' '+ @ID +' from '+@tblName +' where (1=1State of Qatar ' + @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-2State of Qatar+@lastcount as Varchar(20卡塔尔国卡塔尔国 +' '+ @ID +' from '+@tblName +' where (1=1卡塔尔 '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+'卡塔尔 AS TBMaxIDState of Qatar' +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+'State of Qatar AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType end end ------重临查询结果----- exec sp_executesql @strTmp select datediff(ms,@timediff,getdate(State of QatarState of Qatar 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项目中分页测量检验,测量试验项也正如单一,所以相当不够完善系统,但从其效用比较上,大家能够在数据库分页算法上开展实用的主宰。

本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:大数据量分页存储过程效率测试附测试代码与结果

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。