Oracle11新特性——SQL快取結果集(五)

yangtingkun發表於2010-09-08

打算寫一系列的文章介紹11g的新特性和變化。

這篇文章討論DMLSQL快取結果集的影響。

Oracle11新特性——SQL快取結果集(一):http://yangtingkun.itpub.net/post/468/391015

Oracle11新特性——SQL快取結果集(二):http://yangtingkun.itpub.net/post/468/391560

Oracle11新特性——SQL快取結果集(三):http://yangtingkun.itpub.net/post/468/392028

Oracle11新特性——SQL快取結果集(四):http://yangtingkun.itpub.net/post/468/475130

 

 

在看文件的時候發現了這個描述,檢查了一下以前的文章,雖然提到了DML影響緩衝結果集,但是側重點不一樣,並沒有描述當使用者對目標表進行修改後,且事務沒有提交之前,沒有辦法利用快取結果集的特性,具體情況透過一個例子說明:

[oracle@bjtest ~]$ sqlplus TEST/TEST

SQL*Plus: Release 11.2.0.1.0 Production on 星期四 9 9 01:16:19 2010

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


連線到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set pages 100 lines 120
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 as
  2  select *    
  3  from all_objects;

表已建立。

SQL> set autot on
SQL> select /*+ result_cache */ count(*) from t_cache;

  COUNT(*)
----------
     69433


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

------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     1 |   196   (1)| 00:00:03 |
|   1 |  RESULT CACHE       | cs0yudzh0cqqb1mc32r032mgkq |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71356 |   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 /*+ result_cache */ count(*) f
rom t_cache"


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


統計資訊
----------------------------------------------------------
          4  recursive calls
          0  db block gets
        575  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

SQL> select /*+ result_cache */ count(*) from t_cache;

  COUNT(*)
----------
     69433


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

------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     1 |   196   (1)| 00:00:03 |
|   1 |  RESULT CACHE       | cs0yudzh0cqqb1mc32r032mgkq |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71356 |   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 /*+ result_cache */ count(*) f
rom 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

快取結果集對第二次查詢已經生效,下面當前會話修改一條記錄:

SQL> delete t_cache where rownum = 1;

已刪除 1 行。


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

-----------------------------------------------------------------------
| Id  | Operation           | Name    | Rows  | Cost (%CPU)| Time     |
-----------------------------------------------------------------------
|   0 | DELETE STATEMENT    |         |     1 |   196   (1)| 00:00:03 |
|   1 |  DELETE             | T_CACHE |       |            |          |
|*  2 |   COUNT STOPKEY     |         |       |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE | 71356 |   196   (1)| 00:00:03 |
-----------------------------------------------------------------------

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

   2 - filter(ROWNUM=1)

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


統計資訊
----------------------------------------------------------
          6  recursive calls
          1  db block gets
         77  consistent gets
          0  physical reads
        396  redo size
        840  bytes sent via SQL*Net to client
        784  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> select /*+ result_cache */ 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       | cs0yudzh0cqqb1mc32r032mgkq |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71356 |   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 /*+ result_cache */ count(*) f
rom t_cache"


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


統計資訊
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        506  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

SQL> select /*+ result_cache */ 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       | cs0yudzh0cqqb1mc32r032mgkq |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71356 |   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 /*+ result_cache */ count(*) f
rom t_cache"


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


統計資訊
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        506  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

當前會話由於修改了T_CACHE表,因此原本的快取結果集對於當前會話的查詢是不正確的,但是當前會話的第一次查詢並沒有講結果儲存到結果集快取中,第二次執行同樣的查詢仍然需要訪問表。

對於其他的會話由於修改還未提交,因此原本的快取結果集仍然生效:

SQL> set sqlp 'SQL2> '
SQL2> set autot on
SQL2> select /*+ result_cache */ count(*) from t_cache;

  COUNT(*)
----------
     69433


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

------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     1 |   196   (1)| 00:00:03 |
|   1 |  RESULT CACHE       | cs0yudzh0cqqb1mc32r032mgkq |       |            |          |
|   2 |   SORT AGGREGATE    |                            |     1 |            |          |
|   3 |    TABLE ACCESS FULL| T_CACHE                    | 71356 |   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 /*+ result_cache */ count(*) f
rom 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

Oracle為了保證一致性讀,因此在會話修改了表的記錄後,不再啟用快取結果集的機制。

 

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

相關文章