三種資料庫SQL語句高效分頁

2021-04-14 22:41:22 字數 2851 閱讀 8808

在程式的開發過程中,處理分頁是大家接觸比較頻繁的事件,因為現在軟體基本上都是與資料庫進行掛釣的。但效率又是我們所追求的,如果是像原來那樣把所有滿足條件的記錄全部都選擇出來,再去進行分頁處理,那麼就會多多的浪費掉許多的系統處理時間。為了能夠把效率提高,所以現在我們就只選擇我們需要的資料,減少資料庫的處理時間,以下就是常用sql分頁處理:

1、sql server、access資料庫

這都微軟的資料庫,都是一家人,基本的操作都是差不多,常採用如下分頁語句:

pagesize:每頁顯示的記錄數

currentpage:當前頁號

資料表的名字是:components

索引主鍵字是:id

select top pagesize * from components where id not in

(select top (pagesize*(currentpage-1))

id from components order by id)order by id

如下列:

select top 10 * from components where id not in

(select top 10*10 id from components order by id)

order by id

從101條記錄開始選擇,只選擇前面的10條記錄

2、oracle資料庫

因為oracle資料庫沒有top關鍵字,所以這裡就不能夠像微軟的資料據那樣操作,這裡有兩種方法:

(1)、一種是利用相反的。

pagesize:每頁顯示的記錄數

currentpage:當前頁號

資料表的名字是:components

索引主鍵字是:id

select * from components where id not

in(select id from components where

rownum<=(pagesize*(currentpage-1)))

and rownum<=pagesize order by id;

如下例:

select * from components where id not in

(select id from components where rownum<=100)

and rownum<=10 order by id;

從101到記錄開始選擇,選擇前面10條。

(2)、使用minus,即中文的意思就是減去。

select * from components where rownum

<=(pagesize*(currentpage-1)) minus

select * from components where rownum

<=(pagesize*(currentpage-2));

如例:select * from components where

rownum<=10 minus select * from components

where rownum<=5; .

(3)、一種是利用oracle的rownum,這個是oracle查詢自動返回的序號,一般不顯示,但是可以通過select rownum from [表名]看到,注意,它是從1到當前的記錄總數。

select * from (select rownum tid,components.

* from components where rownum<=100) where tid<=10;

3、mysql資料庫

my sql資料庫最簡單,是利用mysql的limit函式,limit [offset,] rows從資料庫表中m條記錄開始檢索n條記錄的語句為:

select * from 表名稱 limit m,n

例如從表sys_option(主鍵為sys_id)中從10條記錄還是檢索20條記錄,語句如下:

select * from sys_option limit 10,20

*************************=附加的分割線****************************************=

---1.oracle

select * from ( select row_.*, rownum rownum_ from (...... ) row_ where rownum <= ?) where rownum_ > ?

先按查詢條件查詢出從0到頁未的記錄.然後再取出從頁開始到頁未的記錄.(據說是效率最高的:))

---2. sql servler

i:select top [pagesize] * from table where id not in ( select top [pagesize*(currentpage-1)] id from table [查詢條件] order by id ) and [查詢條件] order by id

先按查詢條件排除 pagesize*[pagesize*(currentpage-1)]以前的紀錄。&&再按查詢條件把他以後的記錄 top[pagesize] 出來.

ii:select top pagesize * from tablename where id > (select max(id) from (select top startrecord-1 id from tablename [查詢條件] order by id) as temptable) [查詢條件] order by id

先取得開始該頁開始時的最大id,然後再從最大id出top[pagesize]

(聽說記錄組超過10萬第二條好過第一條)

---3.mysql

select * from table [查詢條件] order by id limit ?,?

這個都不用說了。

三種資料庫利用SQL語句進行高效果分頁

在程式的開發過程中,處理分頁 是大家接觸比較頻繁的事件,因為現在軟體基本上都是與資料庫進行掛釣的。但效率又是我們所追求的,如果是像原來那樣把所有滿足條件的記錄全部都選擇出來,再去進行分頁處理,那麼就會多多的浪費掉許多的系統處理時間。為了能夠把效率提高,所以現在我們就只選擇我們需要的資料,減少資料庫的...

三種資料庫利用SQL語句進行高效果分頁

在程式開發中,處理分頁往往是比較頻繁的事件,因為現在軟體基本上都是與資料庫進行掛釣的。但效率又是我們所追求的,如果是像原來那樣把所有滿足條件的記錄全部都選擇出來,再去進行分頁處理,那麼就會多多的浪費掉許多的系統處理時間。為了能夠把效率提高,所以現在我們就只選擇我們需要的資料,減少資料庫的處理時間,以...

三種資料庫sql分頁查詢

關於sql分頁 今天用到分頁了順便就總結了一下 mysql 資料庫 mysql 中有分頁的關鍵字limit,它可以實現分頁很簡單 select from sys user order by userid limit startno,total startno 是查詢開始的行數,total 是要查詢出...