ORACLE大量繫結FORALL與BULK COLLECT

來源:互聯網
上載者:User

標籤:

FORALL與BULK COLLECT的使用方法: 
1.使用FORALL比FOR效率高,因為前者只切換一次上下文,而後者將是在迴圈次數一樣多個上下文間切換。 

2.使用BLUK COLLECT一次取出一個資料集合,比用遊標條取資料效率高,尤其是在網路不大好的情況下。但BLUK COLLECT需要大量記憶體。 

例子: 

Sql代碼  
  1. create table test_forall ( user_id number(10), user_name varchar2(20));  


select into 中使用bulk collect 

Sql代碼  
  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.            --在PL/SQL中,BULK In-BIND與RECORD,%ROWTYPE是不能在一塊使用的,  
  13.            --也就是說,BULK In-BIND只能與簡單類型的數組一塊使用  
  14.     COMMIT;  
  15.   
  16. EXCEPTION  
  17.     WHEN OTHERS THEN  
  18.         ROLLBACK;  
  19.     
  20. END;  


fetch into 中使用bulk collect 

Sql代碼  
  1. DECLARE  
  2.   TYPE table_forall IS TABLE OF test_forall%ROWTYPE;  
  3.   v_table table_forall;     
  4.     
  5.   CURSOR c1 IS  
  6.     SELECT mub.user_id,mub.user_name  
  7.          FROM mag_user_basic mub  
  8.            WHERE mub.user_id BETWEEN 10000 AND 10100;  
  9. BEGIN  
  10.    OPEN c1;  
  11.    --在fetch into中使用bulk collect  
  12.    FETCH c1 BULK COLLECT INTO v_table;  
  13.      
  14.    FORALL idx IN 1..v_table.COUNT  
  15.          INSERT INTO test_forall VALUES v_table(idx);  
  16.     COMMIT;  
  17.   
  18. EXCEPTION  
  19.     WHEN OTHERS THEN  
  20.         ROLLBACK;  
  21. END;  


在returning into中使用bulk collect 

Sql代碼  
  1. CREATE TABLE test_forall2 AS SELECT * FROM test_forall;  
  2. ----在returning into中使用bulk collect  
  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.      
  18. EXCEPTION  
  19.     WHEN OTHERS THEN  
  20.         ROLLBACK;  
  21.          
  22. END;  


批次更新中,將for改成forall 

Sql代碼  
  1. DECLARE   
  2.     TYPE NumList IS VARRAY(20) 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 each iteration of the FOR loop!  
  12.          UPDATE emp SET sal = sal * 1.10 WHERE deptno = depts(i);  
  13.       END LOOP:   
  14.     END;            

 

Sql代碼  
  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 into a FORALL 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 is 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大量繫結FORALL與BULK COLLECT

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.