oracle 我们可以在 PL/SQL 中使用线程吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/576802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can We use threading in PL/SQL?
提问by Burhan
Is there any feature of asynchronous calling in PL/SQL? Suppose I am in a block of code would like to call a procedure multiple times and wouldn't bother when and what the procedure returns?
PL/SQL 中有异步调用的特性吗?假设我在一个代码块中想要多次调用一个过程并且不会打扰该过程何时返回以及返回什么?
BEGIN
myProc(1,100);
myProc(101,200);
myProc(201,300);
...
...
END;
In the above case, I don't want my code to wait for myProc(1,100) to finish processing before executing(101,200)
Thanks.
在上述情况下,我不希望我的代码在执行 (101,200) 之前等待 myProc(1,100) 完成处理,
谢谢。
采纳答案by David Aldridge
+1 for DBMS_SCHEDULER and DBMS_JOB approaches, but also consider whether you ought to be using a different approach.
+1 DBMS_SCHEDULER 和 DBMS_JOB 方法,但还要考虑您是否应该使用不同的方法。
If you have a procedure which executes in a row-by-row manner and you find that it is slow, the answer is probably not to run the procedure multiple times simltaneously but to ensure that a set-based aproach is used instead. At an extreme you can even then use parallel query and parallel DML to reduce the wall clock time of the process.
如果您有一个以逐行方式执行的过程并且您发现它很慢,那么答案可能不是同时多次运行该过程,而是确保使用基于集合的方法。在极端情况下,您甚至可以使用并行查询和并行 DML 来减少进程的挂钟时间。
I mention this only because it is a very common fault.
我提到这一点只是因为这是一个非常常见的故障。
回答by darreljnz
Submit it in a DBMS_JOB like so:
像这样在 DBMS_JOB 中提交它:
declare
ln_dummy number;
begin
DBMS_JOB.SUBMIT(ln_dummy, 'begin myProc(1,100); end;');
DBMS_JOB.SUBMIT(ln_dummy, 'begin myProc(101,200); end;');
DBMS_JOB.SUBMIT(ln_dummy, 'begin myProc(201,300); end;');
COMMIT;
end;
You'll need the job_queue_processes parameter set to >0 to spawn threads to process the jobs. You can query the jobs by examining the view user_jobs.
您需要将 job_queue_processes 参数设置为 >0 以生成线程来处理作业。您可以通过检查视图 user_jobs 来查询作业。
Note that this applies to Oracle 9i, not sure what support 10g has. See more info here.
请注意,这适用于 Oracle 9i,不确定 10g 有什么支持。在此处查看更多信息。
EDIT: Added missed COMMIT
编辑:添加了错过的提交
回答by Matthew Farwell
You may want to look into DBMS_SCHEDULER.
您可能需要查看 DBMS_SCHEDULER。
Edited for completeness:
为完整性编辑:
DMBS_SCHEDULER is available on Oracle 10g. For versions before this, DBMS_JOB does approximately the same job.
DMBS_SCHEDULER 在 Oracle 10g 上可用。对于此之前的版本,DBMS_JOB 执行大致相同的工作。
For more information, see: http://download.oracle.com/docs/cd/B12037_01/server.101/b10739/jobtosched.htm
有关更多信息,请参见:http: //download.oracle.com/docs/cd/B12037_01/server.101/b10739/jobtosched.htm
回答by frohiky
For PL/SQL Parallel processing you have the following options:
对于 PL/SQL 并行处理,您有以下选项:
These will let you "emulate" forking and threading in PL/SQL. Of course, using these, you may realize the need to communicate between parallel executed procedures. To do so check out:
这些将让您在 PL/SQL 中“模拟”分叉和线程。当然,使用这些,您可能会意识到需要在并行执行的程序之间进行通信。为此,请查看:
Personally I've implemented a parallel processing system using DBMS_Scheduler, and used DBMS_Pipe to communicate between "threads". I was very happy with the combination of the two, and my main goal (to reduce major processing times with a particular heavy-weight procedure) was achieved!
我个人已经使用 DBMS_Scheduler 实现了一个并行处理系统,并使用 DBMS_Pipe 在“线程”之间进行通信。我对两者的结合感到非常满意,我的主要目标(通过特定的重量级程序减少主要处理时间)实现了!
回答by Dwayne King
You do have another option starting in 11g. Oracle has introduced a package that does something similar to what you want to do, named DBMS_PARALLEL_EXECUTE
从 11g 开始,您确实有另一个选择。Oracle 引入了一个包,它的作用类似于您想做的事情,名为 DBMS_PARALLEL_EXECUTE
According to them, "The DBMS_PARALLEL_EXECUTE package enables the user to incrementally update table data in parallel". A fairly good summary of how to use it is here
据他们介绍,“DBMS_PARALLEL_EXECUTE 包使用户能够并行地增量更新表数据”。关于如何使用它的相当好的总结是here
Basically, you define a way that Oracle should use to break your job up into pieces (in your case by you seem to be passing some key value), and then it will start each of the pieces individually. There is certainly a little planning and a little extra coding involved in order to use it, but nothing that you shouldn't have been doing anyways.
基本上,您定义了一种 Oracle 应该用来将您的工作分解成多个部分的方式(在您的情况下,您似乎正在传递一些关键值),然后它将单独启动每个部分。为了使用它当然需要一些计划和一些额外的编码,但无论如何你都不应该做任何事情。
The advantage of using a sanctioned method such as this is that Oracle even provides database views that can be used to monitor each of the independent threads.
使用这种认可方法的优势在于,Oracle 甚至提供了可用于监视每个独立线程的数据库视图。
回答by tuinstoel
Another way of doing parallel (multi-threaded) PL/SQL is shown here:
另一种执行并行(多线程)PL/SQL 的方法如下所示:
http://www.williamrobertson.net/documents/parallel-plsql-launcher.html
http://www.williamrobertson.net/documents/parallel-plsql-launcher.html
The disadvantage of using dbms_job or dbms_schedular is that you don't really know when your tasks are finished. I read that you don't bother but maybe you will change your mind in the future.
使用 dbms_job 或 dbms_schedular 的缺点是您并不真正知道您的任务何时完成。我读到你不会打扰,但也许你将来会改变主意。
EDIT:
编辑:
This article http://www.devx.com/dbzone/10MinuteSolution/20902/0/page/1describes another way. It uses dbms_job
and dbms_alert
. The alerts are used to signal that the jobs are done (callback signal).
这篇文章http://www.devx.com/dbzone/10MinuteSolution/20902/0/page/1描述了另一种方式。它使用dbms_job
和dbms_alert
。警报用于表示作业已完成(回调信号)。
回答by Ranzo Taylor
The parallel pipelined approach listed here by askTom provides a more complex approach, but you will actually pause until the work is complete, unlike the DBMS Job techniques. That said, you didask for the "asynchronous" technique, and DBMS_JOB is perfect for that.
askTom 此处列出的并行流水线方法提供了一种更复杂的方法,但与 DBMS 作业技术不同,您实际上将暂停直到工作完成。也就是说,您确实要求使用“异步”技术,而 DBMS_JOB 非常适合。
回答by tuinstoel
Here an explanation of different ways of unloading data to a flat file. One of the ways shows how you can do parallel execution with PL/SQL to speed things up.
这里解释了将数据卸载到平面文件的不同方法。其中一种方法展示了如何使用 PL/SQL 进行并行执行以加快速度。
回答by tuinstoel
Have you considered using Oracle Advaned Queuing?
您是否考虑过使用 Oracle 高级队列?