<span style="font-size:14px;">View Code數據庫
--在select into語句中使用bulk collectapp
DECLARE
TYPE SalList IS TABLE OF emp.sal%TYPE;
sals SalList;
BEGIN
-- Limit the number of rows to 100.
SELECT sal BULK COLLECT INTO sals FROM emp
WHERE ROWNUM <= 100;
-- Retrieve 10% (approximately) of the rows in the table.
SELECT sal BULK COLLECT INTO sals FROM emp SAMPLE 10;測試
END;
/fetch
--在fetch into中使用bulk collectspa
DECLARE
TYPE DeptRecTab IS TABLE OF dept%ROWTYPE;
dept_recs DeptRecTab;
CURSOR c1 IS
SELECT deptno, dname, loc FROM dept WHERE deptno > 10;
BEGIN
OPEN c1;
FETCH c1 BULK COLLECT INTO dept_recs;
END;
/內存
--在returning into中使用bulk collectit
CREATE TABLE emp2 AS SELECT * FROM employees;
DECLARE
TYPE NumList IS TABLE OF employees.employee_id%TYPE;
enums NumList;
TYPE NameList IS TABLE OF employees.last_name%TYPE;
names NameList;
BEGIN
DELETE FROM emp2 WHERE department_id = 30
RETURNING employee_id, last_name BULK COLLECT INTO enums, names;
dbms_output.put_line('Deleted ' || SQL%ROWCOUNT || ' rows:');
FOR i IN enums.FIRST .. enums.LAST
LOOP
dbms_output.put_line('Employee #' || enums(i) || ': ' || names(i));
END LOOP;
END;
/
DROP TABLE emp2;</span>io
經過上面的測試和分析,咱們能夠看到Bulk
Collect批查詢在某種程度上能夠提升查詢效率,它首先將所需數據讀入內存,而後再統計分析,這樣就能夠提升查詢效率。可是,若是Oracle數據庫的內存較小,Shared
Pool Size不足以保存Bulk Collect批查詢結果,那麼該方法須要將Bulk Collect的集合結果保存在磁盤上,在這種狀況下,Bulk
Collect方法的效率反而不如其餘兩種方法,有興趣的讀者能夠進一步測試。table
另外,除了Bulk Collect批查詢外,咱們還能夠使用FORALL語句來實現批插入、刪除和更新,這在大批量數據操做時能夠顯著提升執行效率。ast