11g新特性:result cache 结果缓存

时间:2021-09-24 07:39:45

       result cache是oracle 11g新推出的特性,通过把查询结果还存在内存中来提高查询性能。缓存分为clinet和server端缓存。本文主要讨论server端缓存。serverresult cache是shared pool的内存的一部分。结果缓存更适合于数据变化不频繁的系统,例如OLAP。简单原理如下:当sql执行时,数据库先去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占总比例最大大小

result_cache_max_size                big integer 21568K   --缓存的最大大小

result_cache_mode                    string      MANUAL  ---模式

result_cache_remote_expiration       integer    0

 

result_cache_mode:

manual:查询时候要手动 加hintresult_cache 通知优化器才可以

auto:优化器会判断是否将使用已经存在的result cache(用hint产生的result cache)(依据查询执行的频率、生成结果的成本以及针对底层数据库对象更改的频率。),另外只有使用hint的时候产生result cache,oracle才会使用

force:尽可能的cache 查询结果,将result_cache加到select语句中(no_result_cache可拒绝cache)

 备注:比较奇怪的是,官方文档没有auto的介绍,只有manual和auto

 在以下两种情况下会使用resultcache,分别是系统参数和table进行设置

1,result_cache_mode参数;设置为manual,则需加hint才能使用resultcache;force直接使用;使用auto则由数据库决定,测试了一次,结果为不适用;

 参数为result_cache_mode为manual时,只有使用才使用hint result_cahce

SQL> connect wj/oracle;

ò?á??ó?£

SQL> select count(*) from t;

  COUNT(*)

----------

    13456

 SQL> set autot on;

SQL> select count(*) from t;

  COUNT(*)

----------

    13456

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

Plan hash value: 2966233522

 

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

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

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

|   0| SELECT STATEMENT   |      |    1 |    50   (2)| 00:00:01 |

|   1|  SORT AGGREGATE    |     |     1 |            |          |

|   2|   TABLE ACCESS FULL| T    | 12278 |   50   (2)| 00:00:01 |

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

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

  COUNT(*)

----------

    13456

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

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

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

|   0| SELECT STATEMENT    |                            |     1 |   50   (2)| 00:00:01 |

|   1|  RESULT CACHE       | c8ukugu9jf5474pjzn83wj3zph |       |           |          |

|   2|   SORT AGGREGATE    |                            |     1 |           |          |

|   3|    TABLE ACCESS FULL| T                          | 12278 |    50  (2)| 00:00:01 |

---修改为force,则强制使用resultcache

alter system set result_cache_mode=force;

 SQL> create table t1 as select * from t;

SQL> select count(*) from t1;

  COUNT(*)

----------

    13456

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

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

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

|   0| SELECT STATEMENT    |                            |     1 |   50   (2)| 00:00:01 |

|   1|  RESULT CACHE       | 8q8qwtn4uk3vv7sm1ynpzz6t4k |       |           |          |

|   2|   SORT AGGREGATE    |                            |     1 |            |          |

|   3|    TABLE ACCESS FULL| T1                         | 14242 |    50  (2)| 00:00:01 |

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

 SQL> select count(*) from t1;

  COUNT(*)

----------

    13456

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

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

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

|   0| SELECT STATEMENT    |                            |     1 |   50   (2)| 00:00:01 |

|   1|  RESULT CACHE       | 8q8qwtn4uk3vv7sm1ynpzz6t4k |       |            |          |

|   2|   SORT AGGREGATE    |                            |     1 |            |          |

|   3|    TABLE ACCESS FULL| T1                         | 14242 |    50  (2)| 00:00:01 |

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

  查询V$RESULT_CACHE_OBJECTS可以得到目前缓存的情况

  1*select name,cache_id from V$RESULT_CACHE_OBJECTS

SQL> select name,cache_id fromV$RESULT_CACHE_OBJECTS;

 

NAME                                                                  CACHE_ID

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

WJ.T1                                                                 WJ.T1

WJ.T                                                                  WJ.T

select count(*) from t1                                               8q8qwtn4uk3vv7sm1ynpzz6t4k

select /*+ result_cache */ count(*) fromt                            c8ukugu9jf5474pjzn83wj3zph

 

2,使用Table Annotations。 表注释的优先级低于sql语句。通过关键字RESULT_CACHE来实现。有DEFAULT和FORCE两个数值

DEFAULT

    If at least one table in a query is set to DEFAULT, then result cachingis not enabled at the table level for this query, unless the RESULT_CACHE_MODEinitialization parameter is set to FORCE or the RESULT_CACHE hint is specified.This is the default value.

 FORCE

    If all the tables of a query are marked as FORCE, then the query resultis considered for caching. The table annotation FORCE takes precedence over theRESULT_CACHE_MODE parameter value of MANUAL set at the session level.

 测试结果如下

 SQL> alter table t1 result_cache (MODEforce);   

 SQL> alter session set result_cache_mode= MANUAL;

 --查询T1使用resultcache

SQL> select count(*) from t1;

  COUNT(*)

----------

    13456

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

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

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

|   0| SELECT STATEMENT    |                            |     1 |   50   (2)| 00:00:01 |

|   1|  RESULT CACHE       | 8q8qwtn4uk3vv7sm1ynpzz6t4k |       |           |          |

|   2|   SORT AGGREGATE    |                            |     1 |            |          |

|   3|    TABLE ACCESS FULL| T1                         | 14242 |    50  (2)| 00:00:01 |

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

--查询表T没有使用resultcache

SQL> select count(*) from t;

  COUNT(*)

----------

    13456

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

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

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

|   0| SELECT STATEMENT   |      |    1 |    50   (2)| 00:00:01 |

|   1|  SORT AGGREGATE    |     |     1 |            |         |

|   2|   TABLE ACCESS FULL| T    | 12278 |   50   (2)| 00:00:01 |

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

 参考文献

Oracle? Database Performance Tuning Guide11g Release 2 (11.2)

http://space.itpub.net/12020513/viewspace-622483

11g New Feature : SQL Query Result Cache[ID 453567.1]    

http://yangtingkun.itpub.net/post/468/391015