views:

280

answers:

5

What is the difference of performance between standalone procedure and packaged procedure? Which will be good performance wise and why? Is there any difference in execution of both?

+2  A: 

There should be no difference between the two.

A major use of packages is to group a set of similar/associeted functions+procedures

cagcowboy
There should be, otherwise why standalone procedure are used.
P Sharma
Oracle advises to use packages but it can't drop the support or stanalone procs and functions because it wants to be backward compatible.
tuinstoel
But how will it be performing better?
P Sharma
+3  A: 

There isn't a performance difference except that packages can have state and standalone procedures and functions not.

The use of package is more about ordening and grouping of code. You could see them as an alternative of namespaces.

tuinstoel
Can you please give any business case?
P Sharma
Just google. I don't know what business cases make sense to you.
tuinstoel
What do you mean by state or packages?
P Sharma
Here an example of a very simple package that has state (a global variable). http://www.java2s.com/Tutorial/Oracle/0540__Function-Procedure-Packages/GlobalsStoredinaPackage.htm Variable gv_countryUSA_cd lives outside a procedure or function, one or more procedures and functions within that package can share this variable.
tuinstoel
The same can be done by using standalone procedure/function given below. <br>create or replace package pkg_global isg_var_msg VARCHAR2(100);end;<br>Create or replace function func_test_msgreturn VARCHAR2IS <br>BEGINreturn pkg_global.g_var_msg;END;/
P Sharma
Yes, when you declare the global variable in the package specification, this global can be read and written by others packages, functions and procedures. The package specification is public, the package body is private. Packages can provide encapsulation, it's up to the developer if (s)he wants to use encapsulation.
tuinstoel
I think it is better to declare the global variable in the package body and to write two methods in the package specification that function as getter and setter of this global variable.
tuinstoel
Question is about performance not about encapsulation.
P Sharma
+7  A: 

Tom says:

Always use a package.
Never use a standalone procedure except for demos, tests and standalone utilities (that call nothing and are called by nothing)

There you can also find a very good discussion about their performance. Just search for "performance" on that page.
If still seriously in doubt, you can always test yourself which one is faster. You'll certainly learn something new by doing so.

My take on your question: while it's true that calling package procedures/functions seems to be slower in certain situations than calling standalone procedures/functions, the advantages offered by the additional features available when using packages far outweigh the performance loss. So, just like Tom puts it, use packages.


The link: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7452431376537


Test code(20 million calls, runstats_pkg is a package I wrote based on the runstats package by Tom Kyte):

CREATE OR REPLACE PACKAGE testperf AS
   FUNCTION pow(i INT) RETURN INT;
END;
/

CREATE OR REPLACE PACKAGE BODY testperf AS
   FUNCTION pow(i int) RETURN INT AS
   BEGIN
      RETURN i * i;
   END;
END;
/

CREATE OR REPLACE FUNCTION powperf(i INT) RETURN INT AS
BEGIN
   RETURN i * i;
END;
/

DECLARE
   I INT;
   S INT DEFAULT 0;
BEGIN
   runstats_pkg.start1;
   FOR I IN 1 .. 20000000 LOOP
      s := s + (powperf(i) / i);
   END LOOP;
   runstats_pkg.stop1;

   dbms_output.put_line(s);
   s := 0;

   runstats_pkg.start2;
   FOR I IN 1 .. 20000000 LOOP
      s := s + (testperf.pow(i) / i);
   END LOOP;
   runstats_pkg.stop2;

   dbms_output.put_line(s);

   runstats_pkg.show;
END;

Results(Oracle XE):

Run1 latches total versus runs -- difference and pct
        Run1        Run2        Diff       Pct
       2,491       2,439         -52    102.13%

Run1 ran in 2304 hsecs
Run2 ran in 2364 hsecs
run 1 ran in 97.46% of the time

Results(Oracle 11g R1, different machine):

Run1 latches total versus runs -- difference and pct
        Run1        Run2        Diff       Pct
       2,990       3,056          66     97.84%

Run1 ran in 2071 hsecs
Run2 ran in 2069 hsecs
run 1 ran in 100.1% of the time

So, there you go. Really not much of a difference. Want data for something more complex that also involves SQL DML? You gotta test it yourself.

Marius Burz
Can you please send me url where i can read this article written by Tom Kyte?
P Sharma
there is a link to it, click on **"says"**
Marius Burz
there is no practical reason described about performance.
P Sharma
@P Sharma, maybe you should do some benchmarking yourself (How hard can that be?). Call a million times your standalone procedure with some parameters and a million times the same procedure within a package with the same parameters. Use packages because Oracle advices the use of packages unless you find that your standalone procedures is really significantly faster than the same procedure within a package.
tuinstoel
I got very curious about this, so I did a test myself. The results speak for themselves: no difference results from calling a standalone function vs a package function.
Marius Burz
@Marius Burz, good job!, the results don't suprise me.
tuinstoel
+2  A: 

The primary reason to use packages is they break the dependency chain. For instance if you have two stand-alone procedures, procedure A which calls procedure B and you recompile procedure B you will also need to recompile procedure A. This gets quite complicated as you increase the number of procedures and functions.

If you move these to two to different packages you will not need to recompile them as long as the specification does not change.

David
But if package/procedure is in valid state, then there will not be any difference in the terms of dependency.
P Sharma
A: 

The other answers here are all good (e.g. packages have state, they separate interface from implementation, etc).

Another difference is when procedures or packages are wrapped - it is simple to unwrap a procedure, but not a package body.

Jeffrey Kemp
Question is about performance, not about sequrity
P Sharma
Do you have a citation that unwrapping a package body is substantially harder than unwrapping a procedure? I've read Pete Finnigan's work on unwrapping PL/SQL and I don't recall seeing anything that indicates that one sort of PL/SQL block is any more difficult to unwrap than any other.
Justin Cave
Sorry, missed that the Q was re performance only.Cave: I know it is in theory possible but I haven't come across any tools for unwrapping package bodies. If there is I wwWould be very interested! https://www.blackhat.com/presentations/bh-usa-06/BH-US-06-Finnigan.pdf - "Write PL/SQL as packages; DIANA is not stored in the database"
Jeffrey Kemp