Oracle On the PL/SQL Function Result Cache

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 标签PostgreSQL , Oracle , 函数结果缓存 , 函数三态 , immutable , stable , volatile背景Oracle 11g 支持的一个新特性,在创建PL/SQL函数时,可以指定这个存储过程是否需要对结果进行缓存,缓存内容在SGA内存区域。

标签

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


背景

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

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

详见

https://www.oracle.com/technetwork/testcontent/o57plsql-088600.html

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这类中间件来实现。

参考

https://www.oracle.com/technetwork/testcontent/o57plsql-088600.html

《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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
7天前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL游标属性:数据的“导航仪”与“仪表盘”
【4月更文挑战第19天】Oracle PL/SQL游标属性如同车辆的导航仪和仪表盘,提供丰富信息和控制。 `%FOUND`和`%NOTFOUND`指示数据读取状态,`%ROWCOUNT`记录处理行数,`%ISOPEN`显示游标状态。还有`%BULK_ROWCOUNT`和`%BULK_EXCEPTIONS`增强处理灵活性。通过实例展示了如何在数据处理中利用这些属性监控和控制流程,提高效率和准确性。掌握游标属性是提升数据处理能力的关键。
|
7天前
|
SQL Oracle 安全
Oracle的PL/SQL循环语句:数据的“旋转木马”与“无限之旅”
【4月更文挑战第19天】Oracle PL/SQL中的循环语句(LOOP、EXIT WHEN、FOR、WHILE)是处理数据的关键工具,用于批量操作、报表生成和复杂业务逻辑。LOOP提供无限循环,可通过EXIT WHEN设定退出条件;FOR循环适用于固定次数迭代,WHILE循环基于条件判断执行。有效使用循环能提高效率,但需注意避免无限循环和优化大数据处理性能。掌握循环语句,将使数据处理更加高效和便捷。
|
7天前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL条件控制:数据的“红绿灯”与“分岔路”
【4月更文挑战第19天】在Oracle PL/SQL中,IF语句与CASE语句扮演着数据流程控制的关键角色。IF语句如红绿灯,依据条件决定程序执行路径;ELSE和ELSIF提供多分支逻辑。CASE语句则是分岔路,按表达式值选择执行路径。这些条件控制语句在数据验证、错误处理和业务逻辑中不可或缺,通过巧妙运用能实现高效程序逻辑,保障数据正确流转,支持企业业务发展。理解并熟练掌握这些语句的使用是成为合格数据管理员的重要一环。
|
SQL 存储 Oracle
|
15天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改
|
25天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0
|
15天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
88 6
|
2天前
|
SQL 数据管理 关系型数据库
如何在 Windows 上安装 SQL Server,保姆级教程来了!
在Windows上安装SQL Server的详细步骤包括:从官方下载安装程序(如Developer版),选择自定义安装,指定安装位置(非C盘),接受许可条款,选中Microsoft更新,忽略警告,取消“适用于SQL Server的Azure”选项,仅勾选必要功能(不包括Analysis Services)并更改实例目录至非C盘,选择默认实例和Windows身份验证模式,添加当前用户,最后点击安装并等待完成。安装成功后关闭窗口。后续文章将介绍SSMS的安装。
6 0
|
10天前
|
SQL 安全 网络安全
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
23 0

推荐镜像

更多