PL/SQL Optimizer
Prior to Oracle Database 10g release 1, the PL/SQL compiler translated your source text to system code without applying many changes to improve performance. Now, PL/SQL uses an optimizer that can rearrange code for better performance.
The optimizer is enabled by default. In rare cases, if the overhead of the optimizer makes compilation of very large applications too slow, you can lower the optimization by setting the compilation parameter PLSQL_OPTIMIZE_LEVEL=1
instead of its default value 2. In even rarer cases, PL/SQL might raise an exception earlier than expected or not at all. Setting PLSQL_OPTIMIZE_LEVEL=1
prevents the code from being rearranged.
See Also:
-
Oracle Database Reference for information about the
PLSQL_OPTIMIZE_LEVEL
compilation parameter -
Oracle Database Development Guide for examples of changing the
PLSQL_OPTIMIZE_LEVEL
compilation parameter -
Oracle Database Reference for information about the static dictionary view
ALL_PLSQL_OBJECT_SETTINGS
Subprogram Inlining
One optimization that the compiler can perform is subprogram inlining.
Subprogram inlining replaces a subprogram invocation with a copy of the invoked subprogram (if the invoked and invoking subprograms are in the same program unit). To allow subprogram inlining, either accept the default value of the PLSQL_OPTIMIZE_LEVEL
compilation parameter (which is 2) or set it to 3.
With PLSQL_OPTIMIZE_LEVEL=2
, you must specify each subprogram to be inlined with the INLINE
pragma:
PRAGMA INLINE (subprogram, 'YES')
If subprogram
is overloaded, then the preceding pragma applies to every subprogram with that name.
With PLSQL_OPTIMIZE_LEVEL=3
, the PL/SQL compiler seeks opportunities to inline subprograms. You need not specify subprograms to be inlined. However, you can use the INLINE
pragma (with the preceding syntax) to give a subprogram a high priority for inlining, and then the compiler inlines it unless other considerations or limits make the inlining undesirable.
If a particular subprogram is inlined, performance almost always improves. However, because the compiler inlines subprograms early in the optimization process, it is possible for subprogram inlining to preclude later, more powerful optimizations.
If subprogram inlining slows the performance of a particular PL/SQL program, then use the PL/SQL hierarchical profiler to identify subprograms for which you want to turn off inlining. To turn off inlining for a subprogram, use the INLINE
pragma:
PRAGMA INLINE (subprogram, 'NO')
The INLINE
pragma affects only the immediately following declaration or statement, and only some kinds of statements.
When the INLINE
pragma immediately precedes a declaration, it affects:
-
Every invocation of the specified subprogram in that declaration
-
Every initialization value in that declaration except the default initialization values of records
When the INLINE
pragma immediately precedes one of these statements, the pragma affects every invocation of the specified subprogram in that statement:
-
Assignment
-
CALL
-
Conditional
-
CASE
-
CONTINUE
WHEN
-
EXECUTE
IMMEDIATE
-
EXIT
WHEN
-
LOOP
-
RETURN
The INLINE
pragma does not affect statements that are not in the preceding list.
Multiple pragmas can affect the same declaration or statement. Each pragma applies its own effect to the statement. If PRAGMA
INLINE(
subprogram,
'YES')
and PRAGMA
INLINE(
identifier,
'NO')
have the same subprogram
, then 'NO'
overrides 'YES'
. One PRAGMA
INLINE(
subprogram,
'NO')
overrides any number of occurrences of PRAGMA
INLINE(
subprogram,
'YES')
, and the order of these pragmas is not important.
See Also:
-
Oracle Database Development Guide for more information about PL/SQL hierarchical profiler
-
Oracle Database Reference for information about the
PLSQL_OPTIMIZE_LEVEL
compilation parameter -
Oracle Database Reference for information about the static dictionary view
ALL_PLSQL_OBJECT_SETTINGS
Example 13-1 Specifying that Subprogram Is To Be Inlined
In this example, if PLSQL_OPTIMIZE_LEVEL=2
, the INLINE
pragma affects the procedure invocations p1(1)
and p1(2)
, but not the procedure invocations p1(3)
and p1(4)
.
PROCEDURE p1 (x PLS_INTEGER) IS ...
...
PRAGMA INLINE (p1, 'YES');
x:= p1(1) + p1(2) + 17; -- These 2 invocations to p1 are inlined
...
x:= p1(3) + p1(4) + 17; -- These 2 invocations to p1 are not inlined
...
Example 13-2 Specifying that Overloaded Subprogram Is To Be Inlined
In this example, if PLSQL_OPTIMIZE_LEVEL=2
, the INLINE
pragma affects both functions named p2
.
FUNCTION p2 (p boolean) return PLS_INTEGER IS ...
FUNCTION p2 (x PLS_INTEGER) return PLS_INTEGER IS ...
...
PRAGMA INLINE(p2, 'YES');
x := p2(true) + p2(3);
...
Example 13-3 Specifying that Subprogram Is Not To Be Inlined
In this example, the INLINE
pragma affects the procedure invocations p1(1)
and p1(2)
, but not the procedure invocations p1(3)
and p1(4)
.
PROCEDURE p1 (x PLS_INTEGER) IS ...
...
PRAGMA INLINE (p1, 'NO');
x:= p1(1) + p1(2) + 17; -- These 2 invocations to p1 are not inlined
...
x:= p1(3) + p1(4) + 17; -- These 2 invocations to p1 might be inlined
...
Example 13-4 PRAGMA INLINE ... 'NO' Overrides PRAGMA INLINE ... 'YES'
In this example, the second INLINE
pragma overrides both the first and third INLINE
pragmas.
PROCEDURE p1 (x PLS_INTEGER) IS ... ... PRAGMA INLINE (p1, 'YES'); PRAGMA INLINE (p1, 'NO'); PRAGMA INLINE (p1, 'YES'); x:= p1(1) + p1(2) + 17; -- These 2 invocations to p1 are not inlined ...