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

Tracy Boggiano, 2018-06-11 (first published: 2018-05-29)

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.

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads