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.
- 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.
参考至: 《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本文原创,转载请注明出处、作者