oracle 分頁優化(stopkey)
stopkey技術是一種預測技術,完全是為了適應大量資料的出現而出現的
從名稱就可以看出,stopkey就是在關鍵位置上停止,終止的技術,oracle這裡指的就是不再繼續讀取剩餘的資料.
======================================================
1.建立測試表
create table test_stopkey as
select * from dba_objects;
2.準備測試資料
insert into test_stopkey
select * from dba_objects;
commit;
SQL> select count(*) from test_stopkey;
COUNT(*)
----------
200476
3.測試,提取從1000條到2000條資料
第一種情況
select * from
(select rownum rn,t.* from test_stopkey t)
where rn > 1000 and rn <= 2000;
已用時間: 00: 00: 07.64
執行計劃
----------------------------------------------------------
Plan hash value: 594950436
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 199K| 36M| 615 (1)| 00:00:08 |
|* 1 | VIEW | | 199K| 36M| 615 (1)| 00:00:08 |
| 2 | COUNT | | | | | |
| 3 | TABLE ACCESS FULL| TEST_STOPKEY | 199K| 33M| 615 (1)| 00:00:08 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN"<=2000 AND "RN">1000)
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
7 recursive calls
0 db block gets
2975 consistent gets
0 physical reads
0 redo size
54486 bytes sent via SQL*Net to client
1111 bytes received via SQL*Net from client
68 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1000 rows processed
-----------------------------------------------------------------------------------------------------------------------------------
第二種情況
select * from
(select rownum rn,t.* from test_stopkey t
where rownum <= 2000)
where rn > 1000;
已用時間: 00: 00: 07.35
執行計劃
----------------------------------------------------------
Plan hash value: 3382644055
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 371K| 11 (28)| 00:00:01 |
|* 1 | VIEW | | 2000 | 371K| 11 (28)| 00:00:01 |
|* 2 | COUNT STOPKEY | | | | | |
| 3 | TABLE ACCESS FULL| TEST_STOPKEY | 199K| 33M| 11 (28)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN">1000)
2 - filter(ROWNUM<=2000)
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
7 recursive calls
0 db block gets
233 consistent gets
0 physical reads
0 redo size
54486 bytes sent via SQL*Net to client
1111 bytes received via SQL*Net from client
68 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1000 rows processed
第二種情況比第一種一致性讀大大減小,主要是把rownum引入到了第二層,注意執行計劃中的stopkey,專門是為了提取top n 的需求優化的。
再來句簡單的
select * from test_stopkey t where rownum < 5;
已用時間: 00: 00: 00.04
執行計劃
----------------------------------------------------------
Plan hash value: 4076471010
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 372 | 2 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_STOPKEY | 4 | 372 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM<5)
統計資訊
----------------------------------------------------------
1 recursive calls
0 db block gets
5 consistent gets
0 physical reads
0 redo size
1461 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
條件中有rownum存在,就會有stopkey優化,那越往後分頁是否速度就會一致性讀就會越大呢?
select * from
(select rownum rn,t.* from test_stopkey t
where rownum <= 150000)
where rn > 149000;
已用時間: 00: 00: 03.84
執行計劃
----------------------------------------------------------
Plan hash value: 3382644055
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 150K| 27M| 462 (1)| 00:00:06 |
|* 1 | VIEW | | 150K| 27M| 462 (1)| 00:00:06 |
|* 2 | COUNT STOPKEY | | | | | |
| 3 | TABLE ACCESS FULL| TEST_STOPKEY | 150K| 13M| 462 (1)| 00:00:06 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN">149000)
2 - filter(ROWNUM<=150000)
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
2126 consistent gets
0 physical reads
0 redo size
58219 bytes sent via SQL*Net to client
1111 bytes received via SQL*Net from client
68 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1000 rows processed
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26224278/viewspace-1808926/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用索引優化StopKey索引優化TopK
- SQL優化:limit分頁優化SQL優化MIT
- Oracle優化案例-單表分頁語句的優化(八)Oracle優化
- 分頁查詢優化優化
- MySQL分頁查詢優化MySql優化
- 優化MySQL中的分頁優化MySql
- 優化 MySQL 中的分頁優化MySql
- MySQL分頁效能優化指南MySql優化
- Oracle rownum 分頁引起的效率問題及優化思路Oracle優化
- 記一次分頁優化優化
- MySQL分優化之超大頁查詢MySql優化
- SQL優化案例-單表分頁語句的優化(八)SQL優化
- Web 頁面優化專項 > Lighthouse > 效能分數優化Web優化
- 一次簡單的分頁優化優化
- 關於分頁查詢的優化思路優化
- 一次分頁查詢的優化優化
- SORT (UNIQUE STOPKEY)/ SORT GROUP BY STOPKEYTopK
- SQL Server的分頁優化及Row_Number()分頁存在的問題SQLServer優化
- oracle分頁Oracle
- Python如何優化列表介面進行分頁Python優化
- Oracle最佳化之單表分頁最佳化Oracle
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- mysql count函式與分頁功能極限優化MySql函式優化
- mysql大資料量分頁查詢方法及其優化MySql大資料優化
- Oracle優化案例-分頁語句返回資料順序不一致(十一)Oracle優化
- Apache網頁優化與安全優化Apache網頁優化
- 資料量很大,分頁查詢很慢,該怎麼優化?優化
- MySQL單表百萬資料記錄分頁效能優化MySql優化
- oracle 分頁寫法Oracle
- Oracle的特性分頁Oracle
- Oracle 分頁方法研究Oracle
- oracle分頁查詢Oracle
- oracle 分頁問題Oracle
- 網頁效能優化網頁優化
- apache網頁優化Apache網頁優化
- MySQL 海量資料的 5 種分頁方法和優化技巧MySql優化
- 資料庫全表查詢之-分頁查詢優化資料庫優化
- oracle優化Oracle優化