Oracle OCP 1Z0-050(39题)解析

本文深入探讨Oracle数据库中使用RESULT_CACHE属性的PL/SQL函数get_dept_avg的缓存机制,解释了缓存如何在函数调用时工作,以及在何种情况下缓存结果会失效。特别关注函数依赖的表结构变化对缓存的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oracle OCP 1Z0-050(39题)解析

QUESTION 39:

Evaluate the following function code:

CREATE FUNCTION get_dept_avg(dept_id NUMBER)

RETURN NUMBER

RESULT_CACHE RELIES_ON (EMPLOYEES)

IS

avgsal NUMBER(6);

BEGIN

SELECT AVG(SALARY)INTO avgsal

FROM EMPLOYEES

WHERE DEPARTMENT_ID = dept_id;

RETURN avgsal;

END get_dept_avg;

Which statement is true regarding the above function?

A. The cached result becomes invalid when any structural change is done to the EMPLOYEES table.

B. If the function is invoked with a different parameter value, the existing result in the result cache gets overwritten by the latest value.

C. Each time the function is invoked in a different session, the current result in the result cache gets overwritten.

D. If the function execution results in an unhandled exception, the exception result is also stored in the cache.

Answer: A

解析:

这个题目定义了一个PLSQL函数get_dept_avg,并且使用了RESULT_CACHE RELIES_ON子句,当EMPLOYEES表的结构发生改变时,缓存的结果集信息就无效了。

PL/SQL 函数高速缓存使用:

(1)  在程序包的函数声明部分或函数定义中包括RESULT_CACHE选项。

(2)  可以选择包括RELIES_ON 子句,以指定函数结果依赖的任何表或视图。

 

例如:

CREATE OR REPLACE FUNCTION productName

(prod_id NUMBER, lang_id VARCHAR2)

RETURN NVARCHAR2

RESULT_CACHE RELIES_ON (product_descriptions)

IS

result VARCHAR2(50);

BEGIN

SELECT translated_name INTO result

FROM product_descriptions

WHERE product_id = prod_id AND language_id= lang_id;

RETURN result;

END;

使用说明:

(1)如果函数的执行导致了未处理的异常错误,该异常结果不会存储在高速缓存中。

(2)在以下情况下执行高速缓存结果的函数主体:

- 此数据库实例上的会话第一次使用这些参数值调用该函数。这些参数值的高速缓存结果无效。

- 当在函数定义的RELIES_ON 子句中指定的任何数据库对象发生更改时,高速缓存的结果就会失效。

- 这些参数值的高速缓存结果已过时。如果系统需要更多内存,它可能会放弃最早的高速缓存值。

- 该函数绕过高速缓存。

(3)该函数不应有任何负作用。

(4)该函数不应依赖特定于会话的设置。

(5)该函数不应依赖特定于会话的应用程序上下文。

参考Oracle官方文档

https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/subprograms.htm#BABCDCFA

https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/subprograms.htm#LNPLS700

Developing Applications with Result-Cached Functions

When developing an application that uses a result-cached function, make no assumptions about the number of times the body of the function will run for a given set of parameter values.

Some situations in which the body of a result-cached function runs are:

  • The first time a session on this database instance invokes the function with these parameter values

  • When the cached result for these parameter values is invalid

    When a change to any data source on which the function depends is committed, the cached result becomes invalid.

  • When the cached results for these parameter values have aged out

    If the system needs memory, it might discard the oldest cached values.

  • When the function bypasses the cache (see "Result Cache Bypass")

 

Restrictions on Result-Cached Functions

To be result-cached, a function must meet all of these criteria:

  • It is not defined in a module that has invoker's rights or in an anonymous block.

  • It is not a pipelined table function.

  • It is recommended that a result-cached function also meet these criteria:

  • It has no side effects.

    For information about side effects, see "Subprogram Side Effects".

  • It does not depend on session-specific settings.

    For more information, see "Making Result-Cached Functions Handle Session-Specific Settings".

  • It does not depend on session-specific application contexts.

    For more information, see "Making Result-Cached Functions Handle Session-Specific Application Contexts".

  •  
  • It does not reference dictionary tables, temporary tables, sequences, or nondeterministic SQL functions.

    For more information, see Oracle Database Performance Tuning Guide.

  • It has no OUT or IN OUT parameters.

  • No IN parameter has one of these types:

    • BLOB

    • CLOB

    • NCLOB

    • REF CURSOR

    • Collection

    • Object

    • Record

  • The return type is none of these:

    • BLOB

    • CLOB

    • NCLOB

    • REF CURSOR

    • Object

    • Record or PL/SQL collection that contains an unsupported return type

Examples of Result-Cached Functions

The best candidates for result-caching are functions that are invoked frequently but depend on information that changes infrequently (as might be the case in the first example). Result-caching avoids redundant computations in recursive functions.

Examples:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值