I wrote the script below in Oracle that inserts thousands of rows in a table and uses the auto-generated id resulted from these rows and uses it in other inserts.
The script is working as expected but the issue is that it takes time to finish. Following are some details of the contents of each table currently:
- table_0 contains 16000+ rows
- table_1 contains 4000+ rows
With these volumes the script takes around 15 to 20 seconds. The issue is that I intend to use a similar query to handle Millions of rows.
Here is the code for a function called by the script:
create or replace FUNCTION get_id (name1 IN varchar2) RETURN INTEGER
as res_id INTEGER;
begin
select id into res_id from table_1 where node_type='type1' and name =
name1;
return res_id;
end;
/
Here is the script itself:
DECLARE
TYPE rt IS RECORD (text1 varchar2(20),text2 varchar(20));
TYPE texts_tab IS TABLE OF rt;
TYPE ids_tab IS TABLE OF table_1.id%TYPE;
p_texts texts_tab;
p_ids ids_tab;
id_2 integer;
CURSOR c IS
SELECT DISTINCT text1,text2 FROM table_0 order by text1,text2;
BEGIN
select FUNC1('type2') into id_2 from dual;
OPEN c;
LOOP
FETCH c BULK COLLECT INTO p_texts LIMIT 1000;
FORALL i IN 1 .. p_texts.COUNT
INSERT INTO table_2(object_id,object_type,parent_id)
VALUES (SEQ_ID.NEXTVAL, id_2 ,get_id(p_texts(i).text1) ,0,0)
RETURNING object_id BULK COLLECT INTO p_ids;
FORALL i IN 1 .. p_ids.COUNT
insert into table_3 (object_id,field2)
VALUES ( p_ids(i), p_texts(i).text2 );
FORALL i IN 1 .. p_ids.COUNT
insert into table_1 (node_type,text1,id)
VALUES('type2', p_texts(i).text1 , p_ids(i));
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
COMMIT;
END;
/