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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle優化案例-單表分頁語句的優化(八)Oracle優化
- 分頁查詢優化優化
- MySQL分頁查詢優化MySql優化
- 記一次分頁優化優化
- Web 頁面優化專項 > Lighthouse > 效能分數優化Web優化
- Oracle最佳化之單表分頁最佳化Oracle
- MySQL分優化之超大頁查詢MySql優化
- SQL優化案例-單表分頁語句的優化(八)SQL優化
- Oracle的特性分頁Oracle
- oracle 分頁寫法Oracle
- Python如何優化列表介面進行分頁Python優化
- 一次簡單的分頁優化優化
- Oracle優化案例-分頁語句返回資料順序不一致(十一)Oracle優化
- oracle優化Oracle優化
- Oracle Rownum分頁改寫Oracle
- oracle EM 優化Oracle優化
- oracle 效能優化Oracle優化
- oracle優化技巧Oracle優化
- mysql count函式與分頁功能極限優化MySql函式優化
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- Oracle效能優化-SQL優化(案例一)Oracle優化SQL
- Oracle效能優化-SQL優化(案例二)Oracle優化SQL
- Oracle效能優化-SQL優化(案例三)Oracle優化SQL
- Oracle效能優化-SQL優化(案例四)Oracle優化SQL
- Apache網頁優化與安全優化Apache網頁優化
- [20230103]COUNT STOPKEY operation.txtTopK
- 資料量很大,分頁查詢很慢,該怎麼優化?優化
- Oracle in 查詢優化Oracle優化
- Oracle優化的方法Oracle優化
- Oracle優化案例-使用with as優化Subquery Unnesting(七)Oracle優化
- apache網頁優化Apache網頁優化
- 網頁效能優化網頁優化
- 資料庫全表查詢之-分頁查詢優化資料庫優化
- MySQL 海量資料的 5 種分頁方法和優化技巧MySql優化
- Oracle 效能優化-expdp備份速度優化03Oracle優化
- Oracle 效能優化-expdp備份速度優化02Oracle優化
- Oracle優化案例-(三十四)Oracle優化
- Oracle 儲存過程分頁 + Sqlsugar呼叫Oracle儲存過程SqlSugar
- MySql/Oracle和SQL Server的分頁查MySqlOracleServer