Oracle On the PL/SQL Function Result Cache

简介: 标签PostgreSQL , Oracle , 函数结果缓存 , 函数三态 , immutable , stable , volatile背景Oracle 11g 支持的一个新特性,在创建PL/SQL函数时,可以指定这个存储过程是否需要对结果进行缓存,缓存内容在SGA内存区域。

标签

PostgreSQL , Oracle , 函数结果缓存 , 函数三态 , immutable , stable , volatile


背景

Oracle 11g 支持的一个新特性,在创建PL/SQL函数时,可以指定这个存储过程是否需要对结果进行缓存,缓存内容在SGA内存区域。

如果这个函数的输入参数未变化,同时指定的表数据没有发生变化,那么缓存有效,直接从缓存中获取结果。

详见

In Oracle Database 11g, however, we can add a line to the header of this function as follows:

FUNCTION one_employee (employee_id_in   
IN employees.employee_id%TYPE)  
   RETURN employees%ROWTYPE  
   RESULT_CACHE RELIES_ON (employees)  
IS  
    l_employee   employees%ROWTYPE;  
BEGIN  

RESULT_CACHE 表示这个函数支持结果缓存,当输入参数未变化时,直接从缓存获取结果。

relies_on (employees)表示,当employees表未变化时,缓存有效,当这个表有变化,整个缓存全部失效。

通常用在OLAP业务系统中,用于缓存结果。

This RESULT_CACHE clause tells Oracle Database that it should remember (store in a special in-memory result cache) each record retrieved for a specific employee ID number. And when a session executes this function and passes in an employee ID that was previously stored, the PL/SQL runtime engine will not execute the function body, which includes that query.

Instead, it will simply retrieve the record from the cache and return that data immediately. The result is much faster retrieval.

In addition, by specifying RELIES_ON (employees), we inform Oracle Database that if any session commits changes to that table, any data in the result cache drawn from the table must be invalidated. The next call to the one_employee function would then have to execute the query and retrieve the data fresh from the table.

Because the cache is a part of the System Global Area (SGA), its contents are available to all sessions connected to the instance. Furthermore, Oracle Database will apply its "least recently used algorithm" to the cache, to ensure that the most recently accessed data will be preserved in the cache.

Prior to Oracle Database 11g, a similar kind of caching was possible with package-level collections, but this cache is session-specific and located in the Process Global Area (PGA). This means that if I have 1,000 different sessions running the application, I could use up an enormous amount of memory in addition to that consumed by the SGA.

The PL/SQL function result cache minimizes the amount of memory needed to cache and share this data across all sessions. This low memory profile, plus the automatic purge of cached results whenever changes are committed, makes this feature of Oracle Database 11g very practical for optimizing performance in PL/SQL applications.

PostgreSQL 函数 稳定性 - 并非cache

在一个语句中函数被多次调用时,如果函数被多次调用,并且输入的参数为常量(不变时),这个函数需要被执行多少次?

postgres=# create or replace function f1(int) returns int as $$  
declare begin raise notice '1'; return 1; end;  
$$ language plpgsql strict stable;  
CREATE FUNCTION  
postgres=# select f1(1) from generate_series(1,5);  
NOTICE:  1  
NOTICE:  1  
NOTICE:  1  
NOTICE:  1  
NOTICE:  1  
 f1   
----  
  1  
  1  
  1  
  1  
  1  
(5 rows)  
  
postgres=# create or replace function f1(int) returns int as $$  
declare begin raise notice '1'; return 1; end;  
$$ language plpgsql strict immutable;  
CREATE FUNCTION  
postgres=# select f1(1) from generate_series(1,5);  
NOTICE:  1  
 f1   
----  
  1  
  1  
  1  
  1  
  1  
(5 rows)  
postgres=# create or replace function f1(int) returns int as $$  
declare begin raise notice '1'; return 1; end;  
$$ language plpgsql strict stable;  
CREATE FUNCTION  
postgres=# explain verbose select f1(1) from generate_series(1,5);  
                                     QUERY PLAN                                       
------------------------------------------------------------------------------------  
 Function Scan on pg_catalog.generate_series  (cost=0.00..216.94 rows=1000 width=4)  
   Output: f1(1)  
   Function Call: generate_series(1, 5)  
(3 rows)  
  
postgres=# create or replace function f1(int) returns int as $$  
declare begin raise notice '1'; return 1; end;  
$$ language plpgsql strict immutable;  
CREATE FUNCTION  
postgres=#   
postgres=# explain verbose select f1(1) from generate_series(1,5);  
NOTICE:  1  
                                    QUERY PLAN                                      
----------------------------------------------------------------------------------  
 Function Scan on pg_catalog.generate_series  (cost=0.00..0.19 rows=1000 width=4)  
   Output: 1  
   Function Call: generate_series(1, 5)  
(3 rows)  

原理详见本文末尾PostgreSQL函数三态的介绍。

会话级语句结果缓存,暂时PG内核层面没有支持,可以通过pgpool-ii这类中间件来实现。

参考

《PostgreSQL Oracle 兼容性之 - PL/SQL DETERMINISTIC 与PG函数稳定性(immutable, stable, volatile)》

《PostgreSQL 函数稳定性与constraint_excluded分区表逻辑推理过滤的CASE》

《函数稳定性讲解 - retalk PostgreSQL function's [ volatile|stable|immutable ]》

《函数稳定性讲解 - 函数索引思考, pay attention to function index used in PostgreSQL》

《函数稳定性讲解 - Thinking PostgreSQL Function's Volatility Categories》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
SQL Oracle 关系型数据库
Oracle数据库创建表空间和索引的SQL语法示例
以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
337 8
|
8月前
|
SQL Oracle 关系型数据库
解决大小写、保留字与特殊字符问题!Oracle双引号在SQL中的特殊应用
在Oracle数据库开发中,双引号的使用是一个重要但易被忽视的细节。本文全面解析了双引号在SQL中的特殊应用场景,包括解决标识符与保留字冲突、强制保留大小写、支持特殊字符和数字开头标识符等。同时提供了最佳实践建议,帮助开发者规避常见错误,提高代码可维护性和效率。
314 6
|
9月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
|
9月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】yashandb执行包含带oracle dblink表的sql时性能差
【YashanDB知识库】yashandb执行包含带oracle dblink表的sql时性能差
|
9月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
本文来自YashanDB官网,介绍如何处理Oracle客户端sql*plus中使用@@调用同级目录SQL脚本的场景。崖山数据库23.2.x.100已支持@@用法,但旧版本可通过Python脚本批量重写SQL文件,将@@替换为绝对路径。文章通过Oracle示例展示了具体用法,并提供Python脚本实现自动化处理,最后调整批处理脚本以适配YashanDB运行环境。
|
11月前
|
SQL Oracle 关系型数据库
如何在 Oracle 中配置和使用 SQL Profiles 来优化查询性能?
在 Oracle 数据库中,SQL Profiles 是优化查询性能的工具,通过提供额外统计信息帮助生成更有效的执行计划。配置和使用步骤包括:1. 启用自动 SQL 调优;2. 手动创建 SQL Profile,涉及收集、执行调优任务、查看报告及应用建议;3. 验证效果;4. 使用 `DBA_SQL_PROFILES` 视图管理 Profile。
|
SQL 数据采集 监控
局域网监控电脑屏幕软件:PL/SQL 实现的数据库关联监控
在当今网络环境中,基于PL/SQL的局域网监控系统对于企业和机构的信息安全至关重要。该系统包括屏幕数据采集、数据处理与分析、数据库关联与存储三个核心模块,能够提供全面而准确的监控信息,帮助管理者有效监督局域网内的电脑使用情况。
182 2
|
SQL 监控 Oracle
Oracle SQL性能优化全面指南
在数据库管理领域,Oracle SQL性能优化是确保数据库高效运行和数据查询速度的关键
1566 6
|
SQL 存储 Oracle
Oracle数据库SQL语句详解与应用指南
在数字化时代,数据库已成为各类企业和组织不可或缺的核心组件。Oracle数据库作为业界领先的数据库管理系统之一,广泛应用于各种业务场景。掌握Oracle数据库的SQL语句是数据库管理员、开发人员及运维人员的基本技能。本文将详细介绍Oracle数据库SQL语句的基本概念、语法、应用及最佳实践。一、Or
507 3
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
333 1

推荐镜像

更多