亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

幾種常見SQL分頁方式效率比較

系統(tǒng) 1675 0

分頁很重要,面試會(huì)遇到。不妨再回顧總結(jié)一下。

1.創(chuàng)建測(cè)試環(huán)境,(插入100萬條數(shù)據(jù)大概耗時(shí)5分鐘)。

      
        create
      
      
        database
      
       DBTest
      
use DBTest

-- 創(chuàng)建測(cè)試表
create table pagetest
(
id int identity ( 1 , 1 ) not null ,
col01 int null ,
col02 nvarchar ( 50 ) null ,
col03 datetime null
)

-- 1萬記錄集
declare @i int
set @i = 0
while ( @i < 10000 )
begin
insert into pagetest select cast ( floor ( rand () * 10000 ) as int ), left ( newid (), 10 ), getdate ()
set @i = @i + 1
end


2.幾種典型的分頁sql,下面例子是每頁50條,198*50=9900,取第199頁數(shù)據(jù)。

      
        --
      
      
        寫法1,not in/top
      
      
        
select top 50 * from pagetest
where id not in ( select top 9900 id from pagetest order by id)
order by id




-- 寫法2,not exists
select top 50 * from pagetest
where not exists
( select 1 from ( select top 9900 id from pagetest order by id)a where a.id = pagetest.id)
order by id

-- 寫法3,max/top
select top 50 * from pagetest
where id > ( select max (id) from ( select top 9900 id from pagetest order by id)a)
order by id

-- 寫法4,row_number()
select top 50 * from
( select row_number() over ( order by id)rownumber, * from pagetest)a
where rownumber > 9900

select * from
( select row_number() over ( order by id)rownumber, * from pagetest)a
where rownumber > 9900 and rownumber < 9951

select * from
( select row_number() over ( order by id)rownumber, * from pagetest)a
where rownumber between 9901 and 9950

-- 寫法5,在csdn上一帖子看到的,row_number() 變體,不基于已有字段產(chǎn)生記錄序號(hào),先按條件篩選以及排好序,再在結(jié)果集上給一常量列用于產(chǎn)生記錄序號(hào)
select *
from (
select row_number() over ( order by tempColumn)rownumber, *
from ( select top 9950 tempColumn = 0 , * from pagetest where 1 = 1 order by id)a
)b
where rownumber > 9900

3.分別在1萬,10萬(取1990頁),100(取19900頁)記錄集下測(cè)試。

測(cè)試sql:

      
        declare
      
      
        @begin_date
      
      
        datetime
      
      
declare @end_date datetime
select @begin_date = getdate ()

< .....YOUR CODE..... >

select @end_date = getdate ()
select datediff (ms, @begin_date , @end_date ) as ' 毫秒 '

?

1萬:基本感覺不到差異。

10萬:

幾種常見SQL分頁方式效率比較

100萬:

幾種常見SQL分頁方式效率比較

?

4.結(jié)論:

1.max/top,ROW_NUMBER()都是比較不錯(cuò)的分頁方法。相比ROW_NUMBER()只支持sql2005及以上版本,max/top有更好的可移植性,能同時(shí)適用于sql2000,access。

2.not exists感覺是要比not in效率高一點(diǎn)點(diǎn)。

3.ROW_NUMBER()的3種不同寫法效率看起來差不多。

4.ROW_NUMBER() 的變體基于我這個(gè)測(cè)試效率實(shí)在不好。原帖在這里? http://topic.csdn.net/u/20100617/04/80d1bd99-2e1c-4083-ad87-72bf706cb536.html

?

PS.上面的分頁排序都是基于自增字段id。測(cè)試環(huán)境還提供了int,nvarchar,datetime類型字段,也可以試試。不過對(duì)于非主鍵沒索引的大數(shù)據(jù)量排序效率應(yīng)該是很不理想的。

?

5.簡(jiǎn)單將ROWNUMBER,max/top的方式封裝到存儲(chǔ)過程。

ROWNUMBER():

      
        create
      
      
        proc
      
      
        [
      
      
        dbo
      
      
        ]
      
      .
      
        [
      
      
        spSqlPageByRownumber
      
      
        ]
      
      
@tbName varchar ( 255 ), -- 表名
@tbFields varchar ( 1000 ), -- 返回字段
@PageSize int , -- 頁尺寸
@PageIndex int , -- 頁碼
@strWhere varchar ( 1000 ), -- 查詢條件
@StrOrder varchar ( 255 ), -- 排序條件
@Total int output -- 返回總記錄數(shù)
as
declare @strSql varchar ( 5000 ) -- 主語句
declare @strSqlCount nvarchar ( 500 ) -- 查詢記錄總數(shù)主語句

-- ------------總記錄數(shù)---------------
if @strWhere != ''
begin
set @strSqlCount = ' Select @TotalCout=count(*) from ' + @tbName + ' where ' + @strWhere
end
else
begin
set @strSqlCount = ' Select @TotalCout=count(*) from ' + @tbName
end
-- ------------分頁------------
if @PageIndex <= 0
begin
set @PageIndex = 1
end

set @strSql = ' Select * from (Select row_number() over( ' + @strOrder + ' ) rowId, ' + @tbFields
+ ' from ' + @tbName + ' where 1=1 ' + @strWhere + ' ) tb where tb.rowId > ' + str (( @PageIndex - 1 ) * @PageSize )
+ ' and tb.rowId <= ' + str ( @PageIndex * @PageSize )

exec sp_executesql @strSqlCount ,N ' @TotalCout int output ' , @Total output
exec ( @strSql )

Max/top:(簡(jiǎn)單寫了下,需要滿足主鍵字段名稱就是"id")

      
        create
      
      
        proc
      
      
        [
      
      
        dbo
      
      
        ]
      
      .
      
        [
      
      
        spSqlPageByMaxTop
      
      
        ]
      
      
@tbName varchar ( 255 ), -- 表名
@tbFields varchar ( 1000 ), -- 返回字段
@PageSize int , -- 頁尺寸
@PageIndex int , -- 頁碼
@strWhere varchar ( 1000 ), -- 查詢條件
@StrOrder varchar ( 255 ), -- 排序條件
@Total int output -- 返回總記錄數(shù)
as
declare @strSql varchar ( 5000 ) -- 主語句
declare @strSqlCount nvarchar ( 500 ) -- 查詢記錄總數(shù)主語句

-- ------------總記錄數(shù)---------------
if @strWhere != ''
begin
set @strSqlCount = ' Select @TotalCout=count(*) from ' + @tbName + ' where ' + @strWhere
end
else
begin
set @strSqlCount = ' Select @TotalCout=count(*) from ' + @tbName
end
-- ------------分頁------------
if @PageIndex <= 0
begin
set @PageIndex = 1
end

set @strSql = ' select top ' + str ( @PageSize ) + ' * from ' + @tbName + '
where id>(select max(id) from (select top
' + str (( @PageIndex - 1 ) * @PageSize ) + ' id from ' + @tbName + ' ' + @strOrder + ' )a)
' + @strOrder + ''

exec sp_executesql @strSqlCount ,N ' @TotalCout int output ' , @Total output
exec ( @strSql )

園子里搜到Max/top這么一個(gè)版本,看起來很強(qiáng)大, http://www.cnblogs.com/hertcloud/archive/2005/12/21/301327.html

調(diào)用:

      
        declare
      
      
        @count
      
      
        int
      
      
-- exec [dbo].[spSqlPageByRownumber]'pagetest','*',50,20,'','order by id asc',@count output
exec [ dbo ] . [ spSqlPageByMaxTop ]' pagetest ' , ' * ' , 50 , 20 , '' , ' order by id asc ' , @count output
select @count



幾種常見SQL分頁方式效率比較


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 天天干天天曰天天操 | 国产福利不卡视频在免费播放 | 久久亚洲一级α片 | 中文一级国产特级毛片视频 | 色婷婷综合久久久久中文 | 成人一级黄色片 | 一级aaa级毛片午夜在线播放 | 久久综合久久伊人 | 成人国产精品999视频 | 色综合天天综合网国产人 | 九月丁香婷婷亚洲综合色 | 爱爱爱免费视频 | 久久精品69 | 久久国产免费福利永久 | 久久se精品一区二区国产 | 免费www xxx| 亚洲成人高清在线 | 日韩免费黄色片 | 伊人999| 国产原创麻豆精品视频 | 亚洲欧美一区二区三区四区 | 久色99| 婷婷四房综合激情五月在线 | 毛片久久久 | 亚洲国产精品aa在线看 | 国产高清国产精品国产k | 日本又黄又爽又色的免费视频 | 一本大道加勒比久久综合 | 国产成人短视频 | 日本精品一区二区三区视频 | 久久精品国产只有精品6 | 成人亚洲精品一区二区 | 91国内在线国内在线播放 | 国产在线一区在线视频 | 最新99国产成人精品视频免费 | 日本在线网址 | 99热国产在线观看 | 热久久精品免费视频 | 久久亚洲精品中文字幕三区 | 国产一区中文字幕 | 九九热精品国产 |