Sub topic 13
Performance options: DETERMINISTIC, RESULT_CACHE, and NOCOPY
Assume you know basic PL/SQL subprograms, parameter modes, exceptions, and anonymous blocks. This chapter covers three declarations that can reduce repeated work or parameter-copying overhead. Each one makes a promise or changes behavior. Use it only when that promise is true.
Choosing the right option
| Option | Main purpose | Important risk |
|---|---|---|
| `DETERMINISTIC` | Tell Oracle that the same inputs produce the same output | Incorrect results if the function also reads changing state |
| `RESULT_CACHE` | Store function results for reuse | Stale or unusable results when dependencies change |
| `NOCOPY` | Suggest passing `OUT` or `IN OUT` data by reference | Actual parameters can change even when the procedure raises an exception |
These options solve different problems. DETERMINISTIC describes a function. RESULT_CACHE stores results. NOCOPY affects parameter transfer.
`DETERMINISTIC`: a promise about inputs and outputs
A deterministic function returns the same value whenever its input values are the same. It must not depend on the current time, session settings, random numbers, package variables, or changing table data unless those values are included in the inputs.
CREATE OR REPLACE FUNCTION sales_tax (
p_amount IN NUMBER,
p_rate IN NUMBER
) RETURN NUMBER
DETERMINISTIC
IS
BEGIN
RETURN p_amount * p_rate;
END sales_tax;
/DETERMINISTIC does not force Oracle to cache every result. It gives Oracle permission to reuse a result in contexts such as function-based indexes or query optimization. Oracle does not verify that your function keeps its promise.
`RESULT_CACHE`: store reusable results
RESULT_CACHE asks Oracle to save a function's output in the database result cache. Later calls with matching inputs can avoid running the function again. Oracle tracks referenced database objects and can invalidate cached results when those objects change.
CREATE OR REPLACE FUNCTION shipping_fee (
p_weight IN NUMBER
) RETURN NUMBER
RESULT_CACHE
IS
BEGIN
RETURN CASE
WHEN p_weight <= 1 THEN 5
WHEN p_weight <= 5 THEN 10
ELSE 20
END;
END shipping_fee;
/Use it for expensive, read-only functions with repeated inputs. Avoid it for results that are cheap to calculate, highly unique, session-specific, or dependent on untracked external state. RESULT_CACHE is a caching feature, not a replacement for correct transaction design.
`NOCOPY`: reduce parameter copying
For OUT and IN OUT parameters, PL/SQL can copy values when entering and leaving a procedure. NOCOPY is a compiler hint that allows the parameter to be passed by reference instead.
CREATE OR REPLACE PROCEDURE add_suffix (
p_text IN OUT NOCOPY VARCHAR2
) IS
BEGIN
p_text := p_text || ' (checked)';
END add_suffix;
/NOCOPY can help with large collections or records. It is only a hint, so Oracle may still copy the value. Because the caller's variable may change before an exception is raised, do not use NOCOPY when rollback-like behavior is required.
Practical rules
- Use
DETERMINISTIConly for genuinely stable functions. - Use
RESULT_CACHEafter measuring repeated, expensive calls. - Use
NOCOPYfor large mutable values when partial changes are acceptable. - Test behavior during exceptions, commits, dependency changes, and concurrent sessions.
Exercise
Which option is most appropriate for a function that calculates a tax amount from only an amount and a supplied tax rate?
Show answer
DETERMINISTIC, because the same two input values always produce the same result.