11

我们有一个每晚运行的存储过程,它依次启动许多其他过程。其中一些程序在逻辑上可以与其他一些程序并行运行。

  • 我如何向 SQL Server 指示一个过程应该并行运行还是串行运行——即:异步启动还是阻塞启动?
  • 并行运行它们会有什么影响,请记住,我已经确定这些进程不会竞争表访问或锁定 - 只是总磁盘 io 和内存。在大多数情况下,他们甚至不使用相同的表。
  • 如果其中一些程序是相同的程序,只是参数不同,这有关系吗?
  • 如果我异步启动一对或过程,SQL Server 中是否有一个好的系统来等待它们完成,或者我是否需要让它们中的每一个在某处设置一个标志并使用定期检查和轮询标志WAITFOR DELAY

目前我们仍在使用 SQL Server 2000。

附带说明一下,这很重要,因为主程序是响应从大型机系统到服务器的数据转储完成而启动的。大型机转储每晚大约需要 2 个小时,我们无法控制它。因此,我们一直在努力寻找减少处理时间的方法。

4

4 回答 4

14

I had to research this recently, so found this old question that was begging for a more complete answer. Just to be totally explicit: TSQL does not (by itself) have the ability to launch other TSQL operations asynchronously.

That doesn't mean you don't still have a lot of options (some of them mentioned in other answers):

  • Custom application: Write a simple custom app in the language of your choice, using asynchronous methods. Call a SQL stored proc on each application thread.
  • SQL Agent jobs: Create multiple SQL jobs, and start them asynchronously from your proc using sp_start_job. You can check to see if they have finished yet using the undocumented function xp_sqlagent_enum_jobs as described in this excellent article by Gregory A. Larsen. (Or have the jobs themselves update your own JOB_PROGRESS table as Chris suggests.) You would literally have to create separate job for each parallel process you anticipate running, even if they are running the same stored proc with different parameters.
  • OLE Automation: Use sp_oacreate and sp_oamethod to launch a new process calling the other stored proc as described in this article, also by Gregory A. Larsen.
  • DTS Package: Create a DTS or SSIS package with a simple branching task flow. DTS will launch tasks in individual spids.
  • Service Broker: If you are on SQL2005+, look into using Service Broker
  • CLR Parallel Execution: Use the CLR commands Parallel_AddSql and Parallel_Execute as described in this article by Alan Kaplan (SQL2005+ only).
  • Scheduled Windows Tasks: Listed for completeness, but I'm not a fan of this option.

I don't have much experience with Service Broker or CLR, so I can't comment on those options. If it were me, I'd probably use multiple Jobs in simpler scenarios, and a DTS/SSIS package in more complex scenarios.

One final comment: SQL already attempts to parallelize individual operations whenever it can*. This means that running 2 tasks at the same time instead of after each other is no guarantee that it will finish sooner. Test carefully to see whether it actually improves anything or not.

We had a developer that created a DTS package to run 8 tasks at the same time. Unfortunately, it was only a 4-CPU server :)

*Assuming default settings. This can be modified by altering the server's Maximum Degree of Parallelism or Affinity Mask, or by using the MAXDOP query hint.

于 2011-01-10T15:41:13.097 回答
3

创建几个 SQL Server 代理作业,每个作业都运行一个特定的 proc。

然后从你的主进程中开始工作。

我能想到的唯一等待方法是,如果您有一个状态表,每个 proc 完成时都会更新它。

然后另一个工作可以轮询该表以完全完成并启动最终过程。或者,您可以在此表上设置触发器。

内存影响完全取决于您的环境..

更新: 如果您可以访问任务系统..那么您可以采用相同的方法。只需让 windows 执行多个任务,每个任务负责一个 proc。然后在所有任务完成后使用状态表上的触发器来启动某些操作。

UPDATE2: 此外,如果您愿意创建一个新应用程序,您可以将所有逻辑包含在一个 exe 中......

于 2008-12-08T16:06:37.443 回答
2

You do need to move your overnight sprocs to jobs. SQL Server job control will let you do all of the scheduling you are asking for.

于 2008-12-08T18:45:08.780 回答
2

You might want to look into using DTS (which can be run from the SQL Agent as a job). It will allow you pretty fine control over which stored procedures need to wait for others to finish and what can run in parallel. You can also run the DTS package as an EXE from your own scheduling software if needed.

NOTE: You will need to create multiple copies of your connection objects to allow calls to run in parallel. Two calls using the same connection object will still block each other even if you don't explicitly put in a dependency.

于 2008-12-08T19:24:27.607 回答