Oracle11gr2新增表的RESULT CACHE屬性

yangtingkun發表於2010-09-04

11.2中,Oracle新增了一個表的引數屬性RESULT_CACHE,設定是否將表的查詢結果放到RESULT CACHE中。

 

 

11.1中,Oracle引入了RESULT CACHE功能,設定RESULT CACHE可以透過HINT以及初始化引數兩種方式實現。但是使用初始化引數設定RESULT CACHE的覆蓋面太大,會導致一些不希望被CACHE的查詢也被放到RESULT CACHE中。而使用HINT方式的覆蓋面又太窄,只對指定的查詢有效,使用相對比較麻煩。

於是在11.2中,Oracle推出了表級的RESULT CACHE,當設定表的RESULT CACHE後,這個表的所有查詢都被放到RESULT CACHE緩衝區中:

SQL> SELECT * FROM V$VERSION;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

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 32M
result_cache_mode                    string      MANUAL
result_cache_remote_expiration       integer     0

SQL> CREATE TABLE T_CACHE
  2  RESULT_CACHE (MODE FORCE)
  3  AS SELECT *
  4  FROM ALL_OBJECTS;

表已建立。

SQL> SET AUTOT ON
SQL> SELECT COUNT(*) FROM T_CACHE;

  COUNT(*)
----------
     69432


執行計劃
----------------------------------------------------------
Plan hash value: 56254237

------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     1 |   196   (1)| 00:00:03 |
|   1 |  RESULT CACHE       | 563an9xyg23fyb37x3qwjkadfa |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71348 |   196   (1)| 00:00:03 |
------------------------------------------------------------------------------------------

Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=1; dependencies=(TEST.T_CACHE); attributes=(single-row); name="SELECT COUNT(*) FROM T_CACHE"

Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
         28  recursive calls
          0  db block gets
        577  consistent gets
        502  physical reads
          0  redo size
        529  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> SELECT COUNT(*) FROM T_CACHE;

  COUNT(*)
----------
     69432


執行計劃
----------------------------------------------------------
Plan hash value: 56254237

------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     1 |   196   (1)| 00:00:03 |
|   1 |  RESULT CACHE       | 563an9xyg23fyb37x3qwjkadfa |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71348 |   196   (1)| 00:00:03 |
------------------------------------------------------------------------------------------

Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=1; dependencies=(TEST.T_CACHE); attributes=(single-row); name="SELECT COUNT(*) FROM T_CACHE"

Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          0  consistent gets
          0  physical reads
          0  redo size
        529  bytes sent via SQL*Net to client
        520  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(MODE FORCE)屬性後,對於表的查詢自動使用RESULT CACHE機制,即使初始化引數RESULT_CACHE_MODE設定了MANUAL,且並沒有使用HINT來強制使用RESULT CACHE

不過需要注意,RESULT_CACHE的預設設定是DEFAULT,只要一個查詢訪問的表中有一個是DEFAULT狀態,整個查詢就不會使用RESULT CACHE機制:

SQL> CREATE TABLE T_CACHE2
  2  RESULT_CACHE (MODE DEFAULT)
  3  AS SELECT *
  4  FROM USER_OBJECTS;

表已建立。

SQL> SELECT COUNT(*)
  2  FROM T_CACHE T1, T_CACHE2 T2
  3  WHERE T1.OBJECT_ID = T2.OBJECT_ID;

  COUNT(*)
----------
     13798


執行計劃
----------------------------------------------------------
Plan hash value: 3151804270

--------------------------------------------------------------------------------
| Id  | Operation           | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |          |     1 |    26 |   239   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE     |          |     1 |    26 |            |          |
|*  2 |   HASH JOIN         |          | 12465 |   316K|   239   (1)| 00:00:04 |
|   3 |    TABLE ACCESS FULL| T_CACHE2 | 12465 |   158K|    42   (0)| 00:00:01 |
|   4 |    TABLE ACCESS FULL| T_CACHE  | 71348 |   905K|   196   (1)| 00:00:03 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")

Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
         52  recursive calls
          0  db block gets
        742  consistent gets
          0  physical reads
          0  redo size
        529  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

可以看到,Oracle並沒有嘗試生成RESULT CACHE記錄。如果修改T_CACHE2的屬性:

SQL> SET AUTOT OFF
SQL> SELECT TABLE_NAME, RESULT_CACHE
  2  FROM USER_TABLES
  3  WHERE TABLE_NAME LIKE 'T_CACHE%';

TABLE_NAME                     RESULT_
------------------------------ -------
T_CACHE2                       DEFAULT
T_CACHE                        FORCE

SQL> ALTER TABLE T_CACHE2 RESULT_CACHE (MODE FORCE);

表已更改。

SQL> SET AUTOT ON
SQL> SELECT COUNT(*)
  2  FROM T_CACHE T1, T_CACHE2 T2
  3  WHERE T1.OBJECT_ID = T2.OBJECT_ID;

  COUNT(*)
----------
     13798


執行計劃
----------------------------------------------------------
Plan hash value: 3151804270

---------------------------------------------------------------------------------------------------
|Id| Operation            | Name                       |Rows |Bytes|Cost (%CPU)|Time    |
-----------------------------------------------------------------------------------------
| 0| SELECT STATEMENT     |                            |    1|   26|  239   (1)|00:00:04|
| 1|  RESULT CACHE        | 9ngkv4bm47t0a2u6h7zbrrpyh4 |     |     |           |        |
| 2|   SORT AGGREGATE     |                            |    1|   26|           |        |
|*3|    HASH JOIN         |                            |12465| 316K|  239   (1)|00:00:04|
| 4|     TABLE ACCESS FULL| T_CACHE2                   |12465| 158K|   42   (0)|00:00:01|
| 5|     TABLE ACCESS FULL| T_CACHE                    |71348| 905K|  196   (1)|00:00:03|
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")

Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=1; dependencies=(TEST.T_CACHE, TEST.T_CACHE2); attributes=(single-row); name="SELECT COUNT(*)
FROM T_CACHE T1, T_CACHE2 T2
WHERE T1.OBJECT_ID = T2.OBJECT_ID"


Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
        187  recursive calls
          0  db block gets
        756  consistent gets
          0  physical reads
          0  redo size
        529  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          5  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> SELECT COUNT(*)
  2  FROM T_CACHE T1, T_CACHE2 T2
  3  WHERE T1.OBJECT_ID = T2.OBJECT_ID;

  COUNT(*)
----------
     13798


執行計劃
----------------------------------------------------------
Plan hash value: 3151804270

-------------------------------------------------------------------------------------------
|Id| Operation            | Name                       |Rows | Bytes |Cost (%CPU)| Time   |
-------------------------------------------------------------------------------------------
| 0| SELECT STATEMENT     |                            |    1|    26 |  239   (1)|00:00:04|
| 1|  RESULT CACHE        | 9ngkv4bm47t0a2u6h7zbrrpyh4 |     |       |           |        |
| 2|   SORT AGGREGATE     |                            |    1|    26 |           |        |
|*3|    HASH JOIN         |                            |12465|   316K|  239   (1)|00:00:04|
| 4|     TABLE ACCESS FULL| T_CACHE2                   |12465|   158K|   42   (0)|00:00:01|
| 5|     TABLE ACCESS FULL| T_CACHE                    |71348|   905K|  196   (1)|00:00:03|
--------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")

Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=1; dependencies=(TEST.T_CACHE, TEST.T_CACHE2); attributes=(single-row); name="SELECT COUNT(*)
FROM T_CACHE T1, T_CACHE2 T2
WHERE T1.OBJECT_ID = T2.OBJECT_ID"


Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          0  consistent gets
          0  physical reads
          0  redo size
        529  bytes sent via SQL*Net to client
        520  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,利用RESULT CACHE來提升系統效能變得更加容易和方便。

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4227/viewspace-672612/,如需轉載,請註明出處,否則將追究法律責任。

相關文章