Run stored procedures parallely - oracle

I have a master stored procedure:
MASTER();
In this stored procedure, I call 3 other stored procedures:
SP1();
SP2();
SP3();
Right now, it's running serially, i.e. one after the other. I want to run it in parallel and once all the 3 stored procedures are completely executed, run next part of MASTER() stored procedure.
I am using Oracle Standard One Edition 11.2. How can I achieve this?

One way to archive that is to use DBMS_JOB or DBMS_SCHEDULER to launch the procedures in parallel and DBMS_ALERT to notify the master procedure when they are finished.

There is other way to achieve this.
Set up envirement:
create table t_procedure(id number, procedures varchar2(200));
create type l_char is table of varchar2(100);
create procedure goSleeep(p_sec number)is
begin
dbms_lock.sleep( p_sec );
end;
Package:
create or replace package goParallel is
TYPE t_referential_cursor IS REF CURSOR RETURN t_procedure%ROWTYPE;
function runParallel(p_cursor t_referential_cursor)
return l_char pipelined
parallel_enable(partition p_cursor BY HASH(id));
end;
create or replace package body goParallel is
function runParallel(p_cursor t_referential_cursor)
return l_char pipelined
parallel_enable(partition p_cursor BY HASH(id))
is
v_start date := sysdate;
v_end date;
vid number;
p_proc varchar2(200);
begin
loop
fetch p_cursor into vid, p_proc;
exit when p_cursor%notfound;
execute immediate p_proc;
v_end := sysdate;
pipe row( vid||' --- '||to_char(v_start,'HH24:MI:SS')||' - '|| to_char(v_end,'HH24:MI:SS'));
end loop;
return;
end;
end;
Insert some procedures to run.
insert into t_procedure values (1, 'begin goSleeep(5); end;');
insert into t_procedure values (2, 'begin goSleeep(8); end;');
insert into t_procedure values (3, 'begin goSleeep(9); end;');
commit;
And run it with strange way.
select * from table(goParallel.runParallel(cursor(select /*+ PARALLEL(a 8) */ * from t_procedure a)));
-- result: id - start - end
1 --- 12:15:54 - 12:15:59
2 --- 12:15:54 - 12:16:02
3 --- 12:15:54 - 12:16:03

Related

Parallel execute oracle PL/SQL [duplicate]

This question already has answers here:
Can We use threading in PL/SQL?
(9 answers)
Closed 6 years ago.
Basically I have a package in oracle 11g processing a file and validating and inserting information in multiple tables,
to achiev this I create a stored procedure that reads the file and spread the information, then I call multiple stored procedures to
validate and insert the data in each table (one procedure per table), for errors each SP insert a record in a common error table, at the end,
I call one last stored procedure that identifies if there is errors in the common error table and generate a file with those errors.
Now... I'm trying to improve the code in order to minimize times of execution, then I realice that each SP that validates and insert info
into table does not depend from other SP information, so I'm asking if there is a way to call all this SP in parallel.
TODAY
STORED PROCEDURE charge_file
STORED PROCEDURE insert_table1
STORED PROCEDURE insert_table2
STORED PROCEDURE insert_table3 ...
STORED PROCEDURE return_file
What I am trying to do
STORED PROCEDURE charge_file
STORED PROCEDURE insert_table1 - STORED PROCEDURE insert_table2 - STORED PROCEDURE insert_table3 ...
STORED PROCEDURE return_file
As example using parallel_execute:
create table proc_map (proc_id number, proc_name varchar2(64), is_active varchar2(1));
insert into proc_map (proc_id, proc_name, is_active) values (1, 'insert_table1', 'Y');
insert into proc_map (proc_id, proc_name, is_active) values (2, 'insert_table2', 'Y');
insert into proc_map (proc_id, proc_name, is_active) values (3, 'insert_table3', 'Y');
create or replace procedure p_run_proc (ip_start in number, ip_end in number) is
v_proc_name proc_map.proc_name%type;
begin
begin
select t.proc_name into v_proc_name
from proc_map t
where t.proc_id = ip_start;
exception
when no_data_found then null;
when too_many_rows then null;
end;
if v_proc_name is not null
then
execute immediate 'begin ' || v_proc_name || '; end;';
end if;
end;
declare
v_task_name varchar2(4000) := dbms_parallel_execute.generate_task_name;
v_sql varchar2(4000);
v_run varchar2(4000);
v_thread_count number;
v_task_status number;
begin
dbms_parallel_execute.create_task (task_name => v_task_name);
v_sql := 'select t.proc_id as num_col
,t.proc_id as num_col
from proc_map t
where t.is_active = ''Y''
order by t.proc_id';
dbms_parallel_execute.create_chunks_by_SQL (task_name => v_task_name, sql_stmt => v_sql, by_rowid => false);
v_run := 'begin p_run_proc (ip_start => :start_id, ip_end => :end_id); end;';
select count(*) into v_thread_count
from proc_map t
where t.is_active = 'Y';
dbms_parallel_execute.run_task (task_name => v_task_name
,sql_stmt => v_run
,language_flag => dbms_sql.native
,parallel_level => v_thread_count);
v_task_status := dbms_parallel_execute.task_status (task_name => v_task_name);
if v_task_status = dbms_parallel_execute.FINISHED
then
dbms_parallel_execute.drop_task (task_name => v_task_name);
else
raise_application_error (-20001, 'ORA in task ' || v_task_name);
end if;
end;
There is no built-in fork/background submit in PL/SQL, other than DBMS_SCHEDULER or the older DBMS_JOB. (DBMS_SCHEDULER is more fully featured and has far better tracking for failures.) I wrote a job control object a few years ago as a project but to be honest I've never used it. www.williamrobertson.net/documents/job-control-object.html
You can use run_job or submit.
Refer below link for example :-
http://www.dba-oracle.com/r_execute_pl_sql_in_parallel.htm

Pl/sql Executing procedures

I am totally new to PL/SQL.
create or replace procedure p1(a in customer.id%type,
b out customer.name%type,
c out customer.dept%type)
is
begin
select name,dept into b,c from customer where id=a;
end;
Its created properly.
But I am not sure how to execute it.
EXEC p1(1);
But this is showing error.
Your procedure has three parameters so you'd need to call it with three parameters. In the case of OUT parameters, you need to pass in variables that will hold the values that are being returned by the procedure.
DECLARE
l_id customer.id%type := 1;
l_name customer.name%type;
l_dept customer.dept%type;
BEGIN
p1( l_id, l_name, l_dept );
<<do something with l_name and l_dept>>
END;
/
There are two ways to execute a procedure.
From the SQL prompt.
EXECUTE [or EXEC] procedure_name;
Within another procedure – simply use the procedure name.
procedure_name;
the procedure have 3 parameters, you can't call it like p1(1) using just one parameter
in your case try something like this
DECLARE
p_name customer.name%type;
p_department customer.dept%type;
BEGIN
p1(1, p_name, p_department);
END;
Output parameters should be stored in variables.
Input, can be variables or directly given in between the ( )
Use dbms_output.put_line to easily show output in your IDE.
DECLARE
p_name customer.name%type;
p_department customer.dept%type;
p_id customer.id%type := 1;
BEGIN
p1(p_id, p_name, p_department);
END;

pass pl/sql record as arguement to procedure

How to pass pl/sql record type to a procedure :
CREATE OR REPLACE PACKAGE BODY PKGDeleteNumber
AS
PROCEDURE deleteNumber (
list_of_numbers IN List_Numbers
)
IS
i_write VARCHAR2(5);
BEGIN
--do something
END deleteNumber;
END PKGDeleteNumber;
/
In this procedure deleteNumber I have used List_Numbers, which is a record type. The package declaration for the same is :
CREATE OR REPLACE PACKAGE PKGDeleteNumber
AS
TYPE List_Numbers IS RECORD (
IID NUMBER
);
TYPE list_of_numbers IS TABLE OF List_Numbers;
PROCEDURE deleteNumber (
list_of_numbers IN List_Numbers
);
END PKGDeleteNumber;
I have to execute the procedure deleteNumber passing a list of values. I inserted numbers in temp_test table, then using a cursor U fetched the data from it :
SELECT *
BULK COLLECT INTO test1
FROM temp_test;
Now, to call the procedure I am using
execute immediate 'begin PKGDELETENUMBER.DELETENUMBER(:1); end;'
using test1;
I have tried many other things as well(for loop, dbms_binding, etc). How do I pass a pl/sql record type as argument to the procedure?
EDIT:
Basically, I want to pass a list of numbers, using native dynamic sql only...
adding the table temp_test defn (no index or constraint):
create table test_temp (
IID number
);
and then inserted 1,2,3,4,5 using normal insert statements.
For this solution,
In a package testproc
CREATE TYPE num_tab_t IS TABLE OF NUMBER;
CREATE OR REPLACE PROCEDURE my_dyn_proc_test (p_num_array IN num_tab_t) AS
BEGIN
dbms_output.put_line(p_num_array.COUNT);
END;
/
this is called from sql prompt/toad
DECLARE
v_tab testproc.num_tab_t := testproc.num_tab_t(1, 10);
BEGIN
EXECUTE IMMEDIATE 'BEGIN testproc.my_dyn_proc_test(:1); END;' USING v_tab;
END;
this will not work.This shows error.I am not at my workstation so am not able to reproduce the issue now.
You can't use RECORD types in USING clause of EXECUTE IMMEDIATE statement. If you just want to pass a list of numbers, why don't you just use a variable of TABLE OF NUMBER type? Check below example:
CREATE TYPE num_tab_t IS TABLE OF NUMBER;
CREATE OR REPLACE PROCEDURE my_dyn_proc_test (p_num_array IN num_tab_t) AS
BEGIN
dbms_output.put_line(p_num_array.COUNT);
END;
/
DECLARE
v_tab num_tab_t := num_tab_t(1, 10);
BEGIN
EXECUTE IMMEDIATE 'BEGIN my_dyn_proc_test(:1); END;' USING v_tab;
END;
Output:
2
Edit
Try this:
CREATE TYPE num_tab_t IS TABLE OF NUMBER;
CREATE OR REPLACE PACKAGE testproc AS
PROCEDURE my_dyn_proc_test (p_num_array IN num_tab_t);
END;
/
CREATE OR REPLACE PACKAGE BODY testproc AS
PROCEDURE my_dyn_proc_test (p_num_array IN num_tab_t) AS
BEGIN
dbms_output.put_line(p_num_array.COUNT);
END;
END;
/
DECLARE
v_tab num_tab_t := num_tab_t(1, 10);
BEGIN
EXECUTE IMMEDIATE 'BEGIN testproc.my_dyn_proc_test(:1); END;' USING v_tab;
END;
Use an object type. object types are visible to all packages

Inserting a RefCursor into a table

I have a simple function where I run a stored procedure that returns a RefCursor and I try to use that RefCursor to insert data to a temporary table. I get the following error when trying to do so:
SQL Error: ORA-00947: not enough values
I know for a fact that the refcursor returns exactly the same number of values as the temporary table has, correct column names, their order and their type. I ran print RefCursor and I can see all of the data. Here's the code:
var r refcursor;
EXEC SCHEMA.PACKAGE.SPROC(:r);
insert into SCHEMA.TEMP_TABLE
values
(r);
I have to add that the stored procedure has a refcursor defined as a OUT parameter so it returns a correct type. Using print r; prints the correct data.
What am I doing wrong?
EDIT:
Based on a suggestion I tried to use a fetch to a rowtype variable, but getting Invalid Number exception whenever I attempt to fetch a row:
DECLARE
cur SYS_refcursor;
rec SCHEMA.TEMP_TABLE%rowtype;
begin
SCHEMA.PACKAGE.SPROC( cur );
LOOP
FETCH cur INTO rec;
EXIT WHEN cur%NOTFOUND;
INSERT INTO SCHEMA.TEMP_TABLE
VALUES rec;
END LOOP;
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_output.put_line(rec.move_id);
end;
I added the exception block to see which row is failing and needless to say it is the first one. The stored procedure I run returns a refcursor of a select query from multiple tables. The temporary table defined as the exact copy of the refcursor columns and their types. Not sure what could be causing the exception.
You can't insert into a table from a refcursor. You could write a procedure that fetches from the cursor and inserts into the table. If schema.package.sproc returns a ref cursor of temp_table%rowtype, you could do something like
DECLARE
cur sys_refcursor;
rec schema.temp_table%rowtype;
BEGIN
schema.package.sproc( cur );
LOOP
FETCH cur INTO rec;
EXIT WHEN cur%NOTFOUND;
INSERT INTO schema.temp_table
VALUES rec;
END LOOP;
END;
You can use LOOP + FETCH to filter the row in SYS_REFCURSOR.
Exit the LOOP using "EXIT WHEN ref_%notfound;"
Example: Have 2 functions.
FUNCTION get_data
RETURN SYS_REFCURSOR
is
s varchar2(2000);
ref_ SYS_REFCURSOR;
begin
s := 'select username, password, email from user_info where id < 100';
OPEN ref_ FOR s;
return ref_;
end;
FUNCTION load_data_in_table
RETURN varchar2
is
s varchar2(2000);
puser_name varchar2(2000);
ppassword varchar2(2000);
pemail varchar2(2000);
ref_ SYS_REFCURSOR;
begin
ref_ := get_data();
LOOP
--process_record_statements;
FETCH ref_ into puser_name, ppassword, pemail;
s := 'INSERT INTO TEST_USER_EXP VALUES(:user_name, :password, :email)';
EXECUTE IMMEDIATE s USING puser_name, ppassword, pemail;
EXIT WHEN ref_%notfound;
END LOOP;
commit;
return 'OK';
end;

How to use function returning Oracle REF_CURSOR in a procedure

I have to write an Oracle procedure which should invoke an Oracle function returning REF_CURSOR. The function is declared like that
FUNCTION "IMPACTNET"."TF_CONVERTPARA" (PARASTRING IN NVARCHAR2) RETURN SYS_REFCURSOR
AS
c SYS_REFCURSOR;
BEGIN
OPEN c FOR
SELECT SUBSTR(element, 1, INSTR(element, '|') - 1) as key,
SUBSTR(element, INSTR(element, '|') + 1, 99999) as val
FROM (
SELECT REGEXP_SUBSTR(PARASTRING, '[^;]+', 1, LEVEL) element
FROM dual
CONNECT BY LEVEL < LENGTH(REGEXP_REPLACE(PARASTRING, '[^;]+')) + 1
);
RETURN c;
END;
Can you tell me what I need to write in order to invoke the function from within my procedure? I'd like to insert all the returned values (shaped a table with two columns) into a rational table.
Thank you in advance!
Something along the lines of this should work (obviously, I'm guessing about table names and column names and the exact logic that you're trying to implement)
CREATE PROCEDURE some_procedure_name
AS
l_rc SYS_REFCURSOR := impactnet.tf_convertpara( <<some string>> );
l_key VARCHAR2(100);
l_val VARCHAR2(100);
BEGIN
LOOP
FETCH l_rc
INTO l_key, l_val;
EXIT WHEN l_rc%notfound;
INSERT INTO some_table( key_column, val_column )
VALUES( l_key, l_val );
END LOOP;
END;
As Ollie points out, it would be more efficient to do a BULK COLLECT and a FORALL. If you're just dealing with a few thousand rows (since your function is just parsing the data in a delimited string, I'm assuming you expect relatively few rows to be returned), the performance difference is probably minimal. But if you're processing more data, the difference can be quite noticeable. Depending on the Oracle version and your specific requirements, you may be able to simplify the INSERT statement in the FORALL to insert a record rather than listing each column from the record individually.
CREATE PROCEDURE some_procedure_name
AS
TYPE key_val_rec
IS RECORD(
key VARCHAR2(100),
val VARCHAR2(100)
);
TYPE key_val_coll
IS TABLE OF key_val_rec;
l_rc SYS_REFCURSOR := impactnet.tf_convertpara( <<some string>> );
l_coll key_val_coll;
BEGIN
LOOP
FETCH l_rc
BULK COLLECT INTO l_coll
LIMIT 100;
EXIT WHEN l_coll.count = 0;
FORALL i IN l_coll.FIRST .. l_coll.LAST
INSERT INTO some_table( key_column, val_column )
VALUES( l_coll(i).key, l_coll(i).val );
END LOOP;
END;

Resources