Exforsys

H I D E

Home arrow Technical Training arrow Oracle 11g Tutorial

Oracle Pragma Inline

Page 4 of 4
Author: Saurabh Gupta     Published on: 6th Jul 2011

Oracle Pragma

Oracle Pragma Inline

Inlining is an effective feature to gain performance benefits in a procedural and structural code. During compilation, oracle tends to replace the inline subprogram call with its definition itself.

The INLINE pragma is a latest induction in the family. It is used to manually inlining a subprogram when the optimizer level is set as 2. It has to be specified once, just before the subprogram call. The inlining effect would remain persistent for all the subsequent calls to the subprogram; unless and until the NO pragma is specified for the subprogram.

Oracle Pragma Inline Example Syntax

Sample Code
  1. PRAGMA INLINE (subprogram name, [YES | NO]);
Copyright exforsys.com


For quick reference, PLSQL_OPTIMIZE_LEVEL parameter specifies the optimization level of the subprogram.

• If it is 0, then the optimizer follows ‘NO inlining’ principle.

• For the value of 1, the optimizer follows intermediate inlining like working with iterative constants.

• For optimizer level 2, the optimizer takes the path of ‘Intelligent inlining’ i.e. it would inline the subprogram by virtue of its intelligence. This is the appropriate and default level for the optimizer and provides scalability to manually inline the subprograms using PRAGMA INLINE.

• At optimizer level 3, optimizer is forced to inline all possible subprograms at high priority, which is not commendable from developer’s perspective.

Oracle Pragma Inline Example Code

For demonstration purpose, the session has been altered to set PLSQL_OPTIMIZE_LEVEL at 2.

Sample Code
  1. ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2
  2.  
  3. Session altered.
Copyright exforsys.com


Ads

Oracle Pragma Inline Example Code

Now, for example, I have to calculate sum of factorial series in the format as 1! + 2! + 3! +…+N! I create a procedure P_CALC_SERIES and define a local function FACTORIAL to get the factorial of a number. I would inline the local function call using PRAGMA INLINE as shown below.

Sample Code
  1. CREATE OR REPLACE PROCEDURE P_CALC_SERIES(P_LIMIT NUMBER)
  2. IS
  3.   L_SERIES NUMBER := 0;
  4.   FUNCTION FACTORIAL (P_NUM NUMBER)    RETURN NUMBER IS
  5.     L_FACT NUMBER := 1;
  6.   BEGIN
  7.     FOR I IN 1..P_NUM
  8.     LOOP
  9.        L_FACT := L_FACT*I;
  10.     END LOOP;
  11.     RETURN L_FACT;
  12.   END FACTORIAL;
  13.  
  14. BEGIN
  15.   FOR J IN 1 .. P_LIMIT
  16.   LOOP
  17.     PRAGMA INLINE (FACTORIAL, 'YES');
  18.     L_SERIES := L_SERIES + FACTORIAL(I);
  19.   END LOOP;
  20.   DBMS_OUTPUT.PUT_LINE('Sum of the series is '||TO_CHAR(L_SERIES));
  21. END;
  22. /
  23.  
  24. Procedure created.
Copyright exforsys.com


Read Next: UTL_FILE


 
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.

Oracle 11g Tutorial

 

Comments