Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Prevent Sql Server Jobs to Run simultaneously

I have some scheduled jobs in my SQL Agent:

  • Job1, executing every 2 minutes
  • Job2, executing every 10 minutes
  • Job3, executing every 15 minutes

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.

like image 511
ehsan Avatar asked Oct 19 '10 12:10

ehsan


People also ask

How do I stop a running job in SQL Server?

Expand SQL Server Agent, expand Jobs, right-click the job you want to stop, and then click Stop Job. If you want to stop multiple jobs, right-click Job Activity Monitor, and then click View Job Activity. In the Job Activity Monitor, select the jobs you want to stop, right-click your selection, and then click Stop Jobs.

Will a SQL Server Job skip a scheduled run if it is already running?

The SQL Server agent checks whether the job is already running before starting a new iteration. If you have long running job and its schedule comes up, it would be skipped until the next interval. You can try this for yourself. If you try to start a job that's already running, you will get an error to that effect.

How do I disable jobs in SSMS?

Using SQL Server Management Studio Expand SQL Server Agent. Expand Jobs, and then right-click the job that you want to disable or enable. To disable a job, click Disable. To enable a job, click Enable.

What are batch jobs in SQL?

Technically speaking, most SQL Server Agent Jobs (but not all) are batch jobs in the sense that they're a wrapped batch of commands or operations sent to be executed at various times against the server.


1 Answers

Use a session lock via 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.

like image 143
gbn Avatar answered Oct 13 '22 02:10

gbn