首页 > 其他分享 >[20230526]RESULT_CACHE提示选项.txt

[20230526]RESULT_CACHE提示选项.txt

时间:2023-05-28 20:36:17浏览次数:45  
标签:test01p CACHE DEMO cache RESULT 20230526 id select result

[20230526]RESULT_CACHE提示选项.txt

--//一般如果查询信息很少变化,可以通过提示缓存结果,这样可以一定程度减少latch,逻辑读等等资源的使用。
--//实际上RESULT_CACHE提示还支持一些选项shelflife,snapshot。
--//测试参考链接:http://www.dbi-services.com/index.php/blog/entry/resultcache-hint-expiration-options

1.环境
SCOTT@test01p> @ ver1
PORT_STRING          VERSION    BANNER                                                                       CON_ID
-------------------- ---------- ---------------------------------------------------------------------------- ------
IBMPC/WIN_NT64-9.1.0 12.2.0.1.0 Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production      0

2.例子建立:
SCOTT@test01p> create table DEMO as select rownum id from xmltable('1 to 100000');
Table created.

--//分析略。

SCOTT@test01p> exec if dbms_result_cache.flush then dbms_output.put_line('Flushed.'); end if;
PL/SQL procedure successfully completed.

3.测试:
SCOTT@test01p> set autotrace on
SCOTT@test01p> select /*+ result_cache */ count(*) from DEMO;
COUNT(*)
--------
  100000

Execution Plan
---------------------------
Plan hash value: 2180342005
-----------------------------------------------------------------------------
|Id|Operation           |Name                      |Rows|Cost(%CPU)|Time    |
-----------------------------------------------------------------------------
| 0|SELECT STATEMENT    |                          |  1 |  47   (3)|00:00:01|
| 1| RESULT CACHE       |1s07f2h70ga9484jsgc1s50zjr|    |          |        |
| 2|  SORT AGGREGATE    |                          |  1 |          |        |
| 3|   TABLE ACCESS FULL|DEMO                      |100K|  47   (3)|00:00:01|
-----------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------
   1 - column-count=1; dependencies=(SCOTT.DEMO); attributes=(single-row); name="select /*+ result_cache */ count(*) from DEMO"
Statistics
-------------------------------------------
  0  recursive calls
  0  db block gets
  0  consistent gets
  0  physical reads
  0  redo size
542  bytes sent via SQL*Net to client
608  bytes received via SQL*Net from client
  2  SQL*Net roundtrips to/from client
  0  sorts (memory)
  0  sorts (disk)
  1  rows processed
--//这是第2次运行的结果。

SCOTT@test01p> set autotrace off
SCOTT@test01p> select * from v$result_cache_dependency;
RESULT_ID DEPEND_ID OBJECT_NO CON_ID
--------- --------- --------- ------
      243       242     30020      3

SCOTT@test01p> col name format a50
SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects where id in (242,243) order by id;
 ID TYPE       STATUS    NAME                                          CACHE_ID                   INVALIDATIONS
--- ---------- --------- --------------------------------------------- -------------------------- -------------
242 Dependency Published SCOTT.DEMO                                    SCOTT.DEMO                             0
243 Result     Published select /*+ result_cache */ count(*) from DEMO 1s07f2h70ga9484jsgc1s50zjr             0
--//the query result (id=243) depends on the table (id=242).

4.继续:
--//DML
SCOTT@test01p> delete from DEMO where null is not null;
0 rows deleted.

SCOTT@test01p> commit ;
Commit complete.

SCOTT@test01p> select * from v$result_cache_dependency where OBJECT_NO=30020;
no rows selected

SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects where id in (242,243) order by id;
 ID TYPE       STATUS    NAME                                          CACHE_ID                   INVALIDATIONS
--- ---------- --------- --------------------------------------------- -------------------------- -------------
242 Dependency Published SCOTT.DEMO                                    SCOTT.DEMO                             1
243 Result     Invalid   select /*+ result_cache */ count(*) from DEMO 1s07f2h70ga9484jsgc1s50zjr             0

SCOTT@test01p> set autotrace on
SCOTT@test01p> select /*+ result_cache */ count(*) from DEMO;
  COUNT(*)
----------
    100000

Execution Plan
---------------------------
Plan hash value: 2180342005
-----------------------------------------------------------------------------
|Id|Operation           |Name                      |Rows|Cost(%CPU)|Time    |
-----------------------------------------------------------------------------
| 0|SELECT STATEMENT    |                          |  1 |   47  (3)|00:00:01|
| 1| RESULT CACHE       |1s07f2h70ga9484jsgc1s50zjr|    |          |        |
| 2|  SORT AGGREGATE    |                          |  1 |          |        |
| 3|   TABLE ACCESS FULL|DEMO                      |100K|   47  (3)|00:00:01|
-----------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------
   1 - column-count=1; dependencies=(SCOTT.DEMO); attributes=(single-row); name="select /*+ result_cache */ count(*) from DEMO"
Statistics
----------------------------------------------------------
         51  recursive calls
          4  db block gets
        197  consistent gets
          0  physical reads
          0  redo size
        542  bytes sent via SQL*Net to client
        608  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          5  sorts (memory)
          0  sorts (disk)
          1  rows processed
--//出现逻辑读。

SCOTT@test01p> set autotrace off
SCOTT@test01p> select * from v$result_cache_dependency where OBJECT_NO=30020;
RESULT_ID DEPEND_ID OBJECT_NO CON_ID
--------- --------- --------- ------
      250       242     30020      3

SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects where id in (242,243,250) order by id;
 ID TYPE       STATUS    NAME                                               CACHE_ID                   INVALIDATIONS
--- ---------- --------- -------------------------------------------------- -------------------------- -------------
242 Dependency Published SCOTT.DEMO                                         SCOTT.DEMO                             1
243 Result     Invalid   select /*+ result_cache */ count(*) from DEMO      1s07f2h70ga9484jsgc1s50zjr             0
250 Result     Published select /*+ result_cache */ count(*) from DEMO      1s07f2h70ga9484jsgc1s50zjr             0
--//ID=243的结果集已经无效。可以看出结果集合涉及到的表有DML操作,status变成Invalid.

5.测试RESULT_CACHE(SHELFLIFE=seconds):
SCOTT@test01p> exec if dbms_result_cache.flush then dbms_output.put_line('Flushed.'); end if;
PL/SQL procedure successfully completed.

SCOTT@test01p> set autotrace on
SCOTT@test01p> select /*+ result_cache(shelflife=10) */ count(*) from DEMO;
COUNT(*)
--------
  100000

Execution Plan
---------------------------
Plan hash value: 2180342005
-----------------------------------------------------------------------------
|Id|Operation           |Name                      |Rows|Cost(%CPU)|Time    |
-----------------------------------------------------------------------------
| 0|SELECT STATEMENT    |                          |  1 |   47  (3)|00:00:01|
| 1| RESULT CACHE       |1s07f2h70ga9484jsgc1s50zjr|    |          |        |
| 2|  SORT AGGREGATE    |                          |  1 |          |        |
| 3|   TABLE ACCESS FULL|DEMO                      |100K|   47  (3)|00:00:01|
-----------------------------------------------------------------------------
Result Cache Information (identified by operation id):
------------------------------------------------------
   1 - column-count=1; dependencies=(SCOTT.DEMO); attributes=(single-row, shelflife=10); name="select /*+ result_cache(shelflife=10) */ count(*) from DEMO"
Statistics
---------------------------------------------
  1  recursive calls
  4  db block gets
165  consistent gets
  0  physical reads
  0  redo size
542  bytes sent via SQL*Net to client
608  bytes received via SQL*Net from client
  2  SQL*Net roundtrips to/from client
  0  sorts (memory)
  0  sorts (disk)
  1  rows processed
--//we see the 'shelflife' attribute but the dependency is the same as without the option:

SCOTT@test01p> set autotrace off
SCOTT@test01p> select * from v$result_cache_dependency where OBJECT_NO=30020;
RESULT_ID DEPEND_ID OBJECT_NO CON_ID
--------- --------- --------- ------
      225       224     30020      3

SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects where id in (224,225) order by id;
 ID TYPE       STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
--- ---------- --------- ------------------------------------------------------------ -------------------------- -------------
224 Dependency Published SCOTT.DEMO                                                   SCOTT.DEMO                             0
225 Result     Published select /*+ result_cache(shelflife=10) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0
       
SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects where id in (224,225) order by id;
 ID TYPE       STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
--- ---------- --------- ------------------------------------------------------------ -------------------------- -------------
224 Dependency Published SCOTT.DEMO                                                   SCOTT.DEMO                             0
225 Result     Expired   select /*+ result_cache(shelflife=10) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0

SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects where id in (224,225) order by id;
 ID TYPE       STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
--- ---------- --------- ------------------------------------------------------------ -------------------------- -------------
224 Dependency Published SCOTT.DEMO                                                   SCOTT.DEMO                             0
225 Result     Invalid   select /*+ result_cache(shelflife=10) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0
--//注意id=225的status从Published -> Expired -> Invalid.按照作者介绍等10秒就失效了。你可以设置shelflife大一些测试。
--//当然这样情况DML会失效。

SCOTT@test01p> delete from DEMO where null is not null;
0 rows deleted.

SCOTT@test01p> commit ;
Commit complete.

SCOTT@test01p> select * from v$result_cache_dependency where OBJECT_NO=30020;
no rows selected

SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects order by id;
 ID TYPE       STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
--- ---------- --------- ------------------------------------------------------------ -------------------------- -------------
224 Dependency Published SCOTT.DEMO                                                   SCOTT.DEMO                             0
225 Result     Invalid   select /*+ result_cache(shelflife=10) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0
227 Result     Invalid   select /*+ result_cache(shelflife=10) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0
--//result_cache(shelflife=N),增加了失效时间的控制,当然发生DML变得无效.

6.测试RESULT_CACHE(SNAPSHOT=seconds):
SCOTT@test01p> exec if dbms_result_cache.flush then dbms_output.put_line('Flushed.'); end if;
PL/SQL procedure successfully completed.

SCOTT@test01p> select /*+ result_cache(snapshot=900) */ count(*) from DEMO;
COUNT(*)
--------
  100000

SCOTT@test01p> select * from v$result_cache_dependency where OBJECT_NO=30020;
no rows selected
--//注意没有依赖!!

SCOTT@test01p> select /*+ result_cache(snapshot=900) */ count(*) from DEMO;
COUNT(*)
--------
  100000

SCOTT@test01p> select * from v$result_cache_dependency where OBJECT_NO=30020;
no rows selected

SCOTT@test01p> select id,type,status,name,cache_id,invalidations from v$result_cache_objects order by id;
 ID TYPE   STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
--- ------ --------- ------------------------------------------------------------ -------------------------- -------------
224 Result Published select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0    

--//执行dml操作:
SCOTT@test01p> delete from DEMO where id=1;
1 row deleted.

SCOTT@test01p> commit ;
Commit complete.

SCOTT@test01p> select /*+ result_cache(snapshot=900) */ count(*) from DEMO;
  COUNT(*)
----------
    100000
--//注意看这样结果没有变化,还是100000.而实际上现在是100000-1.

SCOTT@test01p> select /*+ r11esult_cache(snapshot=900) */ count(*) from DEMO;
  COUNT(*)
----------
     99999
--//破坏提示,可以发现真实的结果是99999.

SCOTT@test01p> select CREATION_TIMESTAMP,id,type,status,name,cache_id,invalidations from v$result_cache_objects order by id;
CREATION_TIMESTAMP   ID TYPE   STATUS    NAME                                                         CACHE_ID                       INVALIDATIONS
------------------- --- ------ --------- ------------------------------------------------------------ ------------------------------ -------------
2023-05-26 21:22:22 224 Result Published select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr                 0
--//900秒=15分钟,等一小会看看,时间有点长...

SCOTT@test01p> select sysdate,CREATION_TIMESTAMP,id,type,status,name,cache_id,invalidations from v$result_cache_objects order by id;
SYSDATE             CREATION_TIMESTAMP   ID TYPE   STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
------------------- ------------------- --- ------ --------- ------------------------------------------------------------ -------------------------- -------------
2023-05-26 21:37:21 2023-05-26 21:22:22 224 Result Published select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0

SCOTT@test01p> select sysdate,CREATION_TIMESTAMP,id,type,status,name,cache_id,invalidations from v$result_cache_objects order by id;
SYSDATE             CREATION_TIMESTAMP   ID TYPE   STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
------------------- ------------------- --- ------ --------- ------------------------------------------------------------ -------------------------- -------------
2023-05-26 21:37:22 2023-05-26 21:22:22 224 Result Expired   select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0

SCOTT@test01p> select sysdate,CREATION_TIMESTAMP,id,type,status,name,cache_id,invalidations from v$result_cache_objects order by id;
SYSDATE             CREATION_TIMESTAMP   ID TYPE   STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
------------------- ------------------- --- ------ --------- ------------------------------------------------------------ -------------------------- -------------
2023-05-26 21:37:23 2023-05-26 21:22:22 224 Result Invalid   select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0
--//注意status的变化。

SCOTT@test01p> select /*+ result_cache(snapshot=900) */ count(*) from DEMO;
COUNT(*)
--------
   99999

SCOTT@test01p> select sysdate,CREATION_TIMESTAMP,id,type,status,name,cache_id,invalidations from v$result_cache_objects where name like '%snapshot%' order by id;
SYSDATE             CREATION_TIMESTAMP   ID TYPE   STATUS    NAME                                                         CACHE_ID                   INVALIDATIONS
------------------- ------------------- --- ------ --------- ------------------------------------------------------------ -------------------------- -------------
2023-05-26 21:40:06 2023-05-26 21:38:07  42 Result Published select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0
2023-05-26 21:40:06 2023-05-26 21:22:22 224 Result Invalid   select /*+ result_cache(snapshot=900) */ count(*) from DEMO  1s07f2h70ga9484jsgc1s50zjr             0

7.结论:
--//http://www.dbi-services.com/index.php/blog/entry/resultcache-hint-expiration-options
Conclusion

There are two reasons to invalidate a result cache: DML on dependency, or expiration after a number of seconds. And we
can use any combination of them with undocumented hints. Oracle itself uses them internally. SNAPSHOT is used by Active
Dynamic Sampling: stale result are acceptable for one hour. SHELFLIFE is used on dictionary views bases on X$ tables
where dependency cannot be tracked.
使结果缓存无效的原因有两个:依赖关系上的DML,或在数秒钟后过期。我们可以使用它们的任何组合。Oracle本身也在内部使用它们。
快照是可使用的主动动态采样:陈旧的结果是可以接受的一个小时。基于无法跟踪依赖关系的X$表的字典视图。

I hope it will get documented in future releases. There are some cases where we can accept stale results in order to get
better performance. We already do that with materialized views, so why not do it with result cache?
我希望它能在未来的版本中得到记录。在某些情况下,我们可以接受陈旧的结果,以获得更好的性能。我们已经对实例化视图这样做了,
那么为什么不使用结果缓存呢?

标签:test01p,CACHE,DEMO,cache,RESULT,20230526,id,select,result
From: https://www.cnblogs.com/lfree/p/17438790.html

相关文章

  • [20230527]RESULT_CACHE提示选项2.txt
    [20230527]RESULT_CACHE提示选项2.txt--//昨天测试了result_cache(snapshot=N)提示,它相当于不管查询对象数据有何变化,这个结果集合保持一定的时刻的状态。--//我在想许多情况下其实可能不需要知道准确结果,可以通过它减少对数据库的压力,测试通过sqlprofile或者sqlpatch方式实现这......
  • 【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存C
    缓存的理解缓存的工作机制是先从缓存中读取数据,如果没有再从慢速设备上读取实际数据,并将数据存入缓存中。通常情况下,我们会将那些经常读取且不经常修改的数据或昂贵(CPU/IO)的且对于相同请求有相同计算结果的数据存储到缓存中。它能够让数据更加接近于使用者,下图所示。+-------------......
  • ResultSet和satement和preparedStatement
    1. ResultSet[结果集]   8271.1 基本介绍1.表示数据库结果集的数据表,通常通过执行查询数据库的语生成2.ResultSet对象保持一个光标指向其当前的数据行。最初, 光标位于第一行之前3. next方法将光标移动到下一行,并且由于在ResultSet对象中没有更多行时返回false,因此可以在whil......
  • 5、基于 request cache 请求缓存技术
    Hystrixcommand执行时8大步骤第三步,就是检查Requestcache是否有缓存。首先,有一个概念,叫做RequestContext请求上下文,一般来说,在一个web应用中,如果我们用到了Hystrix,我们会在一个filter里面,对每一个请求都施加一个请求上下文。就是说,每一次请求,就是一次请求上下文......
  • libmemcached支持连接池访问memcached
    #include<stdio.h>#include<libmemcached/memcached.h>#include<libmemcached/util.h>#include<assert.h>//g++-m32-gmemcached_test.c-omemcached_test-I/usr/lib/libmemcached/include-L/usr/lib/libmemcached/lib-lmemcached-l......
  • Task.CompleteTask和Task.FromResult
    问题描述实现接口中的异步方法时,因为返回值类型是Task或Task<T>,所以即使方法的具体实现逻辑极简执行极快(比如直接返回一个常量字符串),我们可能也需要被迫新建一个Task去执行,如下:publicinterfaceIComputer{TaskDo();Task<string>DoString();}publicclass......
  • Memcached与Redis的区别和选择
    一、Memcached简介Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。Memcached是以LiveJournal旗下DangaInteractive公司的BradFitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。Memcache......
  • Guava cache使用总结
    importcom.google.common.cache.CacheBuilder;importcom.google.common.cache.CacheLoader;importcom.google.common.cache.LoadingCache;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Random;importjava.util.concurrent.TimeUnit;......
  • 【异常】com.alicp.jetcache.CacheException: refresh error
    jetcacherefresherror一、背景描述二、报错内容三、报错原因四、解决方案4.1解决方案一,使用一级缓存4.2解决方案二,开启写入权限一、背景描述技术栈:SpringBoot(2.1.5.RELEASE)+SpringCloudOopenfeign(2.1.1.RELEASE)+jetCache(2.5.14)+redis(3.1.0)由于项目使用了微......
  • Linux buffer/cache
    在Linux系统中,经常用free命令来查看系统内存的使用状态。在一个centos7的系统上,free命令的显示内容大概是这样一个状态:这里的默认显示单位是kb,可以通过添加-h参数,来让free命令显示的更为友好一些。[root@k8s-master-192~]#free-htotalused......