日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Oracle 11g新特性:Result Cache

發布時間:2024/8/26 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle 11g新特性:Result Cache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

結果集緩存(Result Cache)是Oracle Database 11g新引入的功能,除了可以在服務器端緩存結果集(Server?Result Cache)之外,還可以在客戶端緩存結果集(Client Result Cache)。下面著重介紹一下服務器端結果集緩存。

服務器端的Result Cache Memorey由兩部分組成。
·SQL?Query Result Cache:存儲SQL查詢的結果集。
·PL/SQL Function Result Cache:用于存儲PL/SQL函數的結果集。

Oracle 通過一個新引入的初始化參數result_cache_max_size來控制該Cache的大小。如果result_cache_max_size=0則表示禁用該特性。參數result_cache_max_result則控制單個緩存結果可以占總的Server Result Cache大小的百分比。

tq@CCDB> select * from v$version where rownum < 2;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production

tq@CCDB> 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????????? 2080K
result_cache_mode??????????????????? string?????????????? MANUAL
result_cache_remote_expiration?????? integer????????????? 0

上面顯示的參數中result_cache_mode用于控制Server Result cache的模式,該參數有3個可選設置。
·設置auto:則優化器會自動判斷是否將查詢結果緩存。
·設置manual:則需要通過查詢提示result_cache來告訴優化器是否緩存結果。
·設置force:則盡可能地緩存查詢結果(通過提示no_result_cache可以拒絕緩存)。

下面通過測試來看一下這一新特性的使用及優勢所在,首先創建一張測試表:

tq@CCDB> create table dbtan as select * from dba_objects;
Table created.

在以前的版本中,第一次執行該SQL可以看到consistent gets和physical reads大致相同:

tq@CCDB> set autotrace on
tq@CCDB> select count(*) from dbtan;
? COUNT(*)
----------
???? 70439
Statistics
----------------------------------------------------------
???????? 28? recursive calls
????????? 0? db block gets
??????1119? consistent gets
?????? 1036? physical reads

再次執行同樣查詢時,由于數據Cache在內存中,physical reads會減少到0,但是consistent gets很難降低:

tq@CCDB> select count(*) from dbtan;
? COUNT(*)
----------
???? 70439
Statistics
----------------------------------------------------------
????????? 0? recursive calls
????????? 0? db block gets
?????? 1041? consistent gets
????????? 0? physical reads

現在再來看看在Server Result Cache下Oracle的行為,首先在result_cache_mode參數設置為MANUAL時:

tq@CCDB> show parameter result_cache_mode
NAME???????????????????????????????? TYPE???????????????? VALUE
------------------------------------ -------------------- -----------------
result_cache_mode??????????????????? string?????????????? MANUAL

需要在SQL語句中手工指定Cache,這需要通過加入一個hints來實現,這個hints是result_cache

tq@CCDB> select/*+ result_cache */count(*) from dbtan;
? COUNT(*)
----------
???? 70439
Execution Plan
----------------------------------------------------------
Plan hash value: 1782547706

------------------------------------------------------------------------------------------
| Id? | Operation?????????? | Name?????????????????????? | Rows? | Cost (%CPU)| Time???? |
------------------------------------------------------------------------------------------
|?? 0 | SELECT STATEMENT??? |??????????????????????????? |???? 1 |?? 290?? (1)| 00:00:04 |
|?? 1 |? RESULT CACHE?????? |g6tx53yfbxr2fah44y3vvdp4hc|?????? |??????????? |????????? |
|?? 2 |?? SORT AGGREGATE??? |??????????????????????????? |???? 1 |??????????? |????????? |
|?? 3 |??? TABLE ACCESS FULL| DBTAN????????????????????? | 78689 |?? 290?? (1)| 00:00:04 |
------------------------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------
?? 1 - column-count=1; dependencies=(TQ.DBTAN); attributes=(single-row); name="select/*+ result_cache */count(*) from dbtan"
Statistics
----------------------------------------------------------
????????? 4? recursive calls
????????? 0? db block gets
?????? 1117? consistent gets
????????? 0? physical reads

注意到這個執行計劃已經和以往的不同,RESULT CACHE以g6tx53yfbxr2fah44y3vvdp4hc名稱創建。那么在接下來的查詢中,這個Result Cache就可以被利用:

tq@CCDB> select /*+ result_cache */ count(*) from dbtan;
? COUNT(*)
----------
???? 70439
Execution Plan
----------------------------------------------------------
Plan hash value: 1782547706

------------------------------------------------------------------------------------------
| Id? | Operation?????????? | Name?????????????????????? | Rows? | Cost (%CPU)| Time???? |
------------------------------------------------------------------------------------------
|?? 0 | SELECT STATEMENT??? |??????????????????????????? |???? 1 |?? 290?? (1)| 00:00:04 |
|?? 1 |? RESULT CACHE?????? |g6tx53yfbxr2fah44y3vvdp4hc|?????? |??????????? |????????? |
|?? 2 |?? SORT AGGREGATE??? |??????????????????????????? |???? 1 |??????????? |????????? |
|?? 3 |??? TABLE ACCESS FULL| DBTAN????????????????????? | 78689 |?? 290?? (1)| 00:00:04 |
------------------------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------

?? 1 - column-count=1; dependencies=(TQ.DBTAN); attributes=(single-row); name="select /*+ result_cache */ count(*) from dbtan"

Note
-----
?? - dynamic sampling used for this statement
Statistics
----------------------------------------------------------
????????? 0? recursive calls
????????? 0? db block gets
????????? 0? consistent gets
????????? 0? physical reads

????????? 0? redo size

在這個利用到Result Cache的查詢中,consistent gets減少到0,直接訪問結果集,不再需要執行SQL查詢。這就是Result Cache的強大之處。

在以上的測試中,當result_cache_mode設置為MANUAL時,只有使用hints的情況下,Oracle才會利用緩存結果集;而如果將result_cache_mode設置為AUTO,Oracle如果發現緩沖結果集已經存在,那么就會自動使用。但是如果緩沖結果集不存在,Oracle并不會自動進行緩沖,只有使用HINTS的情況下,Oracle才會將執行的結果集緩存。

可以通過查詢v$result_cache_memory視圖來看Cache的使用情況:

tq@CCDB> select * from v$result_cache_memory where free='NO';

??????? ID????? CHUNK???? OFFSET FREE??? OBJECT_ID?? POSITION
---------- ---------- ---------- ------ ---------- ----------
???????? 0????????? 0????????? 0 NO????????????? 0????????? 0
???????? 1????????? 0????????? 1 NO????????????? 1????????? 0
???????? 2????????? 0????????? 2 NO????????????? 2????????? 0
???????? 3????????? 0????????? 3 NO????????????? 3????????? 0

通過V$RESULT_CACHE_STATISTICS可以查詢Result Cache的統計信息:

tq@CCDB> select * from v$result_cache_statistics;

??????? ID NAME???????????????????????????????????????????? VALUE
---------- ---------------------------------------- -------------
???????? 1 Block Size (Bytes)??????????????????????????????? 1024
???????? 2 Block Count Maximum?????????????????????????????? 2080
???????? 3 Block Count Current???????????????????????????????? 32
???????? 4 Result Size Maximum (Blocks)?????????????????????? 104
???????? 5 Create Count Success???????????????????????????????? 2
???????? 6 Create Count Failure???????????????????????????????? 0
???????? 7 Find Count?????????????????????????????????????????? 2
???????? 8 Invalidation Count?????????????????????????????????? 1
???????? 9 Delete Count Invalid???????????????????????????????? 0
??????? 10 Delete Count Valid?????????????????????????????????? 0

V$RESULT_CACHE_OBJECTS記錄了Cache的對象:

tq@CCDB> select id,type,name,block_count,row_count from v$result_cache_objects;

??????? ID TYPE??????????? NAME?????????????????????????????????????????? BLOCK_COUNT? ROW_COUNT
---------- --------------- ---------------------------------------------- ----------- ----------
???????? 2 Dependency????? TQ.DBTAN???????????????????????????????????????????????? 1????????? 0
???????? 0 Dependency????? object-id(98106)???????????????????????????????????????? 1????????? 0
???????? 3 Result????????? select /*+ result_cache */ count(*) from dbtan?????????? 1????????? 1
???????? 1 Result????????? select /*+ result_cache */ count(*) from t?????????????? 1????????? 1

一個新的系統包被引入,DBMS_RESULT_CACHE可以用于執行關于Result Cache的管理

tq@CCDB> set serveroutput on
tq@CCDB> 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? = 2080K bytes (2080 blocks)
Maximum Result Size = 104K bytes (104 blocks)
[Memory]
Total Memory = 169352 bytes [0.045% of the Shared Pool]
... Fixed Memory = 5296 bytes [0.001% of the Shared Pool]
... Dynamic Memory = 164056 bytes [0.044% of the Shared Pool]
....... verhead = 131288 bytes
....... Cache Memory = 32K bytes (32 blocks)
........... Unused Memory = 28 blocks
........... Used Memory = 4 blocks
............... Dependencies = 2 blocks (2 count)
............... Results = 2 blocks
................... SQL???? = 1 blocks (1 count)
................... Invalid = 1 blocks (1 count)

PL/SQL procedure successfully completed.


總結

以上是生活随笔為你收集整理的Oracle 11g新特性:Result Cache的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。