1 相關參數
RESULT_CACHE_MAX_RESULT:指定任一結果可以使用的最大高速緩存量,默認值爲5%,但可指定1 到100 之間的任一百分比值,可在系統和會話級別上實施此參數;
result_cache_max_size :32k的整數倍,若是將結果高速緩存的值設爲0,則會禁用此結果高速緩存,不能超過共享池的75%;
RESULT_CACHE_REMOTE_EXPIRATION:能夠指定依賴於遠程數據庫對象的結果保持有效的時間(以分鐘爲單位),默認值爲0,表示不會使用高速緩存使用遠程對象的結果;
result_cache_mode :默認爲 MANUAL,即加hint /+ result_cache / 才使用result cache
啓用result cache:數據庫
alter system set result_cache_max_result=5; alter system set result_cache_max_size=20m;
2 相關存儲過程
查詢內存分配狀況:緩存
SQL>set serveroutput on ; SQL> exec dbms_result_cache.memory_report R e s u l t C a c h e M e m o r y R e p o r t [Parameters] Block Size = 1K bytes Maximum Cache Size = 1280K bytes (1280 blocks) Maximum Result Size = 64K bytes (64 blocks) [Memory] Total Memory = 202160 bytes [0.110% of the Shared Pool] ... Fixed Memory = 5352 bytes [0.003% of the Shared Pool] ... Dynamic Memory = 196808 bytes [0.107% of the Shared Pool] ....... Overhead = 131272 bytes ....... Cache Memory = 64K bytes (64 blocks) ........... Unused Memory = 29 blocks ........... Used Memory = 35 blocks ............... Dependencies = 10 blocks (10 count) ............... Results = 25 blocks ................... SQL = 20 blocks (20 count) ................... Invalid = 5 blocks (5 count) SQL> select dbms_result_cache.status from dual; ENABLED
刪除全部現有結果並清空高速緩存:EXECUTE DBMS_RESULT_CACHE.FLUSH;
檢查:select * from GV$RESULT_CACHE_OBJECTS
使特定結果失效:ide
begin DBMS_RESULT_CACHE.INVALIDATE('SH','SALES'); end;
select cache_id,lru_number,db_link,status,bucket_no,hash,name,namespace,type,status from GV$RESULT_CACHE_OBJECTS where name like '%sales%';
CACHE_ID LRU_NUMBER DB_LINK STATUS BUCKET_NO HASH NAME NAMESPACE TYPE STATUS 3gqafv8xzpk9t535y6dgfmyhjt 0 No Invalid 2055 571566087 "select /*+ result_cache */ prod_id,sum(amount_sold) from sh.sales group by prod_id order by prod_id " SQL Result** Invalid**
3 執行計劃查看SQL> select /*+ result_cache */ prod_id,sum(amount_sold) from sales group by prod_id order by prod_id;
Execution Plan
spa
Plan hash value: 4109827725 -------------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 72 | 648 | 680 (24)| 00:00:09 | | | | 1 | RESULT CACHE | g23n3fafz6vxs65351bmca3jq8 | | | | | | | | 2 | SORT GROUP BY | | 72 | 648 | 680 (24)| 00:00:09 | | | | 3 | PARTITION RANGE ALL| | 918K| 8075K| 557 (8)| 00:00:07 | 1 | 28 | | 4 | TABLE ACCESS FULL | SALES | 918K| 8075K| 557 (8)| 00:00:07 | 1 | 28 |
Result Cache Information (identified by operation id):
1 - column-count=2; dependencies=(SH.SALES); name="select /*+ result_cache */ prod_id,sum(amount_sold) from sales group by prod_id order by prod_id"
能夠看出爲result cache;不使用result cache:code
select prod_id,sum(amount_sold) from sales group by prod_id order by prod_id; Execution Plan ---------------------------------------------------------- Plan hash value: 4109827725 ---------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | ---------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 72 | 648 | 680 (24)| 00:00:09 | | | | 1 | SORT GROUP BY | | 72 | 648 | 680 (24)| 00:00:09 | | | | 2 | PARTITION RANGE ALL| | 918K| 8075K| 557 (8)| 00:00:07 | 1 | 28 | | 3 | TABLE ACCESS FULL | SALES | 918K| 8075K| 557 (8)| 00:00:07 | 1 | 28 |
也能夠強制對象使用result cache:alter table sales result_cache(mode force);
取消:alter table sales result_cache(mode default);
4 相關視圖
(G)V$RESULT_CACHE_STATISTICS : 列出各類高速緩存設置和內存使用量統計信息
select from V$RESULT_CACHE_STATISTICS
(G)V$RESULT_CACHE_MEMORY : 列出全部內存塊和相應的統計信息
select from V$RESULT_CACHE_MEMORY
(G)V$RESULT_CACHE_OBJECTS: 列出全部對象(高速緩存結果和依賴性)及其屬性
select cache_id,lru_number,db_link,status,bucket_no,hash,name,namespace,type,status
from GV$RESULT_CACHE_OBJECTS where name like '%sales%';
(G)V$RESULT_CACHE_DEPENDENCY: 列出高速緩存結果之間的依賴性詳細信息及依賴性
select b.owner,b.object_name,a.* from V$RESULT_CACHE_DEPENDENCY a,all_objects b where a.object_no=b.object_id;orm
5 注意事項
1 綁定變量不一樣,不能夠命中
2 最適用於須要訪問大量行卻僅返回其中一少部分的語句,建議使用在OLAP系統/報表系統中使用server