Oracle Bulk Bind ForAll and bulk COLLECT

Source: Internet
Author: User
Tags switches

How to use ForAll and bulk collect:
1. Using ForAll is more efficient than for, because the former switches only once to the context, while the latter switches between multiple contexts in the same number of cycles.

2. Using Bluk collect to fetch a data collection at once is more efficient than using a cursor bar, especially if the network is not very good. But Bluk collect requires a lot of memory.

Example:

SQL code
    1. Create table Test_forall (user_id number (ten), user_name VARCHAR2 (20));


SELECT INTO using bulk collect

SQL code
  1. DECLARE
  2. TYPE table_forall is table of Test_forall%rowtype;
  3. V_table Table_forall;
  4. BEGIN
  5. SELECT Mub.user_id,mub.user_name
  6. BULK COLLECT into v_table
  7. From mag_user_basic Mub
  8. WHERE mub.user_id between 10000 and 10100;
  9. FORALL idx in 1..v_table. COUNT
  10. INSERT into test_forall VALUES v_table (IDX);
  11. --values (v_table (IDX). user_id,v_table (IDX). user_name); Error
  12. -in PL/SQL, BULK In-bind and Record,%rowtype cannot be used in one piece,
  13. -in other words, BULK in-bind can only be used with arrays of simple types
  14. COMMIT;
  15. EXCEPTION
  16. When OTHERS Then
  17. ROLLBACK;
  18. END;


fetch into uses bulk collect

SQL code
  1. DECLARE
  2. TYPE table_forall is table of Test_forall%rowtype;
  3. V_table Table_forall;
  4. CURSOR C1 is
  5. SELECT Mub.user_id,mub.user_name
  6. From mag_user_basic Mub
  7. WHERE mub.user_id between 10000 and 10100;
  8. BEGIN
  9. OPEN C1;
  10. --use bulk collect in fetch into
  11. FETCH C1 BULK COLLECT into v_table;
  12. FORALL idx in 1..v_table. COUNT
  13. INSERT into test_forall VALUES v_table (IDX);
  14. COMMIT;
  15. EXCEPTION
  16. When OTHERS Then
  17. ROLLBACK;
  18. END;


use bulk collect in returning into

SQL code
  1. CREATE TABLE test_forall2 as SELECT * from Test_forall;
  2. ----using bulk collect in returning into
  3. DECLARE
  4. TYPE idlist is TABLE of Test_forall.  User_id%type;
  5. Enums idlist;
  6. TYPE namelist is TABLE of Test_forall.user_name%type;
  7. Names NameList;
  8. BEGIN
  9. DELETE from test_forall2 WHERE user_id = 10100
  10. Returning user_id, user_name BULK COLLECT into enums, names;
  11. Dbms_output.put_line (' Deleted ' | | Sql%rowcount | |  ' rows: ');
  12. For i in enums.  First: Enums. Last
  13. LOOP
  14. Dbms_output.put_line (' User # ' | | | enums (i) | |  ': ' | | | names (i));
  15. END LOOP;
  16. COMMIT;
  17. EXCEPTION
  18. When OTHERS Then
  19. ROLLBACK;
  20. END;


in a batch update, change the for to ForAll

SQL code
  1. DECLARE
  2. TYPE Numlist is Varray (a) of number ;
  3. Depts numlist: = Numlist (10, 30, 70, ...);
  4. --Department numbers
  5. BEGIN
  6. ...
  7. For i in depts.first. Depts. Last
  8. LOOP
  9. ...
  10. --update statement is sent to the SQL engine
  11. --With all iteration of the for loop!
  12. UPDATE emp SET sal = sal * 1.10 WHERE deptno = depts (i);
  13. END LOOP:
  14. END;

SQL code
    1. --update statement is sent to the  sql engine just once, with the entire nested table  
    2. forall i in depts.first. Depts.last   
    3.    update emp set sal = sal * 1.10  where deptno = depts (i);    


To maximize performance, rewrite your programs as follows:
A. If an INSERT, UPDATE, or DELETE statement executes inside a loop and References collection elements, move it to a for All statement.
B. If a SELECT into, FETCH into, or returning into clause references a
Collection, incorporate the BULK COLLECT clause.
C. If possible, use host arrays to pass collections back and forth between your Programs and the database server.
D. If the failure of a DML operation on a particular row are not a serious problem,include the keywords SAVE EXCEPTIONS in The FORALL statement and report Or clean up the errors in a subsequent loop using the%bulk_exceptions Attribute.

Oracle Bulk Bind ForAll and bulk COLLECT

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.