Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, arish.

Asked: August 22, 2016 - 7:27 am UTC

Last updated: August 22, 2016 - 4:14 pm UTC

Version: 11.2.03

Viewed 1000+ times

You Asked

Hi Tom,
what are the pros and cons if I convert my plsql code type to Native? I have package bodies, procedures & functions where most of my code runs.
I do have lot of data extractions taken from DB & number of PLSQL programs that run for hours. Will my SQLs be impacted when I change to Native?

and Chris said...

The advantage of moving to native compilation is your code may run faster.

But this is a PL/SQL only benefit. Your SQL statements will still take the same amount of time.

So will it benefit your code?

If it's mostly SQL, probably not. But if there's big chunks of PL/SQL only computations, you could get a reasonable boost.

For example, the procedure below does a bunch of PL/SQL calculations:

create or replace procedure p_plsql is
  val number;
begin
  for i in 1 .. 10000000 loop
    val := 100 * (i / 2) + mod (i, 5) - i;
  end loop;
end p_plsql;
/

With intepreted compilation it averages just over 5s/execution:
alter procedure p_plsql compile plsql_code_type = INTERPRETED;

declare
  start_time pls_integer;
begin
  dbms_output.put_line('******');
  start_time := dbms_utility.get_time();
  p_plsql;
  dbms_output.put_line('PL/SQL: Run 1: ' || (dbms_utility.get_time() - start_time));
  start_time := dbms_utility.get_time();
  p_plsql;
  dbms_output.put_line('PL/SQL: Run 2: ' || (dbms_utility.get_time() - start_time));
  start_time := dbms_utility.get_time();
  p_plsql;
  dbms_output.put_line('PL/SQL: Run 3: ' || (dbms_utility.get_time() - start_time));
  dbms_output.put_line('******');
 
end;
/

******
PL/SQL: Run 1: 551
PL/SQL: Run 2: 547
PL/SQL: Run 3: 531
******

Convert it to native compilation and it's generally under 5s:
alter procedure p_plsql compile plsql_code_type = NATIVE;

declare
  start_time pls_integer;
begin
  dbms_output.put_line('******');
  start_time := dbms_utility.get_time();
  p_plsql;
  dbms_output.put_line('PL/SQL: Run 1: ' || (dbms_utility.get_time() - start_time));
  start_time := dbms_utility.get_time();
  p_plsql;
  dbms_output.put_line('PL/SQL: Run 2: ' || (dbms_utility.get_time() - start_time));
  start_time := dbms_utility.get_time();
  p_plsql;
  dbms_output.put_line('PL/SQL: Run 3: ' || (dbms_utility.get_time() - start_time));
  dbms_output.put_line('******');
 
end;
/
******
PL/SQL: Run 1: 473
PL/SQL: Run 2: 463
PL/SQL: Run 3: 482
******


As always, you'll need to test in your environment to figure out your gains.

So what about the downsides?

Oracle stores natively compiled PL/SQL in the system tablespace. It then pulls it into shared memory upon execution. So if you have large* numbers of natively compiled objects active at the same time, this may affect performance.

* The docs define large as > 15,000

http://docs.oracle.com/database/121/LNPLS/tuning.htm#LNPLS910

It also takes slightly longer to compile native PL/SQL. And you can't run debugging tools on native code.

So you may want to leave your dev environments as interpreted. Save native compilation for prod and QA where you shouldn't be compiling or debugging often!


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library