IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

Oracle11g中的result cache

Incessant 2010-06-01 21:55:55 累计浏览 4,274 次
本机暂存
作用:

    缓存sql结果集。

    在SGA中缓存sql语句的执行结果,相同sql再次执行时,直接从SGA直接读结果就可以了,不需要在去数据库中扫描索引,或是回表,计算之类的,若sql中对应的对象(比如表)做了update,insert,delete或是ddl操作,相关所有sql的缓存结果集就自动失效了。

    ORACLE建议应用仅仅对read only的表中使用result cache缓存功能。

    result cache缓存包括服务器端result cache缓存和客户端result cache缓存

    客户端缓存的好处在于sql直接从客户端的内存取数据,减少和服务器交互的开销,如果缓存涉及到的数据已经被更改了,数据库会发一个失效请求给客户端。

    客户端缓存好像只有通过OCI连接才能实现,thin方式不行。

    --result缓存在shared pool中

    The server result cache is a memory pool within the shared pool. This pool contains a SQL query result cache, which stores results of SQL queries, and a PL/SQL function result cache, which stores values returned by PL/SQL functions.

    By default, on database startup, Oracle Database allocates memory to the server result cache in the shared pool.

    --客户端的result cache失效机制

    The client result cache transparently keeps the result set consistent with session state or database changes that affect it. When a transaction changes the data or metadata of database objects used to build the cached result, the database sends

    an invalidation to the OCI client on its next round trip to the server.

    --客户端工作机制

    原图已失效

    --参数

    14:44:10 TC@ tc1>show parameter RESULT

    NAME TYPE VALUE

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

    client_result_cache_lag big integer 3000

    client_result_cache_size big integer 0

    result_cache_max_result integer 5 --分配给单个sql执行结果的内存最大比例%

    result_cache_max_size big integer 7872K --分配给result cache的最大内存

    result_cache_mode string MANUAL --表示只有在sql中写hint result cache,才可以缓存结果。

    result_cache_remote_expiration integer 0

    15:28:04 TC@ tc1>show parameter client_result

    NAME TYPE VALUE

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

    client_result_cache_lag big integer 3000 --设置服务器和客户端之间的数据同步时间间隔,间隔最长不能超过这个时间

    client_result_cache_size big integer 0 --客户端缓存的最大值

    15:36:35 TC@ tc1>

     --加hint,走result cache,直接从缓存中得到结果集

    5:25:07 TC@ tc1>set autot traceonly

    15:25:20 TC@ tc1>select /*+result_cache*/ count(*) from tmp_dy;

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

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

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

    | 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |

    | 1 | RESULT CACHE | 6pf1wm7ttmavh3aq1v2udnx0j7 | | | |

    | 2 | SORT AGGREGATE | | 1 | | |

    | 3 | TABLE ACCESS FULL| TMP_DY | 65 | 3 (0)| 00:00:01 |

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

    --加hint,不走result cache

    16:04:49 TC@ tc1>select /*+no_result_cache*/ count(*) from tmp_dy;

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

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

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

    | 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |

    | 1 | SORT AGGREGATE | | 1 | | |

    | 2 | TABLE ACCESS FULL| TMP_DY | 65 | 3 (0)| 00:00:01 |

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

    result cache的一些限制:

    1.临时表,sys,system下的表不走result cache

    2.CURRVAL,NEXTVAL, CURRENT_DATE, CURRENT_TIMESTAMP, LOCAL_TIMESTAMP, USERENV/SYS_CONTEXT (with non-constant variables), SYS_GUID, SYSDATE, and SYS_TIMESTAMP不走result cache

    --result统计信息表

    16:13:41 TC@ tc1>SELECT NAME, VALUE FROM V$RESULT_CACHE_STATISTICS;

    NAME VALUE

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

    Block Size (Bytes) 1024

    Block Count Maximum 7872

    Block Count Current 32

    Result Size Maximum (Blocks) 393

    Create Count Success 2

    Create Count Failure 0

    Find Count 6

    Invalidation Count 1

    Delete Count Invalid 0

    Delete Count Valid 0

    Hash Chain Length 1

    11 rows selected.

    SELECT ID, TYPE, CREATION_TIMESTAMP, BLOCK_COUNT, COLUMN_COUNT, PIN_COUNT, ROW_COUNT FROM V$RESULT_CACHE_OBJECTS ;

    SQLSET SERVEROUTPUT ON

    EXECUTE DBMS_RESULT_CACHE.MEMORY_REPORT

    其实result result这个功能mysql早就有了,原理上也是非常简单的,把数据不怎么变化的表,的某些sql执行结果缓存起来,然后每次从结果中去取就可以了。

    仅仅做个记录。

    http://download.oracle.com/docs/cd/E11882_01/server.112/e10821/memory.htm#PFGRF987 

同分类推荐文章

  1. 使用deepseek进行Oracle恢复,引起重大故障 (2026-06-22 10:56:00)
  2. 接手一个只差临门一脚的数据库恢复 (2026-06-18 00:13:09)
  3. 我做了一个 AI 版的 StarRocks 升级风险扫描工具,直接帮我定位到一个风险 (2026-06-15 01:00:00)

查看更多 数据库 文章 →

建议继续学习

  1. 为什么字段尽可能用NOT NULL,而不是NULL (累计阅读 8,512)
  2. MySQL优化 之 Discuz论坛MySQL通用优化 (累计阅读 7,727)
  3. 由12306.cn谈谈网站性能技术 (累计阅读 6,399)
  4. mysql sql 百万级数据库优化方案 (累计阅读 6,126)
  5. 冷热数据 (累计阅读 5,678)
  6. 详解MyISAM Key Cache(前篇) (累计阅读 5,000)
  7. 铁路订票网站个人的设计浅见 (累计阅读 4,548)
  8. 为什么长尾数据的翻页技术实现复杂 (累计阅读 4,555)
  9. 数据库开发规范 (累计阅读 4,439)
  10. 写好Hive 程序的五个提示 (累计阅读 4,016)