Optimize PL/SQL process calls and use nocopy prompt

Source: Internet
Author: User
Document directory
  • Restrictions on nocopy
With the nocopy prompt, you can change the in/In out type parameters of the process from by value to by referencetuning PL/SQL procedure callwith the nocopy compiler hint

By default,OUTAndIN OUTParameters are passed by value. The values of anyIN OUTParameters are copied before the subprogram is executed. during subprogram execution, temporary variables hold the output parameter values. if the subprogram Exits normally, these values are copied to the actual parameters. if the subprogram exits with an unhandled exception, the original parameters are unchanged.

When the parameters represent large data structures such as collections, records, and instances of object types, this copying slows down execution and uses up memory. in particle, this overhead applies to each call to an object method: temporary copies are made of all the attributes, so that any changes made by the method are only applied if the method Exits normally.

To avoid this overhead, you can specifyNOCOPYHint, which allows the PL/SQL Compiler to passOUTAndIN OUTParameters by reference. If the subprogram Exits normally, the behavior is the same as normal. If the subprogram exits early with an exception, the valuesOUTAndIN OUTParameters (or object attributes) might still change. To use this technique, ensure that the subprogram handles all exceptions.

The following example asks the compiler to passIN OUTParameterv_staffBy reference, to avoid copying the varray on entry to and exit from the subprogram:

DECLARE
  TYPE Staff IS VARRAY(200) OF Employee;
  PROCEDURE reorganize (v_staff IN OUT NOCOPY Staff) IS ...

Example 11-17 loads 25,000 records into a local nested table, which is passed to two local procedures that do nothing. A call to the procedure that usesNOCOPYTakes much less time.

Example 11-17 using nocopy with Parameters

DECLARE   TYPE EmpTabTyp IS TABLE OF employees%ROWTYPE;   emp_tab EmpTabTyp := EmpTabTyp(NULL);  -- initialize   t1 NUMBER;   t2 NUMBER;   t3 NUMBER;   PROCEDURE get_time (t OUT NUMBER) IS     BEGIN t := DBMS_UTILITY.get_time; END;   PROCEDURE do_nothing1 (tab IN OUT EmpTabTyp) IS     BEGIN NULL; END;   PROCEDURE do_nothing2 (tab IN OUT NOCOPY EmpTabTyp) IS     BEGIN NULL; END;BEGIN   SELECT * INTO emp_tab(1) FROM employees WHERE employee_id = 100;   emp_tab.EXTEND(49999, 1);  -- copy element 1 into 2..50000   get_time(t1);   do_nothing1(emp_tab);  -- pass IN OUT parameter   get_time(t2);   do_nothing2(emp_tab);  -- pass IN OUT NOCOPY parameter   get_time(t3);   DBMS_OUTPUT.PUT_LINE('Call Duration (secs)');   DBMS_OUTPUT.PUT_LINE('--------------------');   DBMS_OUTPUT.PUT_LINE('Just IN OUT: ' || TO_CHAR((t2 - t1)/100.0));   DBMS_OUTPUT.PUT_LINE('With NOCOPY: ' || TO_CHAR((t3 - t2))/100.0);END;/

Restrictions on nocopy

The useNOCOPYIncreases the likelihood of parameter aliasing. For more information, see "Understanding subprogram parameter aliasing ".

Remember,NOCOPYIs a hint, not a directive. In the following cases, the PL/SQL Compiler ignoresNOCOPYHint and uses the by-value parameter-passing method; no error is generated:

  • The actual parameter is an element of an associative array. This restriction does not apply if the parameter is an entire associative array.

  • The actual parameter is constrained, such as by scale orNOT NULL. This restriction does not apply to size-constrained character strings. This restriction does not extend to constrained elements or attributes of composite types.

  • The actual and formal parameters are records, one or both records were declared using%ROWTYPEOr%TYPE, And constraints on corresponding fields in the records differ.

  • The actual and formal parameters are records, the actual parameter was declared (implicitly) as the index of a cursorFORLoop, and constraints on corresponding fields in the records differ.

  • Passing the actual parameter requires an implicit datatype conversion.

  • The subprogram is called through a database link or as an external procedure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.