oracle存儲過程分頁
Ⅰ Oracle 數據量非常大(上億)時,使用存儲過程中的游標返回分頁查詢的10條記錄非常耗時,請問如何優化
select * /*+ FIRST_ROWS */from XXX where XXX
提高sql語句的響應時間,快速的先返回 n 行。
SELECT /*+ FIRST_ROWS */ * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM <= 40
)
WHERE RN >= 21
Ⅱ 求一個通用ORACLE存儲過程,實現分頁和查詢
create
or
replace
package
Tools
is
type
ResultData
is
ref
cursor;
procere
sp_Page(p_PageSize
int,
--每頁記錄數
p_PageNo
int,
--當前頁碼,從
1
開始
p_SqlSelect
varchar2,
--查詢語句,含排序部分
p_SqlCount
varchar2,
--獲取記錄總數的查詢語句
p_OutRecordCount
out
int,--返回總記錄數
p_OutCursor
out
ResultData);
end
Tools;
create
or
replace
package
body
Tools
is
procere
sp_Page(p_PageSize
int,
--每頁記錄數
p_PageNo
int,
--當前頁碼,從
1
開始
p_SqlSelect
varchar2,
--查詢語句,含排序部分
p_SqlCount
varchar2,
--獲取記錄總數的查詢語句
p_OutRecordCount
out
int,--返回總記錄數
p_OutCursor
out
ResultData)
as
v_sql
varchar2(3000);
v_count
int;
v_heiRownum
int;
v_lowRownum
int;
begin
----取記錄總數
execute
immediate
p_SqlCount
into
v_count;
p_OutRecordCount
:=
v_count;
----執行分頁查詢
v_heiRownum
:=
p_PageNo
*
p_PageSize;
v_lowRownum
:=
v_heiRownum
-
p_PageSize
+1;
v_sql
:=
'SELECT
*
FROM
(
SELECT
A.*,
rownum
rn
FROM
('||
p_SqlSelect
||')
A
WHERE
rownum
<=
'||
to_char(v_heiRownum)
||
'
)
B
WHERE
rn
>=
'
||
to_char(v_lowRownum)
;
--注意對rownum別名的使用,第一次直接用rownum,第二次一定要用別名rn
OPEN
p_OutCursor
FOR
v_sql;
end
sp_Page;
end
Tools;
Ⅲ oracle用游標分頁查詢學生所有信息
Oracle內分頁:
1.首先創建返回結果集的包,代碼如下:
--創建包
create or replace package types as
type cursorType is ref cursor;
end;12341234
2.創建實現分頁查詢的存儲過程:
--創建存儲過程
CREATE OR REPLACE PROCEDURE PROC_GET_DATA_PAGING(P_TABLENAME IN VARCHAR2, --表(視圖)名
P_STRWHERE IN VARCHAR2, --查詢條件
P_ORDERCOLUMN IN VARCHAR2, --排序的列
P_CURPAGE IN OUT NUMBER, --當前頁
P_PAGESIZE IN OUT NUMBER, --每頁顯示記錄條數
P_TOTALRECORDS OUT NUMBER, --總記錄數
P_TOTALPAGES OUT NUMBER, --總頁數
V_CUR OUT TYPES.CURSORTYPE) --返回的結果集
IS
V_SQL VARCHAR2(4000) := ''; --SQL語句
V_STARTRECORD NUMBER(10); --開始顯示的記錄條數
V_ENDRECORD NUMBER(10); --結束顯示的記錄條數
V_SHOWALL INTEGER; --是否顯示全部記錄
BEGIN
--記錄中總記錄條數
V_SQL := 'SELECT TO_NUMBER(COUNT(*)) FROM ' || P_TABLENAME ||
' WHERE 1=1 ';
IF P_STRWHERE IS NOT NULL OR P_STRWHERE <> '' THEN
V_SQL := V_SQL || P_STRWHERE;
END IF;
EXECUTE IMMEDIATE V_SQL
INTO P_TOTALRECORDS;
--驗證頁面記錄大小
IF P_PAGESIZE <= 0 THEN
V_SHOWALL := 1;
P_PAGESIZE := 0;
END IF;
IF V_SHOWALL IS NULL THEN
--根據頁大小計算總頁數
IF MOD(P_TOTALRECORDS, P_PAGESIZE) = 0 THEN
P_TOTALPAGES := TRUNC(P_TOTALRECORDS / P_PAGESIZE, 0);
ELSE
P_TOTALPAGES := TRUNC(P_TOTALRECORDS / P_PAGESIZE, 0) + 1;
END IF;
ELSE
P_TOTALPAGES := 1;
END IF;
--驗證頁號
IF P_CURPAGE < 1 THEN
P_CURPAGE := 1;
END IF;
IF P_CURPAGE > P_TOTALPAGES THEN
P_CURPAGE := P_TOTALPAGES;
END IF;
--實現分頁查詢
V_STARTRECORD := (P_CURPAGE - 1) * P_PAGESIZE + 1;
V_ENDRECORD := P_CURPAGE * P_PAGESIZE;
V_SQL := 'SELECT * FROM (SELECT A.*, ROWNUM R FROM ' ||
'(SELECT * FROM ' || P_TABLENAME;
IF P_STRWHERE IS NOT NULL OR P_STRWHERE <> '' THEN
V_SQL := V_SQL || ' WHERE 1=1 ' || P_STRWHERE;
END IF;
IF P_ORDERCOLUMN IS NOT NULL OR P_ORDERCOLUMN <> '' THEN
V_SQL := V_SQL || ' ORDER BY ' || P_ORDERCOLUMN;
END IF;
IF V_SHOWALL IS NULL THEN
V_SQL := V_SQL || ') A WHERE ROWNUM <= ' || V_ENDRECORD ||
') B WHERE R >= ' || V_STARTRECORD;
ELSE
V_SQL := V_SQL || ') A ) B ';
END IF;
DBMS_OUTPUT.PUT_LINE(V_SQL);
OPEN V_CUR FOR V_SQL;
END PROC_GET_DATA_PAGING;0414204142
一定要先創建返回結果集的包,否則編譯存儲過程有問題。
Ⅳ asp連接oracle資料庫分頁問題
利用存儲過程分頁會好一些。
或者換一下分頁的SQL語句,盡量不使用ADO的相關屬性。
參考如下:
方案1
顯示從第n條到第m條記錄
select top m-n+1 * from table where id not in (select top n-1 from table order by id) order by id
思想:顯示從第n條到第m條記錄等價於 先篩選ID不等於前n-1條記錄ID的記錄,在從這些記錄中按ID排序,取m-n+1條記錄。
SQLserver實現:
顯示從第n條到第m條記錄===每頁容量(頁大小)
前n-1條記錄====頁容量*(頁碼-1)
select top 頁容量 * from table where id not in (select top 頁容量*(頁碼-1) from table order by id) order by id
當第1頁時
select top 頁容量 * from table
******************************************************************************************************************
方案2
顯示從第n條到第m條記錄
select top m-n+1 * from table where id >(select max(id) from (select top n-1 from table order by id) as t1) order by id
思想:先從前n-1條記錄中計算ID最大值,則所要顯示的記錄是滿足ID大於最大值的前m-n+1條記錄。
當n=1時,直接 select top m-1 * from table
SQLserver實現:
select top 頁容量 * from table where id >(select max(id) from (select top 頁容量*(頁碼-1)from table order by id) as t1) order by id
當第1頁時
select top 頁容量 * from table
方案3
顯示從第n條到第m條記錄
select * from (select top m-n+1 * from (select top m * from order by id asc) as t1 order by id desc) as t2 order by id asc
思想:先按ID升序(降序)取前m條記錄,再按ID降序(升序)取前m-n+1條記錄,最後從前m-n-1條記錄中取顯示的記錄
SQLserver實現:
select * from (select top 頁容量 * from (select top 頁容量*頁碼 * from order by id asc) as t1 order by id desc) as t2 order by id asc
Ⅳ oracle:寫一個用於分頁的存儲過程.調用的時候可以傳參
select
*
from
(select
a.*,rownum
r
from
(select
*
from
table_a)
a
where
rownum<=b)
where
r>=a
該sql語句實現了分頁查詢。
其中table_a表示你要查詢的那張表,r>=a,rownum<=b中的a和b表示需要查詢的記錄的起止數。
需要做分頁的話,上面的b可以改成currentPage*pageCount,a可以改成(currentPage-1)*pageCount,
currentPage表示當前頁數,pageCount表示總頁數
Ⅵ oracle做分頁查詢怎麼做
sql語句如下:
分頁1
SELECT *
FROM (Select ROWNUM AS ROWNO, T.*
from 表名 T(別名)
where 表欄位 between to_date('20060501', 'yyyymmdd') and to_date('20060731', 'yyyymmdd')
AND ROWNUM <= 20) TABLE_ALIAS
WHERE TABLE_ALIAS.ROWNO >= 10;
經過測試,此方法成本最低,只嵌套一層,速度最快,即使查詢的數據量再大,也幾乎不受影響,速度依然.
分頁2:
SELECT *
FROM (SELECT TT.*, ROWNUM AS ROWNO
FROM (Select t.*
from 表名 T(別名)
where flight_date between to_date('20060501', 'yyyymmdd') and
to_date('20060531', 'yyyymmdd')
ORDER BY FACT_UP_TIME, flight_no) TT(別名二)
WHERE ROWNUM <= 20) TABLE_ALIAS
where TABLE_ALIAS.rowno >= 10;
經過測試,此方法隨著查詢范圍的擴大,速度也會越來越慢,
Ⅶ Oracle多條件查詢的分頁存儲如何操作
項目接近尾聲了源碼天空,感覺將業務邏輯放到Oracle中使得後台代碼很精簡,Oracle很有搞頭!PL\SQL:create or replace procere proc_client_List --客戶Oracle多條件查詢(pro_cursor out pkg_order.p_cursor, --查詢結果集characters_ in varchar2,--客戶性質states_ in varchar2,--客戶狀態type_ in varchar2,--客戶類型calling_ in varchar2,--客戶行業name_ in varchar2,--客戶名稱beginTime_ in date,--創建日期上限endTime_ in date,--創建日期上限area_ in number,--客戶地區clientsource_ in varchar2,--客戶來源importent_ in varchar2,--重要程度start_row in number,--結果集起始行end_row in number--結果集結束行) is sql_str varchar2(1000):= 'select * from ( select row_.*, rownum rownum_ from ( select * from clientinfo c where(:characters_ is null or c.characters like :characters_) and (:states_ is null or c.states like :states_) and (:type_ is null or c.type like :type_) and (:calling_ is null or c.calling like :calling_) and (:name_ is null or c.name like :name_) and (:beginTime_ is null or c.createtime > :beginTime_) and (:endTime_ is null or c.createtime < :endTime_) and (:area_ is null or c.area=:area_) and (:clientsource_ is null or c.clientsource like :clientsource_) and (:importent_ is null or c.importent like :importent_) ) row_ where rownum :start_row'; begin open pro_cursor for sql_str using characters_,'%'||characters_||'%', states_,'%'||states_||'%', type_,'%'||type_||'%', calling_,'%'||calling_||'%', name_,'%'||name_||'%', beginTime_,beginTime_, endTime_,endTime_, area_,area_, clientsource_,'%'||clientsource_||'%', importent_,'%'||importent_||'%', end_row,start_row; end proc_client_List; 上述的相關內容就是對Oracle多條件查詢的分頁存儲過程的描述,希望會給你帶來一些幫助在此方面。