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.
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,'04gQD6Q8uu6ipUbYZ1yKWnU2f8ZsDe6mvfQYLzaNs6ijSI8eRiHtuLyB1v0XtprqPPwRSmw2mKdTzP4tM722PKavefz84MrvtNzJYtA6uDXPDtqTGREXa0J77B1FcqMVWVqTv7AUwUhXu0GQCBOd8YGKRwd4015JzQao2P2CRVtLAy8otaHZ2Mlc1h0Ua8EZkChMAjEPkmEFhfKo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av7mtaifW4yyIVXcdxXOslBJUisGenwgE7PsdXehd0iqGKH8BBuOECjWPIuGG2aS51uRHUs31r2wxPpq4XxNcoP5B8V6T7cPINPbf7bjoMYI5aI8MRRQZBEH8aoZMaY0mhOYWeLKcCyrhf6v8eitWX4QY6wjQ5QwCloo0iS7ESCuQEjc5slwscscmRx3bKPTwV1e0tGyizKceUiwmxFiImC4me7ts1hYKk2AKdCWPwtEew3khFhV8dE2Bo1jLZRuTeCCSjuZKuejLTV0E7Xw3TxmAT7tlDrsd8JuDRuqriBF1TLkd0knIWG4cehoOZIEVVjLKTEABMAYOWm6JORttKrsx0FfAlzRcBJlLQofVhD3xVltnrIVYbczWkai0QQSiySib2c1WYa5t2FG0qaMNwGZf1BZagXrRQgexMS428EBrtmSCZYoNAcfVzSrCC6M5jkRyBHw','VZ7DnA7vcofPtMwPPulenNpC7ErJPaLsWy2XipWF02D1ZwzXCwC8hJ6MrII3PKYGLklG0SzrMqU0','BfjqAIQ6gwX3Xhjl338BqTJaMZUKxzMcdTovIPF6iD3lXeW4szUglzWNmFKXlYQ71cRQM1v0BAZwUqf1lRZfCsKnPlJIDdgy0JmHrfVHGGy84c3NMf7TRuNOeqj7xUYCV','Aox4fssjHKLaZwTS0Bq',57753,NULL,'C:\DOCS\TEMP75');
exit when c%notfound;
end loop;
close c;
commit;
end;
Related
A table is having 100 million records and I need to update a column by adding 10% into the salary of each employee. when I execute update statement I am getting this error:
ORA-01562: Failed to extend rollback segment
How can I update this column for the best performance result?
update employee
set salary = salary + (salary*10/100)
OR
declare
i number(10);
limit number(10) := 100000;
begin
for i in 1 .. limit loop
update employee
set salary = salary + (salary*10/100)
where rownum = i;
limit := limit + 100000;
end loop;
end;
Looks like you are using Oracle version 8i or prior, as the rollback segments have been replaced with undo segments from Oracle 9i onwards.
To solve the problem, I would suggest you to check the trace file to see which rollback segment is creating the problem, then create a bigger rollback segment depending upon the update transaction size.
Try this:
DECLARE
CURSOR CUR
IS
SELECT ROWID, A.*
FROM YOUR_SALARY_TABLE A;
TYPE CUR_TYPE IS TABLE OF CUR%ROWTYPE
INDEX BY PLS_INTEGER;
L_CUR CUR_TYPE;
LIM NUMBER := 100000; -- Update chunk size
BEGIN
OPEN CUR;
LOOP
FETCH CUR BULK COLLECT INTO L_CUR LIMIT LIM;
FOR INDX IN 1 .. L_CUR.COUNT
LOOP
UPDATE YOUR_SALARY_TABLE S
SET S.SALARY_COLUMN = S.SALARY_COLUMN * 2 -- Multiplying here
WHERE ROWID = L_CUR (INDX).ROWID;
END LOOP;
COMMIT;
EXIT WHEN L_CUR.COUNT < LIM;
END LOOP;
CLOSE CUR;
END;
You can try this approach: link
Information about parallel: link
We can use FORALL also to achieve what is required. Hope this below snippet helps.
DROP TABLE test_so1
/
CREATE TABLE TEST_SO1
( COL1 NUMBER, COL2 VARCHAR2(100)
)
/
--Insert values
INSERT INTO TEST_SO1
SELECT LEVEL,'AVRAJIT'||LEVEL FROM DUAL CONNECT BY LEVEL < 10000
/
--FORALL UPDATE
DECLARE
type TEST_REC
IS
RECORD
(
COL1 NUMBER,
COL2 VARCHAR2(100),
col3 VARCHAR2(100));
type TEST_TAB
IS
TABLE OF TEST_REC;
LV_TAB TEST_TAB;
CURSOR LV_CUR
IS
SELECT col1,col2,rowid FROM TEST_SO1;
BEGIN
OPEN LV_CUR;
LOOP
FETCH LV_CUR BULK COLLECT INTO LV_TAB LIMIT 1000;
EXIT
WHEN LV_TAB.COUNT=0;
FORALL I IN LV_TAB.FIRST..LV_TAB.LAST
UPDATE TEST_SO1 SET COL2 = 'shubhojit' WHERE ROWID = lv_tab(i).col3;
COMMIT;
END LOOP;
END;
/
In my following code, I'm unable to get the inserted row count within a cursor fetch.
...
row_count NUMBER:= 0;
BEGIN
OPEN object_id_cur(id, c_bool);
LOOP
FETCH object_id_cur INTO l_object_id;
EXIT WHEN object_id_cur%NOTFOUND;
INSERT INTO table1(
id,
num
)
SELECT t2.r_object_id,
t2.num
FROM table2
WHERE t2.r_object_id = l_object_id.r_object_id;
row_count:= row_count + SQL%ROWCOUNT;
-- I also tried dbms_output.put_line(SQL%ROWCOUNT);
END LOOP;
CLOSE object_id_cur;
COMMIT;
dbms_output.put_line('insert count= ' || row_count || ' rows inserted...');
END;
My obtained result is: count = rows inserted... count is blank. If I move the insert outside the cursor fetch, then the row count works just fine. Is there a logical explanation for this? Thanks!
count is a reserved keyword. Use "count" or some other identifer say cnt. Also, add the missing semicolon at then end of the increment statement.
declare
cnt number := 0;
begin
. . .
cnt := cnt + SQL%ROWCOUNT;
. . .
Demo:
SQL> create table t (id int);
Table created.
SQL> declare
2 cursor c is
3 select level n from dual connect by level <= 100; -- produces 100 rows
4 n int;
5 cnt number := 0;
6 begin
7 open c;
8 loop
9 fetch c into n;
10 exit when c%notfound;
11 insert into t (id)
12 select n from dual union all
13 select n from dual; -- insert each value twice
14 cnt := cnt + sql%rowcount;
15 end loop;
16 close c;
17 dbms_output.put_line(cnt); -- should be 200
18 end;
19 /
200 -- correct output
PL/SQL procedure successfully completed.
SQL>
As you can see above, the SQL%rowcount works correctly. It could be that your below select query is not producing any rows.
SELECT t2.r_object_id,
t2.num
FROM table2
WHERE t2.r_object_id = l_object_id.r_object_id;
SQL%ROWCOUNT return the number of rows fetched/processed by the last DML executed. If the DML fails after fetching 1 row, due to any reason, SQL%ROWCOUNT will return only 1, the number of rows fetched/processed so far. It wont give you the total count. I did a simple PLSQL block and getting the SQL%ROWCOUNT working fine. Seems there is something else which is not working in your code.
declare
num number := 0;
begin
for i in 1 .. 10
loop
insert into a_table (id)
values (i);
num := num + sql%rowcount;
end loop;
dbms_output.put_line (num);
end;
If we have a column in a table of type number, how can we store the result of select query on that column in an array ?
This sample uses a list (table of numbers) to achieve this, because i find
those lists much more handy:
CREATE OR REPLACE TYPE numberlist AS TABLE OF NUMBER;
DECLARE
v_numberlist numberlist;
BEGIN
SELECT intval numbercolumn
BULK COLLECT INTO v_numberlist
FROM lookup;
FOR i IN 1..v_numberlist.count
LOOP
dbms_output.put_line( v_numberlist(i) );
END LOOP;
END;
Create a type which store number:-
CREATE OR REPLACE TYPE varray is table of number;
--write your select query inside for loop () where i am extracting through level
declare
p varray := varray();
BEGIN
for i in (select level from dual connect by level <= 10) loop
p.extend;
p(p.count) := i.level;
end loop;
for xarr in (select column_value from table(cast(p as varray))) loop
dbms_output.put_line(xarr.column_value);
end loop;
END;
output:-
1
2
3
4
5
6
7
8
9
10
Just an option to use some native SQL datatype. Hope it helps.
SET SERVEROUTPUT ON;
DECLARE
lv_num_tab DBMS_SQL.NUMBER_TABLE;
BEGIN
SELECT LEVEL BULK COLLECT INTO lv_num_tab FROM DUAL CONNECT BY LEVEL < 10;
FOR I IN lv_num_tab.FIRST..lv_num_tab.LAST
LOOP
dbms_output.put_line(lv_num_tab(i));
END LOOP;
END;
You may also want to put the whole select in a table. You can use a BULK COLLECT to an array:
CREATE OR REPLACE TYPE t_my_list AS TABLE OF VARCHAR2(100);
CREATE OR REPLACE
PROCEDURE get_tables(p_owner in varchar2)
as
v_res t_my_list;
v_qry varchar2(4000) := '';
begin
v_qry := ' SELECT table_name from all_tables where owner='''||p_owner||'''';
dbms_output.put_line(v_qry);
-- all at once in the table
execute immediate v_qry bulk collect into v_res;
FOR I in 1..v_res.count
loop
dbms_output.put_line(v_res(i));
end loop;
exception
when others then
raise;
end get_tables;
/
begin
get_tables('E') ;
end;
/
I need to process close to 60k records of an Oracle table through a stored procedure. The processing is that for each such row, I need to delete and update a row in a second table and insert a row in a third table.
Using cursor looping, the procedure takes around 6-8 hours to complete. If I switch to Bulk Collect with Limit, the execution time is reduced but processing is not correct. Following is the bulk collect version of the procedure
create or replace procedure myproc()
is
cursor c1 is select col1,col2,col3 from tab1 where col4=3;
type t1 is table of c1%rowtype;
v_t1 t1;
begin
open c1;
loop
fetch c1 bulk collect into v_t1 limit 1000;
exit when v_t1.count=0;
forall i in 1..v_t1.count
delete from tab2 where tab2.col1=v_t1(i).col1;
commit;
forall i in 1..v_t1.count
update tab2 set tab2.col1=v_t1(i).col1 where tab2.col2=v_t1(i).col2;
commit;
forall i in 1..v_t1.count
insert into tab3 values(v_t1(i).col1,v_t1(i).col2,v_t1(i).col3);
commit;
end loop;
close c2;
end;
For around 20k of these records, the first delete operation is processed correctly but subsequent update and insert is not processed. For the remaining 40k records all three operations are processed correctly.
Am I missing something? Also what is the maximum LIMIT value I can use with Bulk Collect?
You should try using SAVE EXCEPTIONS clause of FORALL, something like (untested):
create or replace procedure myproc
as
cursor c1 is select col1,col2,col3 from tab1 where col4=3;
type t1 is table of c1%rowtype;
v_t1 t1;
dml_errors EXCEPTION;
PRAGMA exception_init(dml_errors, -24381);
l_errors number;
l_errno number;
l_msg varchar2(4000);
l_idx number;
begin
open c1;
loop
fetch c1 bulk collect into v_t1 limit 1000;
-- process v_t1 data
BEGIN
forall i in 1..v_t1.count SAVE EXCEPTIONS
delete from tab2 where tab2.col1=v_t1(i).col1;
commit;
EXCEPTION
when DML_ERRORS then
l_errors := sql%bulk_exceptions.count;
for i in 1 .. l_errors
loop
l_errno := sql%bulk_exceptions(i).error_code;
l_msg := sqlerrm(-l_errno);
l_idx := sql%bulk_exceptions(i).error_index;
-- log these to a table maybe, or just output
end loop;
END;
exit when c1%notfound;
end loop;
close c2;
end;
Can bulk collect be done if data is getting inserted into table A from Table B and while selecting data, substr, instr, trunc functions has been used on columns fetched?
INSERT INTO A
SELECT
DISTINCT
SUBSTR(b.component, 1, INSTR(b.component, ':', 1) - 1),
TRUNC(c.end_dt, 'DDD'),
FROM
B b,
C c
WHERE
TRUNC(c.end_dt)=TRUNC(b.last_Date, 'DDD') ;
How can I insert data into table A using bulk collect?
The only reason you'd use Bulk Collect and FORALL to insert rows is when you absolutely need to process/insert rows in chunks. Otherwise always use SQL.
DECLARE
CURSOR c_data IS
SELECT * FROM source_tab;
--
TYPE t_source_tab IS TABLE OF source_tab%ROWTYPE;
l_tab t_source_tab;
v_limit NUMBER:= 1000;
BEGIN
OPEN c_data;
LOOP
FETCH c_data BULK COLLECT INTO l_tab LIMIT v_limit;
EXIT WHEN l_tab.count = 0;
-- Insert --
FORALL i IN l_tab.first .. l_tab.last
INSERT INTO destination_tab VALUES l_tab(i);
COMMIT;
-- prints number of rows processed --
DBMS_OUTPUT.PUT_LINE ('Inserted ' || SQL%ROWCOUNT || ' rows:');
-- Print nested table of records - optional.
-- May overflow the buffer and slow down the performance if you process many rows.
-- Use for testing only and limit the rows with Rownum or Row_Number() in cursor query:
FOR i IN l_tab.FIRST..l_tab.LAST -- or l_tab.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE (l_tab(i).hire_date ||chr(9)||l_tab(i).last_name ||chr(9)||l_tab(i).first_name);
END LOOP;
END LOOP;
CLOSE c_data;
END
/