臨近春節(jié),心早已飛了不在工作上了,下面小編給大家整理些數(shù)據(jù)庫的幾種分頁查詢。
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、沙縣ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的沙縣網(wǎng)站制作公司
Sql
Sever
2005之前版本:
select
top
頁大小
*
from
表名
where
id
not
in
(
select
top
頁大小*(查詢第幾頁-1)
id
from
表名
order
by
id
)
order
by
id
例如:
select
top
10
*
--10
為頁大小
from
[TCCLine].[dbo].[CLine_CommonImage]
where
id
not
in
(
--40是這么計(jì)算出來的:10*(5-1)
--
頁大小*(查詢第幾頁-1)
select
top
40
id
from
[TCCLine].[dbo].[CLine_CommonImage]
order
by
id
)
order
by
id
結(jié)果為:
Sql
Sever
2005及以上版本,多了個(gè)分頁查詢方法:
/*
*
firstIndex:起始索引
*
pageSize:每頁顯示的數(shù)量
*
orderColumn:排序的字段名
*
SQL:可以是簡(jiǎn)單的單表查詢語句,也可以是復(fù)雜的多表聯(lián)合查詢語句
*/
select
top
pageSize
o.*
from
(select
row_number()
over(order
by
orderColumn)
as
rownumber,*
from(SQL)
as
o
where
rownumberfirstIndex;
例如:
select
top
10
numComImg.*
from
(
select
row_number()
over(order
by
id
asc)
as
rownumber,*
from
(select
*
FROM
[TCCLine].[dbo].[CLine_CommonImage])
as
comImg)
as
numComImg
where
rownumber40
結(jié)果:
這兩個(gè)方法,就僅僅是多了一列
rewnumber
嗎?當(dāng)然不是,來看下內(nèi)部差別吧:
在兩個(gè)SQL上,分別加入以下SQL,并使用MS的“包括執(zhí)行計(jì)劃”,便于查看執(zhí)行詳情:
SET
STATISTICS
TIME
ON
GO
要執(zhí)行的SQL:
SET
STATISTICS
TIME
ON
GO
select
top
10
numComImg.*
from
(
select
row_number()
over(order
by
id
asc)
as
rownumber,*
from
(select
*
FROM
[TCCLine].[dbo].[CLine_CommonImage])
as
comImg)
as
numComImg
where
rownumber40
SET
STATISTICS
TIME
ON
GO
select
top
10
*
--10
為頁大小
from
[TCCLine].[dbo].[CLine_CommonImage]
where
id
not
in
(
--40是這么計(jì)算出來的:10*(5-1)
--
頁大小*(查詢第幾頁-1)
select
top
40
id
from
[TCCLine].[dbo].[CLine_CommonImage]
order
by
id
)
order
by
id
執(zhí)行之后,查看執(zhí)行計(jì)劃:
看得出,兩個(gè)同樣功能的SQL,執(zhí)行時(shí),使用
row_number()
的,要比是用
純TOP方式的,查詢開銷少得多,上圖顯示
28:72,純top方式,使用了兩次聚集掃描。
再來看下執(zhí)行時(shí)間信息:
row_number()方式的:
純top方式:
相比之下,還是row_number()解析函數(shù)效率比較高寫。
以上所述是小編給大家分享的SQLSERVER分頁查詢關(guān)于使用Top方式和row_number()解析函數(shù)的不同,希望對(duì)大家有所幫助。
沒有區(qū)別個(gè)人的習(xí)慣 在SQLServer2000中top后只可以跟常數(shù),在2005后可以跟變量了
也許唯一的區(qū)別就是top(Expression)括號(hào)中可以跟表達(dá)式
top是指按順序選取前幾個(gè)數(shù)據(jù)。order
by是屬性默認(rèn)升序排列(asc),如果想倒序+desc
select
*和select
all
select
1一樣是選取所有(任意一個(gè))
top
3前3個(gè)
select
查詢表達(dá)式是+表中的元組中的候選碼
select
no
from
no是table表中的一個(gè)碼,就是唯一標(biāo)示這一元組的名字,額
就是名字
不能直接用select+數(shù)字
只能用select
123
as
no
SQL SELECT TOP 子句用于規(guī)定要返回的記錄的數(shù)目,對(duì)于擁有數(shù)千條記錄的大型表來說,是非常有用的。
from 樹懶學(xué)堂-免費(fèi)的數(shù)據(jù)學(xué)習(xí)平臺(tái)
文章題目:sqlservertop用法的簡(jiǎn)單介紹
當(dāng)前路徑:http://chinadenli.net/article8/dsiciip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、移動(dòng)網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)