oracle result_cache

result_cache 

oracle在sga的shared_pool内存中专门开辟了一个result cache内存结构,当第一个会话查询时会先从磁盘取数据,同时将结果放在shared_pool中的result cache中,第二个回话相同的sql查询时会直接从result cache中读取数据,极大的提升了查询的性能

测试:

--不使用result_cache两次查询

SQL> alter system flush shared_pool;

System altered.

SQL> alter system flush buffer_cache;

System altered.
SQL>  select deptno, avg(sal) from emp group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
        30 1566.66667
        20       2175
        10 2916.66667


Execution Plan
----------------------------------------------------------
Plan hash value: 4067220884

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     3 |    21 |     4  (25)| 00:00:01 |
|   1 |  HASH GROUP BY     |      |     3 |    21 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |    98 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------


Statistics
----------------------------------------------------------
         11  recursive calls
          0  db block gets
         14  consistent gets
          3  physical reads
          0  redo size
        704  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          3  rows processed
SQL> select deptno, avg(sal) from emp group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
        30 1566.66667
        20       2175
        10 2916.66667


Execution Plan
----------------------------------------------------------
Plan hash value: 4067220884

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     3 |    21 |     4  (25)| 00:00:01 |
|   1 |  HASH GROUP BY     |      |     3 |    21 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |    98 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          2  consistent gets
          0  physical reads
          0  redo size
        704  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          3  rows processed

--使用result_cache两次查询结果

SQL> alter system flush shared_pool;

System altered.

SQL> alter system flush buffer_cache;

System altered.

SQL> 

		  
select /*+ result_cache */ deptno, avg(sal) from emp
 group by deptno;
SQL> select /*+ result_cache */ deptno, avg(sal) from emp
  2   group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
        30 1566.66667
        20       2175
        10 2916.66667


Execution Plan
----------------------------------------------------------
Plan hash value: 4067220884

--------------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     3 |    21 |     4  (25)| 00:00:01 |
|   1 |  RESULT CACHE       | 69cmubna179r6gh0sd1y59kbqz |       |       |            |          |
|   2 |   HASH GROUP BY     |                            |     3 |    21 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| EMP                        |    14 |    98 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------

Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=2; dependencies=(SCOTT.EMP); name="select /*+ result_cache */ deptno, avg(sal) from emp
 group by deptno"



Statistics
----------------------------------------------------------
         64  recursive calls
          0  db block gets
         53  consistent gets
         14  physical reads
          0  redo size
        704  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          7  sorts (memory)
          0  sorts (disk)
          3  rows processed

SQL> select /*+ result_cache */ deptno, avg(sal) from emp
  2   group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
        30 1566.66667
        20       2175
        10 2916.66667


Execution Plan
----------------------------------------------------------
Plan hash value: 4067220884

--------------------------------------------------------------------------------------------------
| Id  | Operation           | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                            |     3 |    21 |     4  (25)| 00:00:01 |
|   1 |  RESULT CACHE       | 69cmubna179r6gh0sd1y59kbqz |       |       |            |          |
|   2 |   HASH GROUP BY     |                            |     3 |    21 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| EMP                        |    14 |    98 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------

Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=2; dependencies=(SCOTT.EMP); name="select /*+ result_cache */ deptno, avg(sal) from emp
 group by deptno"



Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          0  consistent gets
          0  physical reads
          0  redo size
        704  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          3  rows processed

可见在使用result_cache第二次查询根本没有访问buffer_cahce,执行性能非常高

适用场景:

    查询记录很多,但返回的记录较少,并且重复查询的次数比较高,表的数据相对静态,变化不到


开启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 2624K
result_cache_mode                    string      MANUAL
result_cache_remote_expiration       integer     0

result_cache_mode  Define using or not using result_cache

MANUAL  当sql语句有相关hint result_cache的时候使用此技术

FORCE只要有可能,所有的语句都将使用result_cache

无论result_cache_mode如何测试,oracle将优先考虑sql语句中hint关于result_cache or no_result_cache的提示


默认情况下oracle result_cache_mode 为manual,一般不要把其设置为force,如果为force所有的查询将考虑result_cache 会增加不必要的开销。

RESULT_CACHE_MAX_SIZE

设置result_cache共分配多少内存使用,如果设置为0,result_cache将不可使用



RESULT_CACHE_MAX_RESULT

决定单个sql查询可以使用多少result_cache容量,默认值是5%,可以根据需求调整此参数的值(1――100)



RESULT_CACHE_REMOTE_EXPIRATION

设置result_cache远程对象失效时间,默认时间为0即远程对象不做result_cache保存


你可能感兴趣的:(oracle,result_cahce)