oracle 11g 之 result cache
今天是2013-10-12,打算最近时间研究一下shared pool的相关原理以及awr报告分析。今天学习一下在oracle 11g shared pool中新增的一个cache 那就是result cache。
从上图可以看出在oracle 11g 的shared pool中存有对sql进行存储、存储sql执行计划、sql解析树等信息的library cache可以通过查看v$librarycahe视图了解其分配情况,以及row CACHE(data dictionary cache)可以查看v$rowcache视图了解其分配情况,对于这两部分内容咱不讨论,但是可以看一下如下这个图,大体明白这两个component的作用原理。
现在开始研究一下result cache,对于oracle 11g 分为client result cache以及server result cache,前者在client进行内存的分配,后者对于数据库server进行内存分配,现在看一下server result cache(如下皆是server result cache内容)。说白了,result cache 就是为了缓存结果集的一块区域,主要是缓存sql 查询结果集以及pl/sql function结果集。
对于 result cache存在几个参数,如下:
result_cache_max_result integer
result_cache_max_size big integer
result_cache_mode string MANUAL(AUTO、FORCE)
1)、result_cache_max_result 表示对于单个的缓存结果占整个result cache 大小的百分比。
2)、result_cache_max_size 参数用于设置该result cache 的大小,是一个动态参数,该参数为0 则说明result cache 功能禁用了。
3)、result_cache_mode,表示result cache的模式,其中有manual、force。manual 表示只有使用hints(result_cache)才可以对其结果进行缓存且当从result cache中获取结果集的时候也必须使用hints(result cache)参数;force表示强制对结果集进行缓存 oracle对该参数的设置不建议,如下:
FORCE mode is not recommended because the database and clients will attempt to cache all queries, which may create significant performance and latching overhead. Moreover, because queries that call non-deterministic PL/SQL functions are also cached, enabling the result cache in such a broad-based manner may cause material changes to the results.
如果需要不对操作进行缓存可以使用hints(no_result_cache)进行设置。
如下是练习过程:
一、
设置result cache 大小为20M,模式为manual,每个结果集占用总的cache比例为50%(为了测试方便);
那么启用result cache 与不启用的效果在那呢?如下测试见证分晓!
eg:
首先看到查看rhys下的amy这张表在稳定下来的时候一致性读为1248,物理读为1246(万源之恶)。
现在使用result cache 功能。
第一次执行该语句并将其缓存到result cache中,一致性读以及物理 读没有变化。
如下使用hints(result_cache)进行在此查询该结果集。如下:
可见物理读和一致性读都没有了,直接获得了结果集。这是好事啊。呵呵。
对于mode为force模式演示如下:
另外在联机手册中并没提及到result_cache_mode 存在auto模式,但是我在不经意输错的是否发现了这个值。
SQL> alter system set result_cache_mode=false;
alter system set result_cache_mode=false
*
ERROR at line 1:
ORA-00096: invalid value FALSE for parameter result_cache_mode, must be from amongFORCE, MANUAL, AUTO
查看资料发现auto,是只有使用hints的时候才能起作用,但是当再次查询结果的时候没必要在使用hints了,而是直接把结果取出来。测试如下:
二、
result cache 管理
对于11g中新增了result cache 那么也新增了4个视图以及一个包。
相关视图如下:
- v$result_cache_dependency
- v$result_cache_memory
- v$result_cache_objects
- v$result_cache_statistics
就不在介绍了,可以参考联机手册进行分析以及学习:
http://www.oracle.com/pls/db112/search?word=v$result_cache_
现在看一个dbms_result_cache包:
存在有6个subprograms,
Subprogram Description Sets the bypass mode for the Result Cache
Attempts to remove all the objects from the Result Cache, and depending on the arguments retains or releases the memory and retains or clears the statistics
Invalidates all the result-set objects that dependent upon the specified dependency object
Invalidates the specified result-set object(s)
Produces the memory usage report for the Result Cache
Checks the status of the Result Cache
演示二个:
其他参考:
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_result_cache.htm#CHDJCFJG