Skip to main content

Subprogram Inlining / Continue statement / Pl Sql Warnings

Subprogram Inlining / Continue statement / Pl Sql Warnings 


Subprogram inlining has been around in optimizing compilers for a long time.  With the 11g database, Oracle has included this feature in PL/SQL.  


Basically subprogram inlining is where the program calls a subprogram (procedure or function) and the compiler places the actual subprogram code in place of the call to that code.  Since the subprogram code is in the main code (INLINE), the overhead of calling that subprogram is saved.  This allows you to program using a modular method, producing readable, maintainable source code, while the compiler optimizes the code to make it more efficient.

-------------------   inlining subprograms ---------------

Oracle 11g has taken compiler optimisation further, in particular with the concept of subprogram inlining.


With subprogram inlining, Oracle will replace a call to a subroutine (such as a function) with the subroutine code itself during compilation. One of the benefits of this is that we can continue to write well-structured, modular code without any performance penalties. For SQL-intensive PL/SQL programs, the gains from inlining might be marginal, but for procedural code, inlining might provide some greater optimisation benefits, as we will see in this article.


If the PLSQL_OPTIMIZER_LEVEL is set to 3 the compiler will automatically try and inline subprograms. 



 If the PLSQL_OPTIMIZE_LEVEL is set to 2 (the default) it will not automatically try and inline subprograms.  So the PRAGMA INLINE is used to tell the optimizer to inline or not to inline depending on the setting of the optimizer level.


ALTER SESSION set PLSQL_OPTIMIZE_LEVEL = 2;

-----------------------------------------


The CONTINUE statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. In other words, it forces the next iteration of the loop to take place, skipping any code in between.


DECLARE 

   a number(2) := 10; 

BEGIN 

   -- while loop execution  

   WHILE a < 20 LOOP 

      dbms_output.put_line ('value of a: ' || a); 

      a := a + 1; 

      IF a = 15 THEN 

         -- skip the loop using the CONTINUE statement 

         a := a + 1; 

         CONTINUE; 

      END IF; 

   END LOOP; 

END; 

value of a: 10 

value of a: 11 

value of a: 12 

value of a: 13 

value of a: 14 

value of a: 16 

value of a: 17 

value of a: 18 

value of a: 19



-------------------------------------------------------

To avoid runtime bugs, Oracle 10g introduced the PLSQL_WARNINGS initialization parameter. It will not stop compiling the program but will only give a compile time warning about inefficient coding.Lets see how we can use it.

First we need to enable compile time warning. It can be enabled for session or system.

We can use the ALTER SESSION SET PLSQL_WARNINGS='ENABLE:ALL' or the DBMS_WARNING package.





 CREATE OR REPLACE PROCEDURE test_warnings AS

      l_dummy  VARCHAR2(10) := '1';

    BEGIN

      IF 1=1 THEN

        SELECT '2'

        INTO   l_dummy

        FROM   dual;

      ELSE

        RAISE_APPLICATION_ERROR(-20000, 'l_dummy != 1!');

     END IF;

   END;

   /


Procedure created.


SQL> ALTER SESSION SET PLSQL_WARNINGS='ENABLE:ALL';


Session altered.


-- run the same code to display warnings


 CREATE OR REPLACE PROCEDURE test_warnings AS

      l_dummy  VARCHAR2(10) := '1';

    BEGIN

      IF 1=1 THEN

        SELECT '2'

        INTO   l_dummy

        FROM   dual;

      ELSE

        RAISE_APPLICATION_ERROR(-20000, 'l_dummy != 1!');

     END IF;

   END;


SP2-0804: Procedure created with compilation warnings


SQL> show err

Errors for PROCEDURE TEST_WARNINGS:


LINE/COL ERROR

-----------------------------------------------------------------

9/5      PLW-06002: Unreachable code

SQL> 



  

Comments

Sourav Nandy said…
Greate explanation. Exactly to the point.

Popular posts from this blog

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C   NUMBER(3); BEGIN WHILE B <

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7     1.      Introduction This document outlines a mini project for the RDBMS LOT

sample tables

  --sample tables DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;   CREATE TABLE EMP        (EMPNO NUMBER(4) NOT NULL,         ENAME VARCHAR2(10),         JOB VARCHAR2(9),         MGR NUMBER(4),         HIREDATE DATE,         SAL NUMBER(7, 2),         COMM NUMBER(7, 2),         DEPTNO NUMBER(2));   INSERT INTO EMP VALUES         (7369, 'SMITH',   'CLERK',      7902,         TO_DATE('17-DEC-1980', 'DD-MON-YYYY'),   800, NULL, 20); INSERT INTO EMP VALUES         (7499, 'ALLEN',   'SALESMAN',   7698,         TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600,   300, 30); INSERT INTO EMP VALUES         (7521, 'WARD',    'SALESMAN',   7698,         TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250,   500, 30); INSERT INTO EMP VALUES         (7566, 'JONES',   'MANAGER',    7839,         TO_DATE('2-APR-1981', 'DD-MON-YYYY'),   2975, NULL,