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

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

熱門標(biāo)簽:小e電話機(jī)器人 申請(qǐng)400電話在哪辦理流程 貴州房產(chǎn)智能外呼系統(tǒng)供應(yīng)商 百度地圖標(biāo)注改顏色 臨沂智能電銷機(jī)器人加盟哪家好 外呼運(yùn)營(yíng)商線路收費(fèi) 電銷外呼有錄音系統(tǒng)有哪些 一個(gè)導(dǎo)航軟件能用幾個(gè)地圖標(biāo)注點(diǎn) 鎮(zhèn)江網(wǎng)路外呼系統(tǒng)供應(yīng)商
測(cè)試環(huán)境
硬件:CPU 酷睿雙核T5750 內(nèi)存:2G
軟件:Windows server 2003 + sql server 2005
OK,我們首先創(chuàng)建一數(shù)據(jù)庫(kù):data_Test,并在此數(shù)據(jù)庫(kù)中創(chuàng)建一表:tb_TestTable
復(fù)制代碼 代碼如下:

create database data_Test --創(chuàng)建數(shù)據(jù)庫(kù)
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

然后我們?cè)跀?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ǔ)過(guò)程:
1,利用select top 和select not in進(jìn)行分頁(yè),具體代碼如下:
復(fù)制代碼 代碼如下:
create procedure proc_paged_with_notin --利用select top and select not in
(
@pageIndex int, --頁(yè)索引
@pageSize int --每頁(yè)記錄數(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, --頁(yè)索引
@pageSize int --頁(yè)記錄數(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)上有人說(shuō)效果最佳,所以貼出來(lái)一同測(cè)試
復(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, --頁(yè)索引
@pageSize int --頁(yè)記錄數(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ǔ)過(guò)程創(chuàng)建完畢,我們分別在每頁(yè)10條數(shù)據(jù)的情況下在第2頁(yè),第1000頁(yè),第10000頁(yè),第100000頁(yè),第199999頁(yè)進(jìn)行測(cè)試,耗時(shí)單位:ms 每頁(yè)測(cè)試5次取其平均值
存過(guò) 第2頁(yè)耗時(shí) 第1000頁(yè)耗時(shí) 第10000頁(yè)耗時(shí) 第100000頁(yè)耗時(shí) 第199999頁(yè)耗時(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

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

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

--/*-----存儲(chǔ)過(guò)程 分頁(yè)處理 孫偉 2005-03-28創(chuàng)建 -------*/
--/*-----存儲(chǔ)過(guò)程 分頁(yè)處理 浪塵 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, ----每頁(yè)顯示的記錄個(gè)數(shù)
@page int = 1, ----要顯示那一頁(yè)的記錄
@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é)果分頁(yè)后的總頁(yè)數(shù)
@Counts int = 1 output ----查詢到的記錄數(shù)
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放動(dòng)態(tài)生成的SQL語(yǔ)句
Declare @strTmp nvarchar(1000) ----存放取得查詢結(jié)果總數(shù)的查詢語(yǔ)句
Declare @strID nvarchar(1000) ----存放取得查詢開頭或結(jié)尾ID的查詢語(yǔ)句

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í)測(cè)試時(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



--------生成查詢語(yǔ)句--------
--此處@strTmp為取得查詢結(jié)果數(shù)量的語(yǔ)句
if @strCondition is null or @strCondition='' --沒(méi)有設(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

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

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

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

set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize

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

標(biāo)簽:澳門 日照 晉城 保定 合肥 延邊 三明 嘉興

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《大數(shù)據(jù)量分頁(yè)存儲(chǔ)過(guò)程效率測(cè)試附測(cè)試代碼與結(jié)果》,本文關(guān)鍵詞  大,數(shù)據(jù),量,分頁(yè),存儲(chǔ),過(guò)程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《大數(shù)據(jù)量分頁(yè)存儲(chǔ)過(guò)程效率測(cè)試附測(cè)試代碼與結(jié)果》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于大數(shù)據(jù)量分頁(yè)存儲(chǔ)過(guò)程效率測(cè)試附測(cè)試代碼與結(jié)果的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章