主頁 > 知識(shí)庫 > 大數(shù)據(jù)量分頁存儲(chǔ)過程效率測試附測試代碼與結(jié)果

大數(shù)據(jù)量分頁存儲(chǔ)過程效率測試附測試代碼與結(jié)果

熱門標(biāo)簽:呼叫中心市場需求 AI電銷 百度競價(jià)排名 Linux服務(wù)器 服務(wù)外包 地方門戶網(wǎng)站 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng)
測試環(huán)境
硬件:CPU 酷睿雙核T5750 內(nèi)存:2G
軟件:Windows server 2003 + sql server 2005
OK,我們首先創(chuàng)建一數(shù)據(jù)庫:data_Test,并在此數(shù)據(jù)庫中創(chuàng)建一表:tb_TestTable
復(fù)制代碼 代碼如下:

create database data_Test --創(chuàng)建數(shù)據(jù)庫
data_Testnbsp;
GO
use data_Test
GO
create table tb_TestTable --創(chuàng)建表
(id int identity(1,1) primary key,
userName nvarchar(20) not null,
userPWD nvarchar(20) not null,
userEmail nvarchar(40) null)
GO

然后我們在數(shù)據(jù)表中插入2000000條數(shù)據(jù):

復(fù)制代碼 代碼如下:

--插入數(shù)據(jù)
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

我首先寫了五個(gè)常用存儲(chǔ)過程:
1,利用select top 和select not in進(jìn)行分頁,具體代碼如下:
復(fù)制代碼 代碼如下:
create procedure proc_paged_with_notin --利用select top and select not in
(
@pageIndex int, --頁索引
@pageSize int --每頁記錄數(shù)
)
as
begin
set nocount on;
declare @timediff datetime --耗時(shí)
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后不支技直接接參數(shù),所以寫成了字符串@sql
select datediff(ms,@timediff,GetDate()) as 耗時(shí)
set nocount off;
end

2,利用select top 和 select max(列鍵)
復(fù)制代碼 代碼如下:
create procedure proc_paged_with_selectMax --利用select top and select max(列)
(
@pageIndex int, --頁索引
@pageSize int --頁記錄數(shù)
)
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 耗時(shí)
set nocount off;
end

3,利用select top和中間變量--此方法因網(wǎng)上有人說效果最佳,所以貼出來一同測試
復(fù)制代碼 代碼如下:
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 耗時(shí)
set nocount off;
end

4,利用Row_number() 此方法為sql server 2005中新的方法,利用Row_number()給數(shù)據(jù)行加上索引
復(fù)制代碼 代碼如下:
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 耗時(shí)
set nocount off;
end

5,利用臨時(shí)表及Row_number
復(fù)制代碼 代碼如下:
create procedure proc_CTE --利用臨時(shí)表及Row_number
(
@pageIndex int, --頁索引
@pageSize int --頁記錄數(shù)
)
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,至此,存儲(chǔ)過程創(chuàng)建完畢,我們分別在每頁10條數(shù)據(jù)的情況下在第2頁,第1000頁,第10000頁,第100000頁,第199999頁進(jìn)行測試,耗時(shí)單位:ms 每頁測試5次取其平均值
存過 第2頁耗時(shí) 第1000頁耗時(shí) 第10000頁耗時(shí) 第100000頁耗時(shí) 第199999頁耗時(shí) 效率排行
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臨時(shí)表 780ms 796ms 798ms 780ms 805ms 4

測試結(jié)果顯示:select max >row_number>not in>臨時(shí)表>中間變量
于是我對(duì)效率最高的select max方法用2分法進(jìn)行了擴(kuò)展,代碼取自互聯(lián)網(wǎng),我修改了ASC排序時(shí)取不到值的BUG,測試結(jié)果:
2分法 156ms 156ms 180ms 470ms 156ms 1*
從測試結(jié)果來看,使用2分法確實(shí)可以提高效率并使效率更為穩(wěn)定,我又增加了第159999頁的測試,用時(shí)僅296ms,效果相當(dāng)?shù)牟诲e(cuò)!
下面是2分法使用select max的代碼,已相當(dāng)完善。

復(fù)制代碼 代碼如下:

--/*-----存儲(chǔ)過程 分頁處理 孫偉 2005-03-28創(chuàng)建 -------*/
--/*-----存儲(chǔ)過程 分頁處理 浪塵 2008-9-1修改----------*/
--/*----- 對(duì)數(shù)據(jù)進(jìn)行了2分處理使查詢前半部分?jǐn)?shù)據(jù)與查詢后半部分?jǐn)?shù)據(jù)性能相同 -------*/

alter PROCEDURE proc_paged_2part_selectMax
(
@tblName nvarchar(200), ----要顯示的表或多個(gè)表的連接
@fldName nvarchar(500) = '*', ----要顯示的字段列表
@pageSize int = 10, ----每頁顯示的記錄個(gè)數(shù)
@page int = 1, ----要顯示那一頁的記錄
@fldSort nvarchar(200) = null, ----排序字段列表或條件
@Sort bit = 0, ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個(gè)排序字段的排列順序(最后一個(gè)排序字段不加排序標(biāo)記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ----查詢條件,不需where
@ID nvarchar(150), ----主表的主鍵
@Dist bit = 0, ----是否添加查詢字段的 DISTINCT 默認(rèn)0不添加/1添加
@pageCount int = 1 output, ----查詢結(jié)果分頁后的總頁數(shù)
@Counts int = 1 output ----查詢到的記錄數(shù)
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放動(dòng)態(tài)生成的SQL語句
Declare @strTmp nvarchar(1000) ----存放取得查詢結(jié)果總數(shù)的查詢語句
Declare @strID nvarchar(1000) ----存放取得查詢開頭或結(jié)尾ID的查詢語句

Declare @strSortType nvarchar(10) ----數(shù)據(jù)排序規(guī)則A
Declare @strFSortType nvarchar(10) ----數(shù)據(jù)排序規(guī)則B

Declare @SqlSelect nvarchar(50) ----對(duì)含有DISTINCT的查詢進(jìn)行SQL構(gòu)造
Declare @SqlCounts nvarchar(50) ----對(duì)含有DISTINCT的總數(shù)查詢進(jìn)行SQL構(gòu)造

declare @timediff datetime --耗時(shí)測試時(shí)間差
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為取得查詢結(jié)果數(shù)量的語句
if @strCondition is null or @strCondition='' --沒有設(shè)置顯示條件
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

----取得查詢結(jié)果總數(shù)量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts

--取得分頁總數(shù)
set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize

/**//**//**//**當(dāng)前頁大于總頁數(shù) 取最后一頁**/
if @page>@pageCount
set @page=@pageCount

--/*-----數(shù)據(jù)分頁2分處理-------*/
declare @pageIndex int --總數(shù)/頁大小
declare @lastcount int --總數(shù)%頁大小

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='' --沒有設(shè)置顯示條件
begin
if @pageIndex2 or @page=@pageIndex / 2 + @pageIndex % 2 --前半部分?jǐn)?shù)據(jù)處理
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 --后半部分?jǐn)?shù)據(jù)處理
if @page = 1 --最后一頁數(shù)據(jù)顯示
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 --前半部分?jǐn)?shù)據(jù)處理
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 --后半部分?jǐn)?shù)據(jù)處理
if @page = 1 --最后一頁數(shù)據(jù)顯示
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

------返回查詢結(jié)果-----
exec sp_executesql @strTmp
select datediff(ms,@timediff,getdate()) as 耗時(shí)
--print @strTmp
SET NOCOUNT OFF
GO

執(zhí)行示例:exec proc_paged_2part_selectMax 'tb_testTable','ID,userName,userPWD,userEmail',10,100000,'ID',0,null,'ID',0
這種測試只在單機(jī)進(jìn)行,并且沒有在實(shí)際開發(fā)WEB項(xiàng)目中分頁測試,測試項(xiàng)也比較單一,所以不夠全面系統(tǒng),但從其效率相比上,我們可以在數(shù)據(jù)庫分頁算法上進(jìn)行有效的控制。
您可能感興趣的文章:
  • 大數(shù)據(jù)量時(shí)提高分頁的效率
  • 數(shù)據(jù)庫高并發(fā)情況下重復(fù)值寫入的避免 字段組合約束
  • 大數(shù)據(jù)量,海量數(shù)據(jù)處理方法總結(jié)
  • MySQL數(shù)據(jù)庫優(yōu)化經(jīng)驗(yàn)詳談(服務(wù)器普通配置)
  • MySQL數(shù)據(jù)庫十大優(yōu)化技巧
  • SQL Server數(shù)據(jù)庫的高性能優(yōu)化經(jīng)驗(yàn)總結(jié)
  • sql 存儲(chǔ)過程分頁代碼 支持億萬龐大數(shù)據(jù)量
  • 數(shù)據(jù)庫性能優(yōu)化三:程序操作優(yōu)化提升性能
  • 數(shù)據(jù)庫性能優(yōu)化二:數(shù)據(jù)庫表優(yōu)化提升性能
  • 數(shù)據(jù)庫性能優(yōu)化一:數(shù)據(jù)庫自身優(yōu)化提升性能
  • Oracle 數(shù)據(jù)庫優(yōu)化實(shí)戰(zhàn)心得總結(jié)
  • 優(yōu)化mysql數(shù)據(jù)庫的經(jīng)驗(yàn)總結(jié)
  • mysql數(shù)據(jù)庫優(yōu)化總結(jié)(心得)
  • 關(guān)于數(shù)據(jù)庫優(yōu)化問題收集匯總
  • oracle數(shù)據(jù)庫sql的優(yōu)化總結(jié)
  • sql 百萬級(jí)數(shù)據(jù)庫優(yōu)化方案分享
  • 優(yōu)化Mysql數(shù)據(jù)庫的8個(gè)方法
  • mysql數(shù)據(jù)庫sql優(yōu)化原則(經(jīng)驗(yàn)總結(jié))
  • 針對(duì)Sqlserver大數(shù)據(jù)量插入速度慢或丟失數(shù)據(jù)的解決方法
  • MySQL數(shù)據(jù)庫優(yōu)化詳解
  • MySQL中實(shí)現(xiàn)高性能高并發(fā)計(jì)數(shù)器方案(例如文章點(diǎn)擊數(shù))
  • 超大數(shù)據(jù)量存儲(chǔ)常用數(shù)據(jù)庫分表分庫算法總結(jié)
  • sqlserver數(shù)據(jù)庫優(yōu)化解析(圖文剖析)
  • 詳解MySQL性能優(yōu)化(一)
  • 詳解MySQL性能優(yōu)化(二)
  • 19個(gè)MySQL性能優(yōu)化要點(diǎn)解析
  • 大數(shù)據(jù)量高并發(fā)的數(shù)據(jù)庫優(yōu)化詳解

標(biāo)簽:湘潭 黃山 仙桃 崇左 湖南 銅川 衡水 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《大數(shù)據(jù)量分頁存儲(chǔ)過程效率測試附測試代碼與結(jié)果》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266