简体   繁体   English

Oracle BULK FETCH和FORALL插入的性能问题

[英]Performance problem with Oracle BULK FETCH and FORALL insert

I am trying to copied records from one table to another as fast as possible. 我正在尝试尽快将记录从一个表复制到另一个表。

Currently I have a simple cursor loop similiar to this: 目前,我有一个与此类似的简单光标循环:

FOR rec IN source_cursor LOOP
   INSERT INTO destination (a, b) VALUES (rec.a, rec.b)
END LOOP;

I want to speed it up to be super fast so am trying some BULK operations (a BULK FETCH, then a FORALL insert): 我想将其加快到超快的速度,因此尝试进行一些BULK操作(BULK FETCH,然后进行FORALL插入):

Here is what I have for the bulk select / forall insert. 这是批量选择/全部插入的功能。

DECLARE 
  TYPE t__event_rows IS TABLE OF _event%ROWTYPE;
  v__event_rows t__event_rows;

  CURSOR c__events IS
    SELECT * FROM _EVENT ORDER BY MESSAGE_ID;
BEGIN
  OPEN c__events;
  LOOP
    FETCH c__events BULK COLLECT INTO v__event_rows LIMIT 10000;  -- limit to 10k to avoid out of memory

    EXIT WHEN c__events%NOTFOUND;

    FORALL i IN 1..v__event_rows.COUNT SAVE EXCEPTIONS
      INSERT INTO destinatoin
        ( col1, col2, a_sequence) 
        VALUES 
        (  v__event_rows(i).col1,  v__event_rows(i).col2, SOMESEQEUENCE.NEXTVAL );


  END LOOP;
  CLOSE c__events;


END;

My problem is that I'm not seeing any big gains in performance so far. 我的问题是,到目前为止,我看不到任何性能上的大进步。 From what I read it should be 10x-100x faster. 根据我的阅读,它应该快10到100倍。

Am I missing a bottleneck here somewhere? 我在这里某个地方缺少瓶颈吗?

The only benefit your code has over a simple INSERT + SELECT is that you save exceptions, plus (as Justin points out) you have a pointless ORDER BY which is making it do a whole lot of meaningless work. 与简单的INSERT + SELECT ,您的代码唯一的好处是您可以保存异常,而且(如Justin所指出的)您将拥有毫无意义的ORDER BY ,这使它可以执行很多无意义的工作。 You then don't have any code to do anything with the exceptions that were saved, anyway. 这样,您就没有任何代码可以对已保存的异常执行任何操作。

I'd just implement it as a INSERT + SELECT . 我只是将其实现为INSERT + SELECT

除非编码本身需要,否则不必不必要地使用循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM