gpt4 book ai didi

multithreading - Oracle PL/SQL 中有多线程吗?

转载 作者:行者123 更新时间:2023-12-02 19:29:32 25 4
gpt4 key购买 nike

现在,我在 PL/SQL ETL 脚本中按顺序运行数据转换。

我注意到有些转换并不相互依赖。有没有办法在 PL/SQL 中打开线程并卸载一些处理,以便作业更快完成?

我尝试了一些性能选项,包括设置并行度,但它限制了 session 。

我无法使用 Java,也无法在 Oracle 之外设计作业 - 它必须是 PL/SQL。

最佳答案

如果您尝试“并行度”设置,我认为可以并行运行一些 ETL 操作,因此您将有一个“任务”,它将等待它的子线程,每个线程只会处理它的“chunk”(所有记录的子集)。从 11g 第 2 版开始,Oracle 中有一个包 DBMS_PARALLEL_EXECUTE 来管理它。我有一个简单的例子。

准备测试数据:

create table test_parallel_processing(
col_id int
, col number
)

begin
for i in 1..1000 loop
execute immediate 'insert into test_parallel_processing
select :p1, 100 from dual' using i;
end loop;
commit;
end;

以 4 个 block 处理所有 1000 条记录,并行度为 2:

declare
v_sql varchar2(1000);
v_chunking_sql varchar2(1000);
begin
/*we can use our own logic of deviding all scope to chunks:*/
v_chunking_sql := ' select 1 as start_id, 250 as end_id from dual
union all
select 251 as start_id, 500 as end_id from dual
union all
select 501 as start_id, 750 as end_id from dual
union all
select 751 as start_id, 1000 as end_id from dual';

DBMS_PARALLEL_EXECUTE.CREATE_TASK ('mytask');
/*creating chunks with our sql*/
DBMS_PARALLEL_EXECUTE.CREATE_CHUNKS_BY_SQL(
task_name => 'mytask'
, sql_stmt => v_chunking_sql
, by_rowid => false
);

/*SQL which will be performed in each chunk*/
v_sql := 'update test_parallel_processing
set col = col*2
where col_id between :start_id and :end_id';

/*running task with any parallel degree, for example, 2*/
DBMS_PARALLEL_EXECUTE.RUN_TASK('mytask', v_sql, DBMS_SQL.NATIVE,
parallel_level => 2);
/*I comment this, cause dropping task will also delete information
about task and chunks from views: USER_PARALLEL_EXECUTE_TASKS
and USER_PARALLEL_EXECUTE_CHUNKS*/
--DBMS_PARALLEL_EXECUTE.drop_task ('mytask');
end;

您可以使用 View 控制处理的状态和结果:

USER_PARALLEL_EXECUTE_TASKS
USER_PARALLEL_EXECUTE_CHUNKS

关于multithreading - Oracle PL/SQL 中有多线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37336698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com