`
winie
  • 浏览: 222403 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

Oracle: Automatic Subprogram Inlining

 
阅读更多

Every call to a procedure or function causes a slight, but measurable, performance overhead, which is especially noticeable when the subprogram is called within a loop. Avoiding procedures and functions is not an option, as it goes against the concept of modular programming, making programs bulky and difficult to manage. Automatic subprogram inlining can reduce the overheads associated with calling subprograms, whilst leaving your original source code in its normal modular state. This is done by replacing the subprogram calls with a copy of the code in the subprogram at compile time.

The process of subprogram inlining is controlled by thePLSQL_OPTIMIZE_LEVELparameter and theINLINEpragma. WhenPLSQL_OPTIMIZE_LEVEL=2(the default), theINLINEpragma determines whether the following statement or declaration should be inlined or not. WhenPLSQL_OPTIMIZE_LEVEL=3, the optimizer may inline code automatically. In this case theINLINEpragma can turn it off inlining for a statement, or increase the likelihood that the optimizer will choose to inline a statement. The relationship is easier to understand when you see the following example.

These tests use an anonymous block with a function defined in the declaration block. The function is then called repeatedly in a loop. The settings forPLSQL_OPTIMIZE_LEVELand theINLINEpragma are altered to switch subprogram inlining on and off. First, we make surePLSQL_OPTIMIZE_LEVEL=2and run the code with noINLINEpragma set. With these settings we would not expect to see subprogram inlining taking place.

ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2;

SET SERVEROUTPUT ON
DECLARE
  l_loops  NUMBER := 10000000;
  l_start  NUMBER;
  l_return NUMBER;

  FUNCTION add_numbers (p_1 IN NUMBER,
                        p_2 IN NUMBER)
    RETURN NUMBER AS
  BEGIN
    RETURN p_1 + p_2;
  END add_numbers;

BEGIN
  l_start := DBMS_UTILITY.get_time;

  FOR i IN 1 .. l_loops LOOP
    --PRAGMA INLINE (add_numbers, 'YES');
    l_return := add_numbers(1, i);
  END LOOP;

  DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');
END;
/
Elapsed Time: 509 hsecs

PL/SQL procedure successfully completed.

SQL>

This results in an elapsed time of 509 hsecs.

Next, we keep the same optimization setting, but include theINLINEpragma with a setting of "YES" for the calls to theADD_NUMBERSfunction. We would now expect subprogram inlining to take place.

ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2;

SET SERVEROUTPUT ON
DECLARE
  l_loops  NUMBER := 10000000;
  l_start  NUMBER;
  l_return NUMBER;

  FUNCTION add_numbers (p_1 IN NUMBER,
                        p_2 IN NUMBER)
    RETURN NUMBER AS
  BEGIN
    RETURN p_1 + p_2;
  END add_numbers;

BEGIN
  l_start := DBMS_UTILITY.get_time;

  FOR i IN 1 .. l_loops LOOP
    PRAGMA INLINE (add_numbers, 'YES');
    l_return := add_numbers(1, i);
  END LOOP;

  DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');
END;
/
Elapsed Time: 245 hsecs

PL/SQL procedure successfully completed.

SQL>

This gives an elapsed time of 245 hsec, which is approximately half that of the previous test, implying that subprogram inlining is taking place.

Next, we make surePLSQL_OPTIMIZE_LEVEL=3and run the code with noINLINEpragma set. We would now expect the optimizer to implicitly choose to inline theADD_NUMBERScall.

ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=3;

SET SERVEROUTPUT ON
DECLARE
  l_loops  NUMBER := 10000000;
  l_start  NUMBER;
  l_return NUMBER;

  FUNCTION add_numbers (p_1 IN NUMBER,
                        p_2 IN NUMBER)
    RETURN NUMBER AS
  BEGIN
    RETURN p_1 + p_2;
  END add_numbers;

BEGIN
  l_start := DBMS_UTILITY.get_time;

  FOR i IN 1 .. l_loops LOOP
    --PRAGMA INLINE (add_numbers, 'YES');
    l_return := add_numbers(1, i);
  END LOOP;

  DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');
END;
/
Elapsed Time: 245 hsecs

PL/SQL procedure successfully completed.

SQL>

This gives an elapsed time of 245 hsec, which implies that subprogram inlining is still taking place.

Finally, we make surePLSQL_OPTIMIZE_LEVEL=3and run the code with anINLINEpragma set to "NO". We would expect there to be no inlining of theADD_NUMBERScall now.

ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=3;

SET SERVEROUTPUT ON
DECLARE
  l_loops  NUMBER := 10000000;
  l_start  NUMBER;
  l_return NUMBER;

  FUNCTION add_numbers (p_1 IN NUMBER,
                        p_2 IN NUMBER)
    RETURN NUMBER AS
  BEGIN
    RETURN p_1 + p_2;
  END add_numbers;

BEGIN
  l_start := DBMS_UTILITY.get_time;

  FOR i IN 1 .. l_loops LOOP
    PRAGMA INLINE (add_numbers, 'NO');
    l_return := add_numbers(1, i);
  END LOOP;

  DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');
END;
/
Elapsed Time: 500 hsecs

PL/SQL procedure successfully completed.

SQL>

This gives an elapsed time of 500 hsecs, which implies that inlining did not take place as we expected.

TheINLINEpragma only affects the following types of statements.

  • Assignment
  • Call
  • Conditional
  • CASE
  • CONTINUE-WHEN
  • EXECUTE IMMEDIATE
  • EXIT-WHEN
  • LOOP
  • RETURN

In each case, it affects every call to specified subprogram from the statement.

The optimizer can choose to ignore anINLINEpragma setting of "YES" if it believes inlining is undesirable, but a setting of "NO" will always prevent inlining.

The compiler inlines subprograms early in the optimization process, which may preventing later, more powerful optimizations taking place. As a result, performance almost always improves with inlining, but in some cases it may not be effective.



show paramaters:

SHOW PARAMATERS SET PLSQL_OPTIMIZE_LEVEL;
分享到:
评论

相关推荐

    11g_plsql_user_guide_and_reference.pdf

    Automatic subprogram inlining is a new optimization technique that can significantly improve the performance of PL/SQL code. When enabled, the Oracle database automatically inlines small subprograms, ...

    oracle 9i pl/sql程序设计笔记

    3. **子程序(Subprogram)**:包括过程(Procedure)、函数(Function)和包(Package)。子程序能够存储在数据库中,这意味着一旦创建,它们就可以被多次调用而无需重新定义,提高了代码的重用性和效率。 4. **...

    oracle 存储过程学习经典

    {IS | AS} {pl_sql_subprogram_body | language{javaname 'String' | c [name, name] library lib_name}}; ``` #### 五、存储过程中的参数类型 - **IN**: 输入参数,用于向存储过程传递数据。 - **OUT**: 输出...

    Oracle存储过程学习经典[语法+实例+调用].

    {IS | AS} {pl_sql_subprogram_body | LANGUAGE {JAVA | C}} ``` - **示例**: ```sql CREATE OR REPLACE PROCEDURE sam.credit ( acc_no IN NUMBER, amount IN NUMBER ) AS BEGIN UPDATE accounts SET ...

    oracle存储过程学习经典[语法+实例+调用].doc

    ### Oracle存储过程学习经典知识点详解 #### Oracle存储过程概述与基础知识 存储过程是数据库中预编译的一系列SQL和PL/SQL语句的集合,它提供了执行复杂操作的能力,如事务处理、数据处理和错误处理。Oracle存储...

    oracle存储过程学习经典[语法+实例+调用]

    {IS | AS} {pl_sql_subprogram_body | language {java_name 'String' | c [name, name] library lib_name}} ``` - **参数类型**: - `IN`:表示此参数由调用者提供,存储过程内只能读取该参数。 - `OUT`:表示...

    oracle 数据库 plsql 存储过程

    PL/SQL_subprogram_body | LANGUAGE {java name 'String' | c [name, name] library lib_name} }; ``` **示例**: ```sql CREATE OR REPLACE PROCEDURE sam.credit ( acc_no IN NUMBER, amount IN NUMBER ) AS...

    oracle 包详细讲解

    ### Oracle包详解 #### 一、包的基本概念 在Oracle数据库中,包(Package)是一种重要的编程机制,用于封装逻辑上相关的多个PL/SQL组件,如类型、对象、子程序等,以提供一种模块化的组织方式。包的概念类似于C++...

    Oracle经典语法的调用

    ### Oracle经典语法的调用——存储过程详解 #### 一、Oracle存储过程基础知识 存储过程是一种预先编写并存储于数据库中的SQL与编程语言混合的脚本。这些脚本可以包括复杂的逻辑,例如条件判断、循环等,使得它们...

    oracle学习笔记

    Oracle数据库是世界上最流行的数据库管理系统之一,尤其在企业级应用中占据重要地位。本文将深入探讨Oracle中的几个关键概念,如Package、Function、Procedure以及它们在Oracle数据库中的使用。 首先,让我们了解...

    ORACLE常用语法

    **子程序(Subprogram):** - 包括存储过程和函数,它们都是可重用的PL/SQL代码块。 **创建存储过程示例:** ```sql CREATE OR REPLACE PROCEDURE p_print_info (p_name IN VARCHAR2) AS BEGIN DBMS_OUTPUT.PUT_...

    Oracle PLSQL9i 程序设计中的例子脚本

    Oracle PLSQL9i是Oracle数据库在9i版本中提供的编程语言,它结合了SQL的查询功能和Procedural Language(过程语言)的控制结构,使得开发者可以创建复杂的数据库应用程序。这个压缩包“Oracle PLSQL9i 程序设计中的...

    MCS-51-subprogram.rar_51?????ASM_51库函数_单片机数据库_浮点函数_滤波

    该压缩包“MCS-51-subprogram.rar”包含了丰富的资源,特别是针对51系列单片机的ASM汇编语言编程和实用程序库,对于学习和开发51单片机应用的人来说,是一份宝贵的资料。 首先,我们来关注一下标题中的关键词:...

    Model-Based Engineering - AADL

    - **子程序** (Subprogram): 可调用的代码单元。 - **示例:** ```aadl package MyPackage is port MyInput : Data; portgroup MyPortGroup is ports MyInput, AnotherInput; parameter Speed : Integer := ...

    subprogram.rar_matlab例程_matlab_

    MATLAB中的`subprogram`文件可能包含了一系列的函数或脚本,用于实现分数阶傅立叶变换的算法。这些程序可能包括以下几个关键部分: 1. **预处理**:首先,需要读取LFM信号的数据,这通常通过MATLAB的`audioread`或`...

    Oacle DBA的PL SQL学习笔记

    存储函数和存储过程是PL/SQL中可以存储在数据库中的子程序(subprogram)。 - 存储函数:有返回值的子程序,类似于其他编程语言中的函数。 - 存储过程:无返回值的子程序,可以进行特定操作。 七、编写触发器 ...

Global site tag (gtag.js) - Google Analytics