資料庫分頁查詢總結

2021-09-01 08:23:41 字數 892 閱讀 7795

公司無聊的情況下,總結下這段時間面試中遇到到一些問題,先來說資料的分頁查詢:

oracle分頁

select * from (

select a.*, rownum, rn from (

select * from table ) a

where rownum <= 40 )

where rn >= 20

mysql 分頁

1、一般最基本的分頁方式:

select * from table order by id desc limit 0, 10

在中小型的資料訪問中足夠使用,但是如果海量資料的話,會有問題,比如超過1000000條資料的話,語句就回變為:

select * from table order by id desc limit 100000000, 10;

就是越往後分頁,limit語句的偏移量就會越大,速度也會明顯變慢。

2、使用子查詢的方式來提高分頁查詢效率

select * from table where id <=

(select id from table order by id desc limit ".($page-1)*$pagesize.", 1)

order by id desc limit $pagesize

3、使用join

select * from table as t1 join

(select id from table order by id desc limit ".($page-1)*$pagesize.", 1) as t2

where t1.id <= t2.id order by t1.id desc limit $pagesize;

都是網路上的資源,搜蒐會找到,總結下,找著方便

資料庫查詢分頁。

csdn上推薦的,轉過來的。呵呵!表中主鍵必須為標識列,id int identity 1,1 1.分頁方案一 利用not in和select top分頁 語句形式 select top 頁記錄數量 from 表名 where id not in select top 每頁行數 頁數 1 id fr...

資料庫分頁查詢

資料庫分頁查詢 在這裡主要講解一下mysql sqlserver2000 及sqlserver2005 和orcale三種資料庫實現分頁查詢的方法。可能會有人說這些網上都有,但我的主要目的是把這些知識通過我實際的應用總結歸納一下,以方便大家查詢使用。下面就分別給大家介紹 講解一下三種資料庫實現分頁查...

資料庫分頁查詢

1 mysql select from demo where 1 1 limit 2,3limit是用來分頁的,第乙個引數是行號,第二個引數是說有多少行 2 oracle 第一種select id,field name,from table name where id in select id fr...