forall insert需要更多时间插入

时间:2014-11-05 07:08:24

标签: sql insert

forall insert花费更多时间插入应用程序支付表中只有4000行..有没有办法优化我的代码

DECLARE
   TYPE PAY_TAB_REC IS RECORD (
      PAYMENT_ID           NUMBER,
      PAYMENT_TYPE_ID      NUMBER,
      AMOUNT               NUMBER,
      PAYMENT_DATE         DATE,
      CREATED_DATE         DATE,
      CREATED_BY           NUMBER,
      PAYMENT_RECEIPT_NO   VARCHAR2 (20),
      MODIFIED_BY          NUMBER,
      MODIFIED_DATE        DATE,
      PAYEE_ID             NUMBER (10),
      PAYER_ID             NUMBER (10),
      IS_URGENT            NUMBER (1)    DEFAULT 0,
      APP_ID               NUMBER
   );

   TYPE PAY_TAB_TYPE IS TABLE OF PAY_TAB_REC
      INDEX BY PLS_INTEGER;

   PAY_TAB   PAY_TAB_TYPE;

   CURSOR C_APP
   IS
      SELECT SEQ_PAYMENT.NEXTVAL PAYMENT_ID, PAYMENT_TYPE_ID, AMOUNT,
             PAYMENT_DATE, CREATED_DATE, CREATED_BY, PAYMENT_REF_NO,
             MODIFIED_BY, MODIFIED_DATE, PAYEE_ID, PAYER_ID, IS_URGENT,
             APP_ID
        FROM APPLICATION_PAYMENT;
BEGIN
   OPEN C_APP;

   LOOP
      FETCH C_APP
      BULK COLLECT INTO PAY_TAB LIMIT 100;

      FORALL I IN 1 .. PAY_TAB.COUNT
         INSERT INTO PAYMENT
              VALUES PAY_TAB (I);
   END LOOP;

   CLOSE C_APP;

   COMMIT;
END;

从application_payment表中选择值并插入到付款表中。但它花费更多的时间和循环运行没有尽头。请帮我这个在application_payment表中只有78000条记录。

1 个答案:

答案 0 :(得分:0)

您可以使用select insert而不是循环

INSERT INTO PAYMENT
 SELECT SEQ_PAYMENT.NEXTVAL PAYMENT_ID, PAYMENT_TYPE_ID, AMOUNT,
             PAYMENT_DATE, CREATED_DATE, CREATED_BY, PAYMENT_REF_NO,
             MODIFIED_BY, MODIFIED_DATE, PAYEE_ID, PAYER_ID, IS_URGENT,
             APP_ID
        FROM APPLICATION_PAYMENT;