ps,此文是純個人筆記;
公司裡乙個專案裡用到了一種資料庫分頁查詢的方式:
1、定義乙個臨時的table ,這個table有乙個自增的之間id,和要查的資料表的主鍵id
2、再一次查詢,用id在分頁數段來and 一下結果;
具體操作如下:
定義個臨時表@temptable
declare @temptable table(id int identity(1,1),nid nvarchar(50),updatetime smalldatetime) --含有臨時表的主鍵id--id,待查資料集結果的id,以及更新時間
將結果集插到臨時表中;
insert into @temptable(nid,updatetime)
select targettable.nid,targettable.updatetime --目標表的待查字段
from targettable
where (查詢條件)
order by targettable.updatetime desc --安時間排序一下
再一次查詢,並聯合臨時表查詢,並加上分頁條件
select targettable.arg1,targettable.arg2,targettable.arg3 from targettable --實際要查詢的表字段內容
inner join @temptable temp --內聯臨時表
on temp.nid = targettable.nid
where (查詢條件)
and (temp.id between 分頁起點 and 分頁結束) --分頁條件 比如說1到20個資料結果
這是公司的乙個小專案中的分頁查詢。可以肯定的是,這樣的查詢對小行的系統可行。如果碰到了大型的資料庫,資料量超大的時候,這種查詢將會是個炸彈!
因為臨時表會占用記憶體,如果要查詢的targettable是資料量很大的表的話,臨時表建立需要一大段的時間,這要消耗伺服器的記憶體和cpu資源。如果僅僅是為了乙個分頁效果而犧牲伺服器資源,這樣是不是有點搞笑啊。。。。
SQL SERVER 分頁查詢
方式一 row number select top 頁大小 from select row number over order by id as rownumber,from table1 as a where rownumber 頁大小 當前頁 1 註解 首先利用row number 為table...
Sql Server 分頁查詢
sql server 中通過sql語句實現分頁查詢 方案一 利用not in和select top分頁 select top 頁大小 from 表名 where id not in select top 頁大小 頁數 1 id from 表名 order by id order by id 方案二 ...
sql server實現分頁查詢
資料庫分頁查詢 一 mysql 資料庫分頁查詢 mysql資料庫實現分頁比較簡單,提供了limit函式。一般只需要直接寫到sql語句後面就行了。limit子句可以用來限制由select語句返回過來的資料數量,它有乙個或兩個引數,如果給出兩個引數,第乙個引數指定返回的第一行在所有資料中的位置,從0開始...