程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 大數據量分頁存儲過程效率測試附測試代碼與結果

大數據量分頁存儲過程效率測試附測試代碼與結果

編輯:更多數據庫知識

測試環境
硬件: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','[email protected]')
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項目中分頁測試,測試項也比較單一,所以不夠全面系統,但從其效率相比上,我們可以在數據庫分頁算法上進行有效的控制。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved