zoukankan      html  css  js  c++  java
  • 通过bulk collect 减少loop处理的开销


    通过bulk collect减少loop处理的开销

    采用bulk collect可以将查询结果一次性地加载到collections中。而不是通过cursor一条一条地处理。可以在select into,fetch into,returning into语句使用bulkcollect。注意在使用bulk collect时,所有的into变量都必须是collections. 
    举几个简单的例子:--在select into语句中使用bulk collectDECLARE
    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 into中使用bulk collectDECLARE
    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 collectCREATE 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;


    感谢——转自 http://blog.sina.com.cn/s/blog_525394060100f822.html

  • 相关阅读:
    洛谷P5281 [ZJOI2019] Minimax搜索
    势函数
    Comet OJ [Contest #5] 迫真大游戏
    洛谷P3307 [SDOI2013] 项链
    洛谷P5985 [PA2019] Muzyka pop
    CF1205E Expected Value Again
    review
    CF891E Lust
    线性代数
    洛谷P4607 [SDOI2018] 反回文串
  • 原文地址:https://www.cnblogs.com/andy-wcl/p/3218134.html
Copyright © 2011-2022 走看看