本篇我们继续讨论Result Cache。
4、聊聊理论
在之前的文章中,我们已经介绍了Result Cache的基本使用方法。这部分我们讨论一下Oracle 11g Result Cache的深层原理。
从参数看,Oracle提供了Client Result Cache和Server Result Cache两种机制。
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 2080K
result_cache_mode string MANUAL
result_cache_remote_expiration integer 0
首先我们说说Server Result Cache。Oracle的Result Cache参数就是表示Server端的Cache功能。Server Result Cache是在Oracle SGA中的Shared Pool里面一块内存池。这个Memory Pool包括两个组成部分,或者说包括两种缓存内容,就是SQL语句的查询结果和PL/SQL函数的返回值结果。Server Result Cache功能的初衷,就是为了缓解一些系统长期高频度的访问某些“稳定性好”的数据。(笔者按:也是权益之计,详见下文)
当我们启用Result Cache的时候,Oracle的工作流程是这样的。首先,如果result_cache_mode参数是MANUAL,表示只有在使用result_cache和no_result_cache参数的时候,才能启用Result Cache功能。
Oracle解析到SQL语句使用了hint后,就回到缓存中查找相应的记录。如果不存在对应的结果集信息,Oracle就会真正执行这个SQL语句,附带将结果以一个ID保存在shared pool的cache pool中。第二次相同SQL到来之后,就会发现与现有ID的一致性,将结果直接返回。
应该说,Oracle的Result Cache是消除逻辑读,更进一步的缓存策略。但是,所有的缓存都存在一个相同的问题,就是冗余数据(Cache)的一致性。
从目前分析看,Oracle是借助对象之间的“依赖”关系来做到数据同步。首先,我们看一个系列例子:
SQL> create table t_tab as select * from dba_tables;
Table created
SQL> select /*+result_cache*/count(*) from t_tab, t where t.owner=t_tab.owner and t.object_name=t_tab.table_name;
COUNT(*)
----------
4108
SQL> col name for a30;
SQL> select id, type, status, name, row_count from v$result_cache_objects;
ID TYPE STATUS NAME ROW_COUNT
---------- ---------- --------- ------------------------------ ----------
1 Dependency Published SCOTT.T_TAB 0
0 Dependency Published SCOTT.T 0
2 Result Published select /*+result_cache*/count( 1
*) from t_tab, t where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
操作涉及到两个对象t和t_tab。我们从v$result_cache_objects视图中,可以看到,Oracle不仅仅将那个结果集合(TYPE=RESULT)保存下来,而且将两个数据表的信息保存下来,作为“Dependency”关系。
从我们第三部分的实验,我们可以知道,一旦发生数据表t和t_tab的任何变化,包括数据DML变化操作,两个缓存的依赖对象就会失效,从而引发Result Cache结果集合的失效。注意:这个依赖是非常敏感的,即使是权限变化这样的小事,都会引起Cache的失效。
SQL> grant select on t to hr;
Grant succeeded
SQL> select id, type, status, name, row_count from v$result_cache_objects;
ID TYPE STATUS NAME ROW_COUNT
---------- ---------- --------- ------------------------------ ----------
1 Dependency Published SCOTT.T_TAB 0
0 Dependency Published SCOTT.T 0
2 Result Invalid select /*+result_cache*/count( 1
*) from t_tab, t where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
那么,字面值(SQL语句结构)对缓存有影响吗?
SQL> select /*+result_cache*/count(*) from t_tab, t where t.owner=t_tab.owner and t.object_name=t_tab.table_name;
COUNT(*)
----------
4108
--新成立一个result cache
SQL> select id, type, status, name, row_count from v$result_cache_objects;
ID TYPE STATUS NAME ROW_COUNT
---------- ---------- --------- ------------------------------ ----------
1 Dependency Published SCOTT.T_TAB 0
0 Dependency Published SCOTT.T 0
3 Result Published select /*+result_cache*/count( 1
*) from t_tab, t where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
2 Result Invalid select /*+result_cache*/count( 1
*) from t_tab, t where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
--交换位置
SQL> select /*+result_cache*/count(*) from t, t_tab where t.owner=t_tab.owner and t.object_name=t_tab.table_name;
COUNT(*)
----------
4108
SQL> select id, type, status, name, row_count from v$result_cache_objects;
ID TYPE STATUS NAME ROW_COUNT
---------- ---------- --------- ------------------------------ ----------
1 Dependency Published SCOTT.T_TAB 0
0 Dependency Published SCOTT.T 0
4 Result Published select /*+result_cache*/count( 1
*) from t, t_tab where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
3 Result Published select /*+result_cache*/count( 1
*) from t_tab, t where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
2 Result Invalid select /*+result_cache*/count( 1
*) from t_tab, t where t.owner
=t_tab.owner and t.object_name
=t_tab.table_name
当SQL语句结构变化比较大的时候,Result Cache是不会共用的。
下面聊聊Client Result Cache。Client Result Cache全名为Oracle Call Interface(OCI)Client Result Cache。Client Result Cache主要是为了基于OCI的应用程序,每一个Client Result Cache是在一个应用程序中的,所有这个Process的会话,都会共享这个Result Cache。
注意:Client Result Cache和Server Result Cache是完全不同的两个特点。相互之间没有关系。
OCI Client Result Cache是直接在客户端建立缓存,适用于OCCI,JDBC OCI和ODP.NET应用程序。OCI Client Result Cache最直接的好处就是减少数据库相应和资源消耗,提高整体应用体验。
对数据失效的问题,Oracle一旦发现依赖对象的失效,就会反向向客户端发送信息,引起Client Cache的失效。
5、Result Cache的相关参数解析
Result Cache的功能是通过Oracle一系列加入的参数来进行控制的。本部分介绍一下Oracle Result Cache所使用的参数信息。
Server Result Cache是主体,默认Result Cache在SGA中是有一块区域pool的,通过hint Result_cache和no_result_cache来控制。从shared_pool中,我们可以看到当前result cache的信息。
SQL> select * from v$sgastat where lower(name) like '%result_cache%'
2 ;
POOL NAME BYTES
------------ ------------------------------ ----------
shared pool Result Cache: State Objs 2852
shared pool Result Cache 98396
shared pool Result Cache: Memory Mgr 124
shared pool Result Cache: Bloom Fltr 2048
shared pool Result Cache: Cache Mgr 4416
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 2080K
result_cache_mode string MANUAL
result_cache_remote_expiration integer 0
ü RESULT_CACHE_MAX_SIZE
大小控制参数,控制shared pool中Result Cache可以达到的最大容量大小。如果这个参数设置为0,就表示不使用Result Cache功能。
ü RESULT_CACHE_MAX_RESULT
比例控制参数,表示每一个Result Cache记录可以使用到的Result Cache总大小比例。默认为5%。
ü RESULT_CACHE_REMOTE_EXPIRATION
远程数据自我失效时间。在所有的数据集合中,Remote数据集合是最敏感的。因为数据从远端来,受到网络因素影响。而且,通常比较难让Invalid通知机制起作用。
REMOT_EXPIRATION的参数就是设置一个过期时间,以分钟计数。超过这个时间的远程结果集合会失效。默认这个参数取值为0。
相对于传统的Oracle内存池,在自动调整时代,Oracle是怎么管理Result Cache大小呢?在启动的时候,根据不同的情况设置,Oracle会选择出Cache Pool的大小。
当设置MEMORY_TARGET的时候,Oracle会将Result Cache的大小设置为Target值的0.25%。如果没有设置MEMORY_TARGET参数,只设置了SGA_TARGET,这个大小为0.5%。如果设置了SHARD_POOL_SIZE参数,这个算法的参数值为1%。
从参数上看,我们可以看到,对于每个Result Cache记录,Oracle都是有保留的。并不是所有结果集合都会被记录。当Result Cache Pool达到设置了本身的限制之后,比可用空间大的结果集合,都不会被缓存。
Result Cache Pool自身使用的是LRU算法,将结果Age Out出去。在RAC环境下,每个实例Instance都可以设置自己的Result Cache大小。
下面我们继续介绍Result Cache相关的信息。