年夜數據量分頁存儲進程效力測試附測試代碼與成果。本站提示廣大學習愛好者:(年夜數據量分頁存儲進程效力測試附測試代碼與成果)文章只能為提供參考,不一定能成為您想要的結果。以下是年夜數據量分頁存儲進程效力測試附測試代碼與成果正文
測試情況
硬件: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項目平分頁測試,測試項也比擬單一,所以不敷周全體系,但從其效力比擬上,我們可以在數據庫分頁算法長進行有用的掌握。