如何防止 Sql Server 作业同时运行

我的 SQL 代理中有一些预定的作业:

I have some scheduled jobs in my SQL Agent:

  • Job1,每 2 分钟执行一次
  • Job2,每 10 分钟执行一次
  • Job3,每 15 分钟执行一次

如您所见,多个作业可以同时运行.当这些作业同时运行时,会导致 CPU 使用率达到 100%.

As you can see, multiple jobs can run simultaneously. When these jobs do run simultaneously, it is causing the CPU usage to go to 100%.

有解决办法吗?有没有办法控制并发运行的作业数量?注意:我需要这些作业在适当的时间内大约运行.

Is there a solution? Is there a way to control the number of jobs running concurrently? Note: I need these jobs to run approximately in their appropriate period.

推荐答案

通过 sp_getapplock

您要求用户控制并发,这通常是最好的方法.

You're asking for user-controlled concurrency and this is usually the best way.

这允许您在锁已被另一个作业持有时等待或中止.我们在一两个地方使用它来阻止多个用户强迫同一任务重叠运行.效果很好.

This allows you to wait or abort if the lock is already held by another job. We use it in one or 2 places to stop multiple users forcing the same task to run overlapping. It works well.

相关文章