Sql-server – How to limit SQL Server Agent’s simultaneous jobs number

queuesql server

I'm looking for a setting (other than schedules shifting) which could limit the number of jobs running simultaneously.
I have 844 jobs in my SQL Agent (SQL Server 2017 Enterprise). Most of them needs few minutes to complete, there are about 190 jobs that needs half of hour. All of them are scheduled to run every hour.
If they run all together, I can't fit total duration time below 60 minutes.
Is there any setting that could queue 744 jobs until one from those 844 finishes and SQL Agent would start the next job waiting in the queue?

Best Answer

This is best done outside of SQL Agent. Honestly, at 800+ jobs running every hour you should have moved to a different method long before now.

My preference for this would be to create a single master SSIS package that reads in a list of jobs that need to be run and then starts up the first X in the list, then sleeps until the number of running jobs is below threshold, then starts up the next X in the list. You could randomize the list or sort it, as you choose. I've done something similar to this in my environment where I needed to make sure that certain classes of packages (client imports) were done one at a time.

The less dynamic approach would be to just break the list of jobs that need to be run into however many simultaneous you want running. Each of those would have a list of steps that call the actual job that needs running. It takes less fiddling about with timing, sleeps and querying active jobs but is less dynamic (for example, changing the number of max simultaneous threads will take work.

But in any case, I would leave the jobs as they are now, just remove/disable their schedules. Then whatever solution you want can just call the sp_start_job procedure. This lets you still easily run any job you want out of cycle and you see independent reports of success/failure.