對於常常要查的結果集,返回少量記錄,server端是能夠快取的,結果集儲存在共享池中,假設是繫結變數,繫結變數的值也要一樣。
SQL> show parameter result_cache
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
client_result_cache_lag big integer 3000
client_result_cache_size big integer 0
result_cache_max_result integer 5
result_cache_max_size big integer 33440K
result_cache_mode string manual
result_cache_remote_expiration integer 0
--result_cache_max_result 指定不論什麼單個結果集能夠使用result_cache_max_size的大小(單位為百分比),默覺得5,同意從1到100的值,超過這個限制的結果集會被雙色至為無效。
--result_cache_max_size 指定用來作為結果快取的共享池記憶體的大小,假設被設定為0,表示這個特性被禁用。
--result_cache_mode 假設設定為MANUAL(這也是預設情況),僅僅有指定hint result_cache的時候才幹使用結果快取;當為force的時候,全部不包括hint no_result_cache的查詢語句都會使用結果快取,查詢第二次即生效;當為auto時,在11g下執行相同的SQL第三次,快取才起作用。
--result_cache_remote_expiration 快取遠端物件的有效期(單位為分鐘),由於基於遠端物件的結果集無法由於遠端物件的變更而自己主動地變為無效,通常默覺得0,這意味著基於遠端物件的查詢結果的快取是被禁止的。
--result_cache_max_result和result_cache_max_size是系統級別的設定,result_cache_mode和result_cache_remote_expiration能夠在會話級別改動。
SQL> alter
system set result_cache_mode=force
SQL>
SELECT COUNT(1)
2 FROM GG_DISTRIBUTION W
3 WHERE W.DATA_AREA LIKE '03' || '%'
4 AND W.CREATE_DATE > TO_DATE('2013-01-01', 'yyyy-GG-dd');
已用時間: 00: 00: 22.48
執行計劃
----------------------------------------------------------
Plan hash value: 3923546474
-------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
-------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 106K (1)| 00:24:46 | | |
| 1 | SORT AGGREGATE | | 1 | 14 | | | | |
| 2 | PARTITION RANGE ALL | | 2173K| 29M| 106K (1)| 00:24:46 | 1 | 2 |
| 3 | PARTITION LIST ITERATOR| | 2173K| 29M| 106K (1)| 00:24:46 | KEY | KEY |
|* 4 | TABLE ACCESS FULL | GG_DISTRIBUTION | 2173K| 29M| 106K (1)| 00:24:46 | 1 | 48 |
-------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("W"."CREATE_DATE">TO_DATE(' 2013-01-01 00:00:00', 'syyyy-GG-dd hh24:mi:ss') AND
"W"."DATA_AREA" LIKE '03%')
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
280123 consistent gets
263679 physical reads
0 redo size
339 bytes sent via SQL*Net to client
337 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> /
已用時間: 00: 00: 00.11
執行計劃
----------------------------------------------------------
Plan hash value: 3923546474
-------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
-------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 106K (1)| 00:24:46 | | |
| 1 | RESULT CACHE | 0mr1089p1wxv3919raqyvtwtsv | | | | | | |
| 2 | SORT AGGREGATE | | 1 | 14 | | | | |
| 3 | PARTITION RANGE ALL | | 2173K| 29M| 106K (1)| 00:24:46 | 1 | 2 |
| 4 | PARTITION LIST ITERATOR| | 2173K| 29M| 106K (1)| 00:24:46 | KEY | KEY |
|* 5 | TABLE ACCESS FULL | GG_DISTRIBUTION | 2173K| 29M| 106K (1)| 00:24:46 | 1 | 48 |
-------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - filter("W"."CREATE_DATE">TO_DATE(' 2013-01-01 00:00:00', 'syyyy-GG-dd hh24:mi:ss') AND "W"."DATA_AREA"
LIKE '03%')
Result Cache Information (identified by operation id):
-----------------------------------------------------
1 - column-count=1; dependencies=(LCAM_TEST.GG_DISTRIBUTION); attributes=(single-row); parameters=(nls); name="SELECT COUNT(1)
FROM GG_DISTRIBUTION W
WHERE W.DATA_AREA LIKE '03' || '%'
AND W.CREATE_DATE > TO_DATE('2013-01-01', 'yyyy-"
統計資訊
----------------------------------------------------------
1 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
339 bytes sent via SQL*Net to client
337 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
能夠依據RESULT CACHE=0mr1089p1wxv3919raqyvtwtsv找到其快取的資訊。
SQL>
select s.STATUS,
s.CREATION_TIMESTAMP,
s.BUILD_TIME,
s.ROW_COUNT,
s.SCAN_COUNT
from v$result_cache_objects s
where cache_id = '0mr1089p1wxv3919raqyvtwtsv';
STATUS CREATION_TIMES BUILD_TIME ROW_COUNT SCAN_COUNT
--------- -------------- ---------- ---------- ----------
Published 20-6月 -14 700 1 26
結果快取的限制:
當查詢語句使用非確定性函式、序列號和暫時表的時候不能被快取。
查詢語句可能會造成資料不一致的時候。
引用到資料字典檢視的查詢語句不能快取。
能夠使用dbms_result_cache管理快取。