We've got some logging in MSDB sysJobStepsLogs
I suspect at some point we found that SQL Agent Jobs logging was set to some very small (default) number, and we had a job running at short interval - every few minutes - and we were losing job logging history too soon to be any use to us when retrospectively investigating a job.
Spent the day tidying up a run-away MSDB database. There's the old culprits of Logging of Backups and clearing that out using sp_delete_backupHistory (the issue is that there are NO indexes which means that clearing the Backup Log tables is catastrophically slow; that is fixed in SQL2014 - that's 7 years after people started asking MS for a solution - DBAs all around the world wasting time solving a problem that MS should have fixed promptly)
Then I found that sp_purge_jobHistory
wasn't scheduled either. At least that one runs reasonably quickly. But for both of them I had to write a loop that only cleared one month's data at a time (for us that's about 25,000 rows per loop iteration, so not exactly trivial)
NOTE: Don't attempt to clear one month's log backup history from MSDB if you have a large number of databases and/or frequent log backups - it will take days. Make sure you have found suitable indexes to create first. I don't recommend you try, for example, clearing one day's worth of the backlog at a time - it will lock up the tables and interfere with backups; its a PoS ... just create the recommended indexes (which turn out to be the same ones, bar one, that MS have added in SQL 2014)
And then I spotted that sysJobStepsLogs
wasn't reducing in size. I had assumed that sp_purge_jobHistory
would take care of that too ...
So how to purge that? We only have three rows in that table ... but the space used by the table is 4GB !! I haven't bothered to look into it closely but I think SQL Agent is using the nvarchar(MAX) LOG column as a circular buffer - which strikes me as a strange approach compared to a row-per-log-entry table, which would be easy to purge.
So I'm not sure that there is any benefit in purging this "gently" because each loop will, I am presuming?, reduce the circular buffer size but, in effect, write back the same row. So it will have to handle that row again, each iteration a bit smaller, but given how massive the BLOB is that's going to be a lot of logging each time.
sp_delete_jobStepLog
also requires a Job ID parameter - rather than the Log ID or the GUID for the Job Step (both of which are available in the sysJobStepsLogs
table). Whilst its not an unreasonable requirement it does require writing a more complicated loop to figure out which jobs actually have anything in the sysJobStepsLogs
table, which sysJobSteps
they refer to and, from there, which sysJobs
job_id
that refers to.
A Google didn't turn up much about sysJobStepsLogs
/ sp_delete_jobStepLog
- perhaps people aren't daft enough to increase the logging retention time/amount like me?
Or maybe I've got the wrong end of the stick and there are easier ways to manage this housekeeping?
I can't help thinking its high time that MS tidied up all this crud. Job Dates being stored as separate yyyymmdd
and hhmmss
INTERGERS rather than DateTime datatype is way past its bedtime ... along with the Micky-Mouse coding that is to be found in so many of the System SProcs
Grrrrr ...