SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Spreading Job Run Times Out on an MSX/TSX Scheduled Job

Problem

My company heavily uses MSX/TSX jobs for everything.  But our SAN doesn’t necessarily like if we run all our index maintenance, backups, and integrity checks at the same time.  So here is a quick little post on how you can schedule the workload to spread out across all your servers across multiple time blocks and still have only one job.

Solution

First, you need to decide how many time blocks or hours you want the jobs to run in.  So let’s start with scenario one where you pick for example four time blocks.  First, you declare a variable with the time block in it and we will feed in the @@SERVERNAME to let determine a value for the time block that server will run.  Then we wrap our code around our time block, our example we will run Index Maintenance for a 12 period spread out for three hours.  Mind you for my index process which I probably should blog about as well I am processing one index at a time have something that BREAKs out of the procedure when it exceeds the time block it is.  So below we run Index Maintenace between start the index maintenance job on a server between the hours 6  PM and 5 AM based on the time block value we got back.

DECLARE @TimeBlock INT = ABS(CHECKSUM(@@SERVERNAME))%4+1 --Returns a value between 1 and 4 based on @@SERVERNAME

IF (@TimeBlock = 1 AND (CONVERT(INT,(DATEPART(HOUR,GETDATE()))) >= 21 OR CONVERT(INT,(DATEPART(HOUR,GETDATE()))) < 18)) OR (@TimeBlock = 2 AND CONVERT(INT,(DATEPART(HOUR,GETDATE()))) >= 0)
	OR (@TimeBlock = 3 AND CONVERT(INT,(DATEPART(HOUR,GETDATE()))) >= 3)
	OR (@TimeBlock = 4 AND CONVERT(INT,(DATEPART(HOUR,GETDATE()))) >= 6)
BEGIN
--Run code for index maintenace
END

Using the same concept we could spread out when out backups start on our servers at night by using the following code:

DECLARE @Hour INT = ABS(CHECKSUM(@@SERVERNAME))%5 --Returns a value between 1 and 4 based on @@SERVERNAME

IF @Hour = CONVERT(INT,(DATEPART(HOUR,GETDATE())))
BEGIN
--Run backups
END

Next Steps

If you are using MSX/TSX you can now consolidate your jobs, if you aren’t using MSX/TSX explore it, it does make managing your jobs easier.  This can use with Ola Hallengren’s scripts or MinionWare scripts or any other scripts you need to run for maintenance across all your servers during a certain window of time without saturating you SAN.

Database Superhero’s Blog

Tracy is the Database Superhero for ChannelAdvisor. She has spent over 20 years in IT and has used SQL Server since 1999. Tracy covers all aspects of administration and deals heavily with performance tuning and high availability and disaster recovery. Tracy is a co-organizer of a Special Interest Group (SIG) dedicated to advanced DBA topics in our local user group TriPass. She is also the founder of http://WeSpeakLinux.com. Before she worked full-time as a DBA she was formally a developer and network administrator. She also tinkered with databases in middle/high school to keep her sports card collection organized.

Comments

Leave a comment on the original post [tracyboggiano.com, opens in a new window]

Loading comments...