Bulk insert using FORALL in Oracle

5.4k views Asked by At

I am currently using the following code to do a bulk insert of 50 million rows:

declare cnt number;
BEGIN 
    select max(run_ver_issue_id) into cnt from "ABHINAV"."MV_RUN_VER_ISSUE";
    FOR x IN 1 .. 50000000 LOOP
         INSERT INTO "ABHINAV"."MV_RUN_VER_ISSUE" NOLOGGING ("RUN_VER_ISSUE_ID","CUST_ISSUE_ID","TITLE","ORIGINAL_ISSUE_ID","SOURCE_FILE_LINE","DISPLAY_TEXT","RUN_VER_ID","SANDBOX_SRC_FILE_ID","ACCOUNT_ID","ISSUE_STATE","CURRENT_SEVERITY","INSERT_TS","MODIFIED_TS","CWE_ID","TEMPLATE_FILE_ID","TEMPLATE_FILE_LINE","CURRENT_EXPLOIT_LEVEL","CNT","SOURCE_FUNCTION_PROTOTYPE","SOURCE_RELATIVE_LOCATION","SOURCE_SCOPE","PROCEDURE_NAME","PROCEDURE_HASH","PROTOTYPE_HASH","STATEMENT_HASH","STATEMENT_HASH_COUNT","STATEMENT_HASH_ORDINAL","SUBMODULE_PATH") VALUES (x+cnt,2,..);
    END LOOP;
END;

It takes almost 6 hours it to finish. The table does have an index on RUN_VER_ISSUE_ID, but still doesn't help. I tried using FORALL, getting an error PLS-00430: FORALL iteration variable X is not allowed in this context for the below code:

declare cnt number;
BEGIN 
    select max(run_ver_issue_id) into cnt from "ABHINAV"."MV_RUN_VER_ISSUE";
    FORALL x IN 1 .. 5
    INSERT INTO "ABHINAV"."MV_RUN_VER_ISSUE" NOLOGGING ("RUN_VER_ISSUE_ID","CUST_ISSUE_ID","TITLE","ORIGINAL_ISSUE_ID","SOURCE_FILE_LINE","DISPLAY_TEXT","RUN_VER_ID","SANDBOX_SRC_FILE_ID","ACCOUNT_ID","ISSUE_STATE","CURRENT_SEVERITY","INSERT_TS","MODIFIED_TS","CWE_ID","TEMPLATE_FILE_ID","TEMPLATE_FILE_LINE","CURRENT_EXPLOIT_LEVEL","CNT","SOURCE_FUNCTION_PROTOTYPE","SOURCE_RELATIVE_LOCATION","SOURCE_SCOPE","PROCEDURE_NAME","PROCEDURE_HASH","PROTOTYPE_HASH","STATEMENT_HASH","STATEMENT_HASH_COUNT","STATEMENT_HASH_ORDINAL","SUBMODULE_PATH") VALUES (x+cnt,2,.....);
END;

I would request you to please provide your suggestions and comments as to how I can speed this up.

1

There are 1 answers

12
Barbaros Özhan On BEST ANSWER

you may use this:

declare 
    p_array_size number := 100000; 
    type array is table of number;
    l_data ARRAY;
    cursor c is select rownum seq_no from dual connect by rownum <= p_array_size; 
    cnt number;
begin 
    open c;
    loop
        select max(RUN_VER_ISSUE_ID) into cnt from "ABHINAV"."MV_RUN_VER_ISSUE"; 
        fetch c bulk collect into l_data limit p_array_size;
        forall x IN 1 .. l_data.count
        insert into "ABHINAV"."MV_RUN_VER_ISSUE"("RUN_VER_ISSUE_ID","CUST_ISSUE_ID","TITLE","ORIGINAL_ISSUE_ID","SOURCE_FILE_LINE","DISPLAY_TEXT","RUN_VER_ID","SANDBOX_SRC_FILE_ID","ACCOUNT_ID","ISSUE_STATE","CURRENT_SEVERITY","INSERT_TS","MODIFIED_TS","CWE_ID","TEMPLATE_FILE_ID","TEMPLATE_FILE_LINE","CURRENT_EXPLOIT_LEVEL","CNT","SOURCE_FUNCTION_PROTOTYPE","SOURCE_RELATIVE_LOCATION","SOURCE_SCOPE","PROCEDURE_NAME","PROCEDURE_HASH","PROTOTYPE_HASH","STATEMENT_HASH","STATEMENT_HASH_COUNT","STATEMENT_HASH_ORDINAL","SUBMODULE_PATH") 
        values(l_data(x)+cnt,2,'0pJDFy1viDnN2Ku66XPl458gKakF4xz4pTawwoOZYCt0yR2YOw7tH1Mk3oKQ7ynuyasbBjBXr1teBbBdmIsJReIM3kbzWY7H4c6CVgVaw',NULL,230969,'04gQD6Q8uu6ipUbYZ1yKWnU2f8ZsDe6mvfQYLzaNs6ijSI8eRiHtuLyB1v0XtprqPPwRSmw2mKdTzP4tM722PKavefz84MrvtNzJYtA6uDXPDtqTGREXa0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FAej1chgxsSuU7OxF1dOtfzuneNH0UWE1F6ynN2Wum3xbzoer5rKsv2suOba80LxdeCHEKNyOyK5860x6JZhEJZIUfgMF4HTav7mtaifW4yyIVXcdxXOslBJUisGenwgE7PsdXehd0iqGKH8BBuOECjWPIuGG2aS51uRHUs31r2wxPpq4XxNcoP5B8V6T7cPINPbf7bjoMYI5aI8MRRQZBEH8aoZMaY0mhOYWeLKcCyrhf6v8eitWX4QY6wjQ5QwCloo0iS7ESCuQEjc5slwscscmRx3bKPTwV1e0tGyizKceUiwmxFiImC4me7ts1hYKk2AKdCWPwtEew3khFhV8dE2Bo1jLZRuTeCCSjuZKuejLTV0E7Xw3TxmAT7tlDrsd8JuDRuqriBF1TLkd0knIWG4cehoOZIEVVjLKTEABMAYOWm6JORttKrsx0FfAlzRcBJlLQofVhD3xVltnrIVYbczWkai0QQSiySib2c1WYa5t2FG0qaMNwGZf1BZagXrRQgexMS428EBrtmSCZYoNAcfVzSrCC6M5jkRyBHw','VZ7DnA7vcofPtMwPPulenNpC7ErJPaLsWy2XipWF02D1ZwzXCwC8hJ6MrII3PKYGLklG0SzrMqU0','BfjqAIQ6gwX3Xhjl338BqTJaMZUKxzMcdTovIPF6iD3lXeW4szUglzWNmFKXlYQ71cRQM1v0BAZwUqf1lRZfCsKnPlJIDdgy0JmHrfVHGGy84c3NMf7TRuNOeqj7xUYCV','Aox4fssjHKLaZwTS0Bq',57753,NULL,'C:\DOCS\TEMP75');
      exit when c%notfound;
    end loop;
    close c;   
    commit;
end;