gpt4 book ai didi

oracle - 确定性函数何时使用先前的计算值?

转载 作者:行者123 更新时间:2023-12-04 14:45:29 25 4
gpt4 key购买 nike

考虑一个确定性函数,例如:

CREATE OR REPLACE FUNCTION SCHEMA.GET_NAME(ss_id nvarchar2
) RETURN nvarchar2 DETERMINISTIC IS
tmpVar nvarchar2(500);
BEGIN

select name into tmpvar from logistics.organization_items
where id = ss_id ;
return tmpvar ;

END ss_name;

使用 Toad 我调用了 SCHEMA.GET_NAME(1)它返回 A .然后我将表中的值从 A 更改为至 B并回顾 SCHEMA.GET_NAME(1)返回 B .

这是一个很好的结果。但是我怕值没有根据 this page in the documentation更新,其中说:

When Oracle Database encounters a deterministic function in one of these contexts, it attempts to use previously calculated results when possible rather than reexecuting the function. If you subsequently change the semantics of the function, you must manually rebuild all dependent function-based indexes and materialized views.


GET_NAME(1)的值在什么情况下会出现返回一个旧的缓存值( A 而不是 B )?

最佳答案

如果您从表中选择,那么您的函数的结果是不确定的。一个 deterministic system在给定相同初始条件的情况下,它总是会产生相同的输出。

可以更改表中的信息,因此从表中选择的函数不是确定性的。引用 PL/SQL Language Reference :

Do not specify this clause to define a function that uses package variables or that accesses the database in any way that might affect the return result of the function. The results of doing so are not captured if the database chooses not to reexecute the function.



换句话说,Oracle 不保证函数的结果是准确的(它们只是可能)。如果您的 table 是静态的,并且不太可能改变,那么应该没问题,但这不是我想要依赖的东西。要回答您的问题,请不要假设 Oracle 将在同一事务/ session 中返回缓存值以外的任何内容。

如果您需要加快速度,有两种方法。首先,检查您在 ID 上是否有索引!
  • 只需加入此表。如果你的功能只是这个,那么这个功能就不需要存在了。
  • 使用scalar sub-query caching (不一定可能,但值得一试)。
    select ( select get_name(:id) from dual )
    from your_table

    Oracle 将创建函数结果的内存散列,如结果缓存。如果您多次执行相同的函数,那么 Oracle 将命中缓存而不是函数。
  • 关于oracle - 确定性函数何时使用先前的计算值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21400818/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com