当前位置:首页 > 开发 > 数据库 > 正文

Oracle 11g PL/SQL and Java Automatic Native Compilation(原创)

发表于: 2013-10-29   作者:czmmiao   来源:转载   浏览次数:
摘要: PL/SQL and Java Automatic Native CompilationUp until the Oracle Database 11g release, the database always transformed PL/SQL code to C code first before executing it. This meant you needed a third-par

PL/SQL and Java Automatic Native Compilation
Up until the Oracle Database 11g release, the database always transformed PL/SQL code to C code first before executing it. This meant you needed a third-party C compiler to execute the C code. In Oracle Database 11g, the database skips the C compiler by directly translating PL/SQL source code to DLL for the server. The feature is called PL/SQL native compilation. Oracle also performs the linking and delinking itself and bypasses the file system directories for doing that. Oracle claims that its test show performance improvements as large as two-fold, with the native compilation of PL/SQL.

The really good news for DBAs is that it is extremely easy to take advantage of the native PL/SQL compilation capability. You simply set the appropriate value for the new initialization parameter plsql_code_type to turn automatic native PL/SQL compilation on, as the next section explains.
Using Real Native Compilation
Use the initialization parameter plsql_code_type to specify the compilation mode for PL/SQL library units. The parameter can take two values, interpreted and native.

  • INTERPRETED: PL/SQL library units will be compiled to PL/SQL bytecode format. Such modules are executed by the PL/SQL interpreter engine.
  • NATIVE: PL/SQL library units (with the possible exception of top-level anonymous PL/SQL blocks) will be compiled to native (machine) code. Such modules will be executed natively without incurring any interpreter overhead.

Setting the value of this parameter to compiled produces the default behavior where the database compiles PL/SQL code first to a PL/SQL bytecode format using the C compiler. The PL/SQL interpreter engine then executes the bytecode. By setting the parameter to the value native, you let the database compile the PL/SQL code to machine code and execute it natively without the need for an interpreter. The database stores the DLLs it generates from the PL/SQL source code in the database catalog, from where the Oracle executable loads the code directly without first using a file system to stage them.

When the value of this parameter is changed, it has no effect on PL/SQL library units that have already been compiled. The value of this parameter is stored persistently with each library unit.If a PL/SQL library unit is compiled native, all subsequent automatic recompilations of that library unit will use native compilation.
By default, the plsql_code_type parameter is set to the value interpreted, and you can turn native PL/SQL compilation on in the database by setting the plsql_code_type parameter to native, as shown here: plsql_code_type=native
You can check that the database is using the correct mode of PL/SQL compilation by issuing the following statement:
SQL> select name, value from v$parameter where
     name like '%plsql%;
NAME                          VALUE
---------------------     ------------
plsql_code_type            INTERPRETED
plsql_optimize_level       2
...
9 rows selected.
You can also use the alter system or alter session statements to change the value for the plsql_code_type parameter dynamically, without restarting the database. Any PL/SQL units that are already compiled won’t be affected by a change in the compilation mode. Also, even after you change the compilation mode, say from compiled to native, PL/SQL units that the database has already compiled will be recompiled in the original compilation mode.
Setting Up a PL/SQL Program Unit for Native Compilation
In order to set up a single PL/SQL program unit for native compilation, you must change the value of the plsql_code_type parameter to native from its default value of interpreted, by making the change and restarting the database or by using the alter system/session statement to change the value of the parameter. You can also issue the alter <PLSQL unit type> statement to enable native compilation for a single PL/SQL program unit. Let’s use a simple example that illustrates how to do this:
1.  First, create a simple procedure, called TEST_NATIVE.
SQL> create or replace procedure test_native as
  2   begin
  3   dbms_output.put_line('Test Procedure.');
  4*  end test_native;
SQL> /
Procedure created.
2. Check the current PL/SQL compilation mode by issuing the following statement:
SQL> select plsql_code_type
  2  from all_plsql_object_settings
  3  where name='TEST_NATIVE';
PLSQL_CODE_TYPE
----------------
INTERPRETED
The query shows that, currently, the procedure TEST_NATIVE is set for interpreted compiling and not native compilation.
3. Issue the following alter procedure statement to change the compilation mode to native for just the TEST_NATIVE procedure.
SQL> alter procedure test_native compile plsql_code_type=native;
Procedure altered.
4. Confirm that the procedure TEST_NATIVE will now use native compilation, by issuing the following query on the DBA_PLSQL_OBJECT_SETTINGS view:
SQL> select plsql_code_type
  2  from all_plsql_object_settings
  3* where name='TEST_NATIVE';
PLSQL_CODE_TYPE
---------------
NATIVE
The value of native for the PLSQL_CODE_TYPE column means that from here on, the database will use native compilation for the TEST_NATIVE procedure. Recompiling a Database for PL/SQL Native Compilation You can use the dbmsupgnv.sql script provided with Oracle Database 11g to recompile all the PL/SQL modules in the database to compile natively. Follow these steps to recompile all the PL/SQL modules:
1.Shut down the database using the shutdown normal or shutdown immediate commands.
SQL> shutdown immediate;
In the initialization parameter file, set the plsql_code_type parameter to native so it will allow native compilation. plsql_code_type=native
2. You must also check to ensure that the value of the plsql_optimize_level parameter is at least 2. The default value for this parameter is 2.plsql_optimize_level=3 Because the value of the plsql_optimize_level parameter is more than 2, you don’t have to change the setting of this parameter.
3. Start the database with the startup upgrade command, which you specify when upgrading to a new release of the Oracle database.
SQL> connect sys/sammyy1 as sysdba
Connected to an idle instance.
SQL> startup upgrade
ORACLE instance started.
...
Database opened.
4.After the database is opened in the upgrade mode, execute the script dbmsupgnv.sql, located in the $ORACLE_HOME/rdbms/admin directory.
SQL> @$ORACLE_HOME/rdbms/admin/dbmsupgnv.sql
OC>#########################################################
DOC>########################################################
DOC>   dbmsupgnv.sql completed successfully.
DOC> All PL/SQL procedures, functions, type bodies, triggers,
DOC> and type bodies objects in the database have been
DOC> invalidated and their settings set to native.
DOC>
DOC>   Shut down and restart the database in normal mode and
DOC>   run utlrp.sql to recompile invalid objects.
SQL>
When the dbmsupgnv.sql script completes executing, all PL/SQL procedures in the database are natively compiled.
5. Once the script finishes running, shut down the database and start it back up again. Run the utlrp.sql script located in the $ORACLE_HOME/rdbms/
admin directory to recompile the invalidated PL/SQL program units.
SQL> shutdown immediate;
SQL> startup
ORACLE instance started.
...
Database opened.
SQL> @$ORACLE_HOME/rdbms/admin/utlrp.sql
...
SQL> Rem END utlrp.sql
As a result of upgrading the database and compiling all PL/SQL units in the native mode, you don’t need to individually enable PL/SQL procedures for native compilation. You can always change the compilation mode back to the default value of interpreted by reversing the recompilation process shown here. You follow a procedure similar to the one shown here to compile all PL/SQL program units in the interpreted mode by running the script dbmsupgin.sql, also located in the $ORACLE_HOME/rdbms/admin directory.

Java Native Compilation

Oracle Database 11g uses the new initialization parameter java_jit_enabled to set the Java compilation mode. By default, the value of the java_jit_enabled initialization parameter is true, meaning Java native compilation is enabled in the database. As in the case of the PL/SQL native compilation, this feature allows the database to compile Java in the database natively without using a C compiler. Oracle claims that native compilation offers a 100 percent faster performance for both pure PL/SQL and Java code. Oracle’s Java native compilation is similar to that of the Java Development Kit and runs as an independent session in the server process that is transparent to the user. There’s only one compiler session per Oracle instance and the database stores the Java code for future recompilations. Java native compilation offers you the high performance of pure Java execution and is very easy to implement because you can enable it for the entire database, not merely when you actually execute the Java code in the database. The absence of a C compiler means you save on licensing and other costs involved in maintaining the compiler.

 

参考至: 《McGraw.Hill.OCP.Oracle.Database.11g.New.Features.for.Administrators.Exam.Guide.Apr.2008》           
    http://docs.oracle.com/cd/E11882_01/server.112/e40402/initparams196.htm#REFRN10253本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:czmcj@163.com

Oracle 11g PL/SQL and Java Automatic Native Compilation(原创)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Automatic Diagnostic Repository The automatic diagnostic repository tracks all problems and i
PL/SQL概述 PL/SQL是过程化的SQL语言,是ORACLE对SQL语言的扩展,在普通SQL语句的基础上增加了编程
1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Dev
原文地址:http://www.cnblogs.com/ymj126/p/3712727.html 1.登录PL/SQL Developer 这里省略Oracle数
转摘:http://www.cnblogs.com/ymj126/p/3712727.html 用于学习,笔记,以备后用。 1.登录PL/SQL De
本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ 第四章 控制结构(也较
1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Dev
官网的链接: Overview of PL/SQL http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/overvie
《Oracle PL/SQL实战》 基本信息 作者: (英)比林顿(Billington,A·)等著,卢涛 [作译者介绍] 出版社
《Oracle PL/SQL攻略》 基本信息 原书名:Oracle and PL/SQL recipes:a problem-solution approach
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号