|
|
|
A m Z o n e
Call by Reference parameters in PL/SQL (NOCOPY Hint)
Prior to Oracle 8i releases, the IN OUT parameters in PL/SQL routines are passed using the copy-in and copy-out semantics (call by value).
For example,
create or replace procedure cpy_chk (pi_val in varchar2, po_dat out date, pio_status in out varchar2) is begin po_dat := sysdate; pio_status := pio_status || 'amar is testing'; end;
When the above code is executed, Oracle will copy the value being passed to parameters PO_DAT and PIO_STATUS in separate buffer, for the routine. On completion of the routine Oracle will copy the value being held in parameters PO_DAT and PIO_STATUS back to the original variable. Result, multiple buffers being opened in memory and the over head of copying data back and forth. This can be huge CPU and Memory overhead if the parameter is meant for large strings or collection objects.
The IN parameter is passed by reference, that is, a pointer to the actual IN parameter is passed to the corresponding formal parameter. So, both parameters refer the same memory location and no copying overhead involved. However the OUT and IN OUT parameters are passed by value.
To get over this, package variables were being used to pass values around. Though an alternative to prevent multiple buffers and copy overhead, it resulted in higher maintenance cost.
From Oracle 8i onwards, the NOCOPY parameter hint is introduced for OUT and IN OUT parameters. Using this hint tells Oracle to make a call by reference. Use this hint when there is no need to preserve the Original value (in case the called routine raises an exception). Oracle's internal Benchmark testing showed improvements of 30% to 200% for PL/SQL tables being passed as parameters. NOCOPY is the ideal hint for OUT and IN OUT parameters when original value is not to be preserved as is the case mostly.
Example:
create or replace procedure cpy_chk (pi_val in varchar2, po_dat out nocopy date, pio_status in out nocopy varchar2) is begin po_dat := sysdate; pio_status := pio_status || 'amar is testing'; end;
Drawbacks
NOCOPY is a hint and Oracle does not guarantee a parameter will be passed by reference when explicitly mentioned. Here are some places where this is not possible:
This article was hosted on 21 sep 02.
Press the Back button of you Browser to go to previous page.
Home