人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

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

發(fā)布日期:2022-01-28 16:36 | 文章來(lái)源:站長(zhǎng)之家

硬件: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_Test 
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

我首先寫(xiě)了五個(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ù),所以寫(xiě)成了字符串@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分處理使查詢(xún)前半部分?jǐn)?shù)據(jù)與查詢(xún)后半部分?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, ----查詢(xún)條件,不需where
@ID nvarchar(150), ----主表的主鍵
@Dist bit = 0, ----是否添加查詢(xún)字段的 DISTINCT 默認(rèn)0不添加/1添加
@pageCount int = 1 output, ----查詢(xún)結(jié)果分頁(yè)后的總頁(yè)數(shù)
@Counts int = 1 output ----查詢(xún)到的記錄數(shù)
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放動(dòng)態(tài)生成的SQL語(yǔ)句
Declare @strTmp nvarchar(1000) ----存放取得查詢(xún)結(jié)果總數(shù)的查詢(xún)語(yǔ)句
Declare @strID nvarchar(1000) ----存放取得查詢(xún)開(kāi)頭或結(jié)尾ID的查詢(xún)語(yǔ)句 Declare @strSortType nvarchar(10) ----數(shù)據(jù)排序規(guī)則A
Declare @strFSortType nvarchar(10) ----數(shù)據(jù)排序規(guī)則B Declare @SqlSelect nvarchar(50) ----對(duì)含有DISTINCT的查詢(xún)進(jìn)行SQL構(gòu)造
Declare @SqlCounts nvarchar(50) ----對(duì)含有DISTINCT的總數(shù)查詢(xún)進(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 --------生成查詢(xún)語(yǔ)句--------
--此處@strTmp為取得查詢(xún)結(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 ----取得查詢(xún)結(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 @pageIndex<2 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 --有查詢(xún)條件
begin
if @pageIndex<2 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 ------返回查詢(xún)結(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í)際開(kāi)發(fā)WEB項(xiàng)目中分頁(yè)測(cè)試,測(cè)試項(xiàng)也比較單一,所以不夠全面系統(tǒng),但從其效率相比上,我們可以在數(shù)據(jù)庫(kù)分頁(yè)算法上進(jìn)行有效的控制。

美國(guó)服務(wù)器租用

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線(xiàn)路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線(xiàn)

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線(xiàn)
客服

在線(xiàn)客服:7*24小時(shí)在線(xiàn)

客服
熱線(xiàn)

400-630-3752
7*24小時(shí)客服服務(wù)熱線(xiàn)

關(guān)注
微信

關(guān)注官方微信
頂部