oracle 使用游标批量分段插入数据

By Mark Rittman

Using Bulk Binds And FORALL

Yesterday, I looked at collections, an evolution of PL/SQL tables that allows us to manipulate many variables at once, as a unit. Collections, coupled with two new features introduced with Oracle 8i, BULK_COLLECT and FORALL, can dramatically increase the performance of data manipulation code within PL/SQL.

As a reminder, we were looking at a piece of code that implemented collections, BULK COLLECT and FORALL, taken from a question and answer posed online by Daniel Morgan.

CREATE OR REPLACE PROCEDURE fast_proc (p_array_size IN PLS_INTEGER DEFAULT 100) IS

TYPE ARRAY IS TABLE OF all_objects%ROWTYPE; l_data ARRAY;

CURSOR c IS SELECT * FROM all_objects;

BEGIN     OPEN c;     LOOP     FETCH c BULK COLLECT INTO l_data LIMIT p_array_size;

    FORALL i IN 1..l_data.COUNT     INSERT INTO t2 VALUES l_data(i);

    EXIT WHEN c%NOTFOUND;     END LOOP;     CLOSE c; END fast_proc; /

which was subsequently refined in a later answer to;

SQL> create or replace procedure fast_proc is 2         type TObjectTable is table of ALL_OBJECTS%ROWTYPE; 3         ObjectTable$ TObjectTable; 4         begin 5         select 6                     * BULK COLLECT INTO ObjectTable$ 7         from ALL_OBJECTS; 8 9         forall x in ObjectTable$.First..ObjectTable$.Last 10       insert into t1 values ObjectTable$(x) ; 11       end; 12 /

The key things here are the collection that is set up to hold the table data, the BULK COLLECT clause and the FORALL statement. I went into detail about the collection yesterday, so now it's time to look at the other two.

Steven Feuernstein explains the basics behind BULK_COLLECT and FORALL in a recent OTN article, and together these two features are known as 'Bulk Binding'. Bulk Binds are a PL/SQL technique where, instead of multiple individual SELECT, INSERT, UPDATE or DELETE statements are executed to retrieve from, or store data in, at table, all of the operations are carried out at once, in bulk. This avoids the context-switching you get when the PL/SQL engine has to pass over to the SQL engine, then back to the PL/SQL engine, and so on, when you individually access rows one at a time. To do bulk binds with INSERT, UPDATE, and DELETE statements, you enclose the SQL statement within a PL/SQL FORALL statement. To do bulk binds with SELECT statements, you include the BULK COLLECT clause in the SELECT statement instead of using INTO.

According to the online documentation, BULK COLLECT is explained as;

"The keywords BULK COLLECT tell the SQL engine to bulk-bind output collections before returning them to the PL/SQL engine. You can use these keywords in the SELECT INTO, FETCH INTO, and RETURNING INTO clauses. Here is the syntax:

... BULK COLLECT INTO collection_name[, collection_name] ..."

and FORALL is defined as

"The keyword FORALL instructs the PL/SQL engine to bulk-bind input collections before sending them to the SQL engine. Although the FORALL statement contains an iteration scheme, it is not a FOR loop. Its syntax follows:

FORALL index IN lower_bound..upper_bound    sql_statement;

The index can be referenced only within the FORALL statement and only as a collection subscript. The SQL statement must be an INSERT, UPDATE, or DELETE statement that references collection elements. And, the bounds must specify a valid range of consecutive index numbers. The SQL engine executes the SQL statement once for each index number in the range."  

So there you go. Collections, BULK COLLECT and FORALL are the new features in Oracle 8i, 9i and 10g PL/SQL that can really make a different to you PL/SQL performance. Hopefully, if you've not come across these areas before, this has been of some use - certainly through writing this up I've found out quite a few things I'd only heard of in passing before. Lastly, if you want to read a good article on bulk binding and FORALL, take a look at "Oracle 10g Adds More to FORALL" by Steven Feuerstein.

原文:http://www.dba-oracle.com/oracle_tips_rittman_bulk%20binds_FORALL.htm

还可以参考:

http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96624/05_colls.htm#28329

http://www.oracle.com/technology/oramag/oracle/04-jan/o14tech_plsql.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值