[email protected]> select * from v$version where rownum < 2;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
[email protected]> select count(1) from "9959".tbcard;
COUNT(1)
----------
2140321
已用时间: 00: 00: 01.36
执行计划
----------------------------------------------------------
Plan hash value: 113828700
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2948 (1)| 00:00:36 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_TBCARD | 2140K| 2948 (1)| 00:00:36 |
---------------------------------------------------------------------------
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
10921 consistent gets
10107 physical reads
0 redo size
346 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
COUNT(1)
----------
2140321
已用时间: 00: 00: 00.17
执行计划
----------------------------------------------------------
Plan hash value: 113828700
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2948 (1)| 00:00:36 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_TBCARD | 2140K| 2948 (1)| 00:00:36 |
---------------------------------------------------------------------------
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
10921 consistent gets
0 physical reads
0 redo size
346 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
COUNT(1)
----------
2140321
已用时间: 00: 00: 00.16
执行计划
----------------------------------------------------------
Plan hash value: 113828700
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2948 (1)| 00:00:36 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_TBCARD | 2140K| 2948 (1)| 00:00:36 |
---------------------------------------------------------------------------
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
10921 consistent gets
0 physical reads
0 redo size
346 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
[email protected]>
[email protected]> select /*+ result_cache */ count(*) from "9959".tbcard;
COUNT(*)
----------
2140321
已用时间: 00: 00: 00.18
执行计划
----------------------------------------------------------
Plan hash value: 113828700
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2948 (1)| 00:00:36 |
| 1 | RESULT CACHE | b1b507vy3w6x324m6t00cbugmw | | | |
| 2 | SORT AGGREGATE | | 1 | | |
| 3 | INDEX FAST FULL SCAN| PK_TBCARD | 2140K| 2948 (1)| 00:00:36 |
---------------------------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------
1 - column-count=1; dependencies=(9959.TBCARD); attributes=(single-row); name="select /*+ result_cache */ count(*) from "9959".tbcard"
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
10921 consistent gets
0 physical reads
0 redo size
346 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
[email protected]> select /*+ result_cache */ count(*) from "9959".tbcard;
COUNT(*)
----------
2140321
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 113828700
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2948 (1)| 00:00:36 |
| 1 | RESULT CACHE | b1b507vy3w6x324m6t00cbugmw | | | |
| 2 | SORT AGGREGATE | | 1 | | |
| 3 | INDEX FAST FULL SCAN| PK_TBCARD | 2140K| 2948 (1)| 00:00:36 |
---------------------------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------
1 - column-count=1; dependencies=(9959.TBCARD); attributes=(single-row); name="select /*+ result_cache */ count(*) from "9959".tbcard"
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
346 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
[email protected]>
在这个利用到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的使用情况:
[email protected]> select * from v$result_cache_memory where free='NO';
ID CHUNK OFFSET FRE OBJECT_ID POSITION
---------- ---------- ---------- --- ---------- ----------
0 0 0 NO 0 0
1 0 1 NO 1 0
已用时间: 00: 00: 00.01
通过V$RESULT_CACHE_STATISTICS可以查询Result Cache的统计信息:
[email protected]> select * from v$result_cache_statistics;
ID NAME VALUE
---------- -------------------------------------------------------------------------------------------------------------------------------- ----------
1 Block Size (Bytes) 1024
2 Block Count Maximum 5280
3 Block Count Current 32
4 Result Size Maximum (Blocks) 264
5 Create Count Success 1
6 Create Count Failure 0
7 Find Count 1
8 Invalidation Count 0
9 Delete Count Invalid 0
10 Delete Count Valid 0
已选择10行。
已用时间: 00: 00: 00.02
[email protected]>
V$RESULT_CACHE_OBJECTS记录了Cache的对象:
[email protected]> select id,type,name,block_count,row_count from v$result_cache_objects;
ID TYPE NAME BLOCK_COUNT ROW_COUNT
0 Dependency 9959.TBCARD 1 0
1 Result select /*+ result_cache */ count(*) from "9959".tbcard 1 1
已用时间: 00: 00: 00.01
DBMS_RESULT_CACHE可以用于执行关于Result Cache的管理:
[email protected]> 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 = 5280K bytes (5280 blocks)
Maximum Result Size = 264K bytes (264 blocks)
[Memory]
Total Memory = 155248 bytes [0.093% of the Shared Pool]
... Fixed Memory = 2400 bytes [0.001% of the Shared Pool]
... Dynamic Memory = 152848 bytes [0.091% of the Shared Pool]
....... Overhead = 120080 bytes
....... Cache Memory = 32K bytes (32 blocks)
........... Unused Memory = 30 blocks
........... Used Memory = 2 blocks
............... Dependencies = 1 blocks (1 count)
............... Results = 1 blocks
................... SQL = 1 blocks (1 count)
PL/SQL 过程已成功完成。
已用时间: 00: 00: 00.03
[email protected]>
[email protected]> show parameter result
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 5280K
result_cache_mode string MANUAL
result_cache_remote_expiration integer 0
[email protected]>