提升性能的BULK COLLECT語句和FORALL語句sql
pl/sql向sql引擎發送sql語句執行,而後SQL向pl/sql 返回結果數據。能夠使用pl/sql的大批量sql特性來下降sql和pl /sql之間的通訊開銷。FORALL語句將INSERT或UPDATE或DELETE批次處理,BULK COLLECT子句帶回批結果。若是,DML語句影響四個或更多個數據庫行,使用bulk sql(批量SQL)能夠顯著提升性能。數據庫
1.FORALL語句的使用oop
可是,FORALL語句只能重複單獨一條 DML語句。 例子:FORALL語句一次性把三條DELETE 語句發給SQL引擎:性能
1 CREATE TABLE employees_temp AS SELECT * FROM employees; 2 DECLARE 3 TYPE NumList IS VARRAY(20) OF NUMBER; 4 depts NumList := NumList(10, 30, 70); -- department numbers 5 BEGIN 6 FORALL i IN depts.FIRST..depts.LAST 7 DELETE FROM employees_temp WHERE department_id = depts(i); 8 COMMIT; 9 END; 10 /
咱們能夠經過下面的例子來比較FOR語句和FORALL語句的執行時間:this
1 CREATE TABLE parts1 (pnum INTEGER, pname VARCHAR2(15)); 2 CREATE TABLE parts2 (pnum INTEGER, pname VARCHAR2(15)); 3 DECLARE 4 TYPE NumTab IS TABLE OF parts1.pnum%TYPE INDEX BY PLS_INTEGER; 5 TYPE NameTab IS TABLE OF parts1.pname%TYPE INDEX BY PLS_INTEGER; 6 pnums NumTab; 7 pnames NameTab; 8 iterations CONSTANT PLS_INTEGER := 500; 9 t1 INTEGER; 10 t2 INTEGER; 11 t3 INTEGER; 12 BEGIN 13 FOR j IN 1..iterations LOOP -- load index-by tables 14 pnums(j) := j; 15 pnames(j) := 'Part No. ' || TO_CHAR(j); 16 END LOOP; 17 t1 := DBMS_UTILITY.get_time; 18 FOR i IN 1..iterations LOOP -- use FOR loop 19 INSERT INTO parts1 VALUES (pnums(i), pnames(i)); 20 END LOOP; 21 t2 := DBMS_UTILITY.get_time; 22 FORALL i IN 1..iterations -- use FORALL statement 23 INSERT INTO parts2 VALUES (pnums(i), pnames(i)); 24 t3 := DBMS_UTILITY.get_time; 25 DBMS_OUTPUT.PUT_LINE('Execution Time (secs)'); 26 DBMS_OUTPUT.PUT_LINE('---------------------'); 27 DBMS_OUTPUT.PUT_LINE('FOR loop: ' || TO_CHAR((t2 - t1)/100)); 28 DBMS_OUTPUT.PUT_LINE('FORALL: ' || TO_CHAR((t3 - t2)/100)); 29 COMMIT; 30 END; 31 / 32 Execution Time (secs) 33 --------------------- 34 FOR loop: .02 35 FORALL: 0
由此,咱們能夠看出使用FORALL語句更快。spa
2.使用部分索引的FORALL語句(Part of a Collection)code
此外,咱們能夠只使用條件集合的部分用於FORALL條件。如:blog
1 CREATE TABLE employees_temp AS SELECT * FROM employees; 2 DECLARE 3 TYPE NumList IS VARRAY(10) OF NUMBER; 4 depts NumList := NumList(5,10,20,30,50,55,57,60,70,75); 5 BEGIN 6 FORALL j IN 4..7 -- use only part of varray 7 DELETE FROM employees_temp WHERE department_id = depts(j); 8 COMMIT; 9 END; 10 /
3.使用非連續的稀疏索引的FORALL語句(Non-Consecutive Index Values)
一樣,咱們也能夠使用非連續的index value(索引值)做爲條件,這須要使用INDICES OF子句來處理。索引
1 -- Create empty tables to hold order details 2 CREATE TABLE valid_orders (cust_name VARCHAR2(32), amount NUMBER(10,2)); 3 CREATE TABLE big_orders AS SELECT * FROM valid_orders WHERE 1 = 0; 4 CREATE TABLE rejected_orders AS SELECT * FROM valid_orders WHERE 1 = 0; 5 DECLARE 6 -- Make collections to hold a set of customer names and order amounts. 7 SUBTYPE cust_name IS valid_orders.cust_name%TYPE; 8 TYPE cust_typ IS TABLE OF cust_name; 9 cust_tab cust_typ; 10 SUBTYPE order_amount IS valid_orders.amount%TYPE; 11 TYPE amount_typ IS TABLE OF NUMBER; 12 amount_tab amount_typ; 13 -- Make other collections to point into the CUST_TAB collection. 14 TYPE index_pointer_t IS TABLE OF PLS_INTEGER; 15 big_order_tab index_pointer_t := index_pointer_t(); 16 rejected_order_tab index_pointer_t := index_pointer_t(); 17 PROCEDURE setup_data IS BEGIN 18 -- Set up sample order data, including some invalid orders and some 'big' orders. 19 cust_tab := cust_typ('Company1','Company2','Company3','Company4','Company5'); 20 amount_tab := amount_typ(5000.01, 0, 150.25, 4000.00, NULL); 21 END; 22 BEGIN 23 setup_data(); 24 DBMS_OUTPUT.PUT_LINE('--- Original order data ---'); 25 FOR i IN 1..cust_tab.LAST LOOP 26 DBMS_OUTPUT.PUT_LINE('Customer #' || i || ', ' || cust_tab(i) || ': $' || 27 amount_tab(i)); 28 END LOOP; 29 -- Delete invalid orders (where amount is null or 0). 30 FOR i IN 1..cust_tab.LAST LOOP 31 IF amount_tab(i) is null or amount_tab(i) = 0 THEN 32 cust_tab.delete(i); 33 amount_tab.delete(i); 34 END IF; 35 END LOOP; 36 DBMS_OUTPUT.PUT_LINE('--- Data with invalid orders deleted ---'); 37 FOR i IN 1..cust_tab.LAST LOOP 38 IF cust_tab.EXISTS(i) THEN 39 DBMS_OUTPUT.PUT_LINE('Customer #' || i || ', ' || cust_tab(i) || ': $' || 40 amount_tab(i)); 41 END IF; 42 END LOOP; 43 -- Because the subscripts of the collections are not consecutive, use 44 -- FORALL...INDICES OF to iterate through the actual subscripts, 45 -- rather than 1..COUNT 46 FORALL i IN INDICES OF cust_tab 47 INSERT INTO valid_orders(cust_name, amount) 48 VALUES(cust_tab(i), amount_tab(i)); 49 -- Now process the order data differently 50 -- Extract 2 subsets and store each subset in a different table 51 setup_data(); -- Initialize the CUST_TAB and AMOUNT_TAB collections again. 52 FOR i IN cust_tab.FIRST .. cust_tab.LAST LOOP 53 IF amount_tab(i) IS NULL OR amount_tab(i) = 0 THEN 54 rejected_order_tab.EXTEND; -- Add a new element to this collection 55 -- Record the subscript from the original collection 56 rejected_order_tab(rejected_order_tab.LAST) := i; 57 END IF; 58 IF amount_tab(i) > 2000 THEN 59 big_order_tab.EXTEND; -- Add a new element to this collection 60 -- Record the subscript from the original collection 61 big_order_tab(big_order_tab.LAST) := i; 62 END IF; 63 END LOOP; 64 -- Now it's easy to run one DML statement on one subset of elements, 65 -- and another DML statement on a different subset. 66 FORALL i IN VALUES OF rejected_order_tab 67 INSERT INTO rejected_orders VALUES (cust_tab(i), amount_tab(i)); 68 FORALL i IN VALUES OF big_order_tab 69 INSERT INTO big_orders VALUES (cust_tab(i), amount_tab(i)); 70 COMMIT; 71 END; 72 / 73 -- Verify that the correct order details were stored 74 SELECT cust_name "Customer", amount "Valid order amount" FROM valid_orders; 75 SELECT cust_name "Customer", amount "Big order amount" FROM big_orders; 76 SELECT cust_name "Customer", amount "Rejected order amount" FROM rejected_orders;