技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> Oracle --> Oracle11g中的result cache

Oracle11g中的result cache

浏览:3145次  出处信息
作用:

    缓存sql结果集。

    在SGA中缓存sql语句的执行结果,相同sql再次执行时,直接从SGA直接读结果就可以了,不需要在去数据库中扫描索引,或是回表,计算之类的,若sql中对应的对象(比如表)做了update,insert,delete或是ddl操作,相关所有sql的缓存结果集就自动失效了。

    ORACLE建议应用仅仅对read only的表中使用result cache缓存功能。

    result cache缓存包括服务器端result cache缓存和客户端result cache缓存

    客户端缓存的好处在于sql直接从客户端的内存取数据,减少和服务器交互的开销,如果缓存涉及到的数据已经被更改了,数据库会发一个失效请求给客户端。

    客户端缓存好像只有通过OCI连接才能实现,thin方式不行。

    --result缓存在shared pool中

    The server result cache is a memory pool within the shared pool. This pool contains a SQL query result cache, which stores results of SQL queries, and a PL/SQL function result cache, which stores values returned by PL/SQL functions.

    By default, on database startup, Oracle Database allocates memory to the server result cache in the shared pool.

    --客户端的result cache失效机制

    The client result cache transparently keeps the result set consistent with session state or database changes that affect it. When a transaction changes the data or metadata of database objects used to build the cached result, the database sends

    an invalidation to the OCI client on its next round trip to the server.

    --客户端工作机制

    

    --参数

    14:44:10 TC@ tc1>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 --分配给单个sql执行结果的内存最大比例%

    result_cache_max_size big integer 7872K --分配给result cache的最大内存

    result_cache_mode string MANUAL --表示只有在sql中写hint result cache,才可以缓存结果。

    result_cache_remote_expiration integer 0

    15:28:04 TC@ tc1>show parameter client_result

    NAME TYPE VALUE

    ------------------------------------ ----------- ------------------------------

    client_result_cache_lag big integer 3000 --设置服务器和客户端之间的数据同步时间间隔,间隔最长不能超过这个时间

    client_result_cache_size big integer 0 --客户端缓存的最大值

    15:36:35 TC@ tc1>

     --加hint,走result cache,直接从缓存中得到结果集

    5:25:07 TC@ tc1>set autot traceonly

    15:25:20 TC@ tc1>select /*+result_cache*/ count(*) from tmp_dy;

    ------------------------------------------------------------------------------------------

    | Id | Operation | Name | Rows | Cost (%CPU)| Time |

    ------------------------------------------------------------------------------------------

    | 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |

    | 1 | RESULT CACHE | 6pf1wm7ttmavh3aq1v2udnx0j7 | | | |

    | 2 | SORT AGGREGATE | | 1 | | |

    | 3 | TABLE ACCESS FULL| TMP_DY | 65 | 3 (0)| 00:00:01 |

    ------------------------------------------------------------------------------------------

    --加hint,不走result cache

    16:04:49 TC@ tc1>select /*+no_result_cache*/ count(*) from tmp_dy;

    ---------------------------------------------------------------------

    | Id | Operation | Name | Rows | Cost (%CPU)| Time |

    ---------------------------------------------------------------------

    | 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |

    | 1 | SORT AGGREGATE | | 1 | | |

    | 2 | TABLE ACCESS FULL| TMP_DY | 65 | 3 (0)| 00:00:01 |

    ---------------------------------------------------------------------

    result cache的一些限制:

    1.临时表,sys,system下的表不走result cache

    2.CURRVAL,NEXTVAL, CURRENT_DATE, CURRENT_TIMESTAMP, LOCAL_TIMESTAMP, USERENV/SYS_CONTEXT (with non-constant variables), SYS_GUID, SYSDATE, and SYS_TIMESTAMP不走result cache

    --result统计信息表

    16:13:41 TC@ tc1>SELECT NAME, VALUE FROM V$RESULT_CACHE_STATISTICS;

    NAME VALUE

    -------------------------------- ---------------------------------------------------------------------------------

    Block Size (Bytes) 1024

    Block Count Maximum 7872

    Block Count Current 32

    Result Size Maximum (Blocks) 393

    Create Count Success 2

    Create Count Failure 0

    Find Count 6

    Invalidation Count 1

    Delete Count Invalid 0

    Delete Count Valid 0

    Hash Chain Length 1

    11 rows selected.

    SELECT ID, TYPE, CREATION_TIMESTAMP, BLOCK_COUNT, COLUMN_COUNT, PIN_COUNT, ROW_COUNT FROM V$RESULT_CACHE_OBJECTS ;

    SQLSET SERVEROUTPUT ON

    EXECUTE DBMS_RESULT_CACHE.MEMORY_REPORT

    其实result result这个功能mysql早就有了,原理上也是非常简单的,把数据不怎么变化的表,的某些sql执行结果缓存起来,然后每次从结果中去取就可以了。

    仅仅做个记录。

    http://download.oracle.com/docs/cd/E11882_01/server.112/e10821/memory.htm#PFGRF987 

建议继续学习:

  1. Buffer和cache的区别是什么?    (阅读:6737)
  2. 谈冷热数据    (阅读:5584)
  3. Linux操作系统中内存buffer和cache的区别    (阅读:5242)
  4. 学习:一个并发的Cache    (阅读:4853)
  5. 关于Linux的文件系统cache    (阅读:4645)
  6. Twitter架构图(cache篇)    (阅读:4588)
  7. 详解MyISAM Key Cache(前篇)    (阅读:4006)
  8. 7个示例科普CPU Cache    (阅读:3975)
  9. [squid] 过期时间在 60 秒内 squid 不 Cache 的问题    (阅读:3852)
  10. 为什么程序员需要关心顺序一致性(Sequential Consistency)而不是Cache一致性(Cache Coherence?)    (阅读:3447)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1