分頁實現方法的效能比較

2021-06-15 21:36:14 字數 2575 閱讀 7143

我們先給出幾種主要的分頁方法和核心語句,然後直接給出結論,有興趣的讀者可以看看後面的資料

幾種常用儲存過程分頁方法

topn方法

select top(@pagesize) from tablename where id not in  

(select top ((@pageindex-1)*@pagesize)  id from table name where .... order by ... )

where .... order by ...

臨時表

declare @indextable table(id int identity(1,1),nid int,postusername nvarchar(50))

declare @pagelowerbound int

declare @pageupperbound int

set @pagelowerbound=(@pageindex-1)*@pagesize--下限

set @pageupperbound=@pagelowerbound+@pagesize--上限

set rowcount @pageupperbound

insert into @indextable(nid,postusername) select replyid,postusername from  tablename order by ......

select *  from  tablename p,@indextable t where p.id=t.nid

and t.id>@pagelowerbound and t.id<=@pageupperbound order by t.id

cte--2005新語法,類似臨時表,但是生命週期稍微不同,這裡只是他的乙個運用

with cte_temp--定義零時表,pageindex是乙個計算字段,儲存了搜尋結果的頁號

as (ceiling((row_number() over(order by .... )-1)/@pagesize as int) as pageindex,* from tablename where.....)

select *  from cte_temp where pageindex=@pageindex-1;

結論:

topn在小頁數下最快,如果在10頁以下,可以考慮用它,cte和臨時表時間很穩定,cte消耗的時間比臨時表多,但是不會引起tempdb的暴漲和io增加

效能比較

試驗環境:win2003server,sqlserver2005,庫大小2,567,245行,沒有where子句,試驗時每頁大小50,頁碼作為變數

取0,3,10,31,100,316,1000,3162...頁,也就是10的指數,試驗結果如下

頁數topn

cte臨時表(有快取)

臨時表(無快取)

公司正在使用的儲存過程

cte改進13

資料解釋和分析

臨時表分為有沒有快取兩種時間,cte就是上面的方法,cte改進只是把選入cte臨時表的列數減少了,只選取了頁號和主鍵,null表示時間無法計算(時間太長),資料單位是毫秒.

從上面的資料可以看到,topn在前32頁都是有優勢的,但是頁數增大後,效能降低很快,cte改進比cte有所進步,平均進步兩秒左右,但是還是比臨時表慢,但是考慮臨時表會增大日誌檔案的大小,引起大量io,cte也就有他自己的優勢,公司現在正在使用的儲存過程效率不錯,但是在頁碼靠後的情況下效能會降低

資料庫分頁實現方法的效能比較

分頁實現方法的效能比較 我們先給出幾種主要的分頁方法和核心語句,然後直接給出結論,有興趣的讀者可以看看後面的資料 幾種常用儲存過程分頁方法 topn方法 select top pagesize from tablename where id not in select top pageindex 1...

各種排序方法的效能比較

測試環境說明 win xp下,vs2008,主頻 core2 雙核2.53ghz 下面是測試的 using system using system.collections.generic using system.linq using system.text using system.collect...

併發實現方案的效能比較

偶然看到erlang vs.stackless python a first benchmark,對erlang和stackless python的併發處理效能進行了實驗比較,基本結論認為二者有比較相近的效能。我看完產生的問題是,stackless python與python的其他併發實現機制效能又...