We help IT Professionals succeed at work.

parallel insert on non-partitioned tables using dbms_parallel_execute.

mallelaajay
mallelaajay asked
on
Hi,
    I have a requirement where i need to do a parallel insert into 8 tables using dbms_parallel_execute. The requirement is as below:

create chunks by SQL:    select distinct level_key, level_key from chunk_table.

chunk_table would be as below:

level_key      table_name
-----------   ------------------
101            sales_level101
102            sales_level102
  -                 -        -      -
108            sales_level108

I need to do a parallel insert into all 8 tables by mentioning parallel level 8. My run_task SQL should be as below

insert into table_name select /*+ parallel(stg, 8) */ from staging_table stg where level_key=:start_id

All the 8 different sales level of data is in the staging_table. Based on level_key, the data should be inserted into respective tables.

Is this possible? It at all, please give some ideas.
Comment
Watch Question

johnsoneSenior Oracle DBA
CERTIFIED EXPERT

Commented:
The one line overview of that package from the documentation is this:

This package enables the user to incrementally update table data in parallel, in two high level steps.

That says update and not insert.  Also, from the examples supplied, they all work on the same table.

Are you trying to kick off 8 jobs that select from one table and insert into 8 other tables based on id?  Or are you trying to kick off 8 jobs that select from one table and insert into one table based on id?  Or, am I missing somethings?

Author

Commented:
I'm trying to kick off 8 jobs that select from one table and insert into 8 other tables based on id.

Author

Commented:
It actually does the insert in parallel. I have tried inserting the data in chunks into a partitioned table before. But, the thing i'm trying to do here is, insert into 8 different non-partitioned tables selecting from a single table.
johnsoneSenior Oracle DBA
CERTIFIED EXPERT

Commented:
From my reading of the documentation on that package, what you are trying to do cannot be done with that package.

My suggestion would be to kick off 8 jobs via DBMS_SCHEDULER that do the 8 separate inserts.  Be careful with your parallel hint, as with 8 jobs, you would have 64 parallel query threads (assuming there is no index on LEVEL_KEY.
I posted this on one of the other forums and one of the guys suggested me the below:

begin
begin
DBMS_PARALLEL_EXECUTE.DROP_TASK(task_name => 'TASK_NAME');
exception when others then null;
end;
DBMS_PARALLEL_EXECUTE.CREATE_TASK(task_name => 'TASK_NAME');
DBMS_PARALLEL_EXECUTE.CREATE_CHUNKS_BY_SQL(task_name => 'TASK_NAME',
sql_stmt =>'select distinct level_key, level_key from chunk_table', by_rowid => false);
end;


begin
DBMS_PARALLEL_EXECUTE.RUN_TASK (task_name => 'TASK_NAME',
sql_stmt =>'declare
s varchar2(16000); vstart_id number := :start_id; vend_id number:= :end_id;
table_name varchar2(30);
begin
select table_name into table_name from chunk_table where level_key=vstart_id;
s:=''insert into ''||table_name||'' select /*+ PARALLEL(STG, 8) */ colx from STAGING_TABLE STG
where level_key =:vstart_id'';
execute immediate s using vstart_id;
commit;
end;',
language_flag => DBMS_SQL.NATIVE, parallel_level => 2 );
end;


and it worked. Thanks for the help...

Author

Commented:
Figured it out