141 Chapter 11: History Cleanup Task The History Cleanup task is very straightforward. It simply removes old data from the msdb database, and that's it. Over time, as backup and restore jobs run, as SQL Server Agents jobs run, and as Maintenance Plans run, historical data about each of these jobs is stored in tables of the msdb database. In the short term, data stored in msdb can be useful. For example, if you are having problems with SQL Server Agent jobs, or Maintenance Plans, then this past msdb data can be used to help troubleshoot what went wrong. Also, SSMS uses the data stored in msdb about backup jobs to make it easier for you to use SSMS to restore databases or log files. Of course, you can also restore backups and logs using T-SQL commands that don't require this data. Ultimately, however, this data has a limited life span, and once it gets old, there is no point in keeping it around. An Overview of the History Cleanup Task The msdb database is often referred to as the "SQL Agent database," since SQL Server Agent uses it to store all sorts of information about the jobs it runs. Unfortunately, SQL Server doesn't do any kind of its own internal clean up of this data so, over time, the msdb database can grow, and grow, and grow. It can even lead to some minor performance problems. As such, the DBA is responsible for cleaning up old records from msdb that are no longer of any value, and the History Cleanup task is designed to make that job easy. When the History Cleanup task runs using its default settings, it executes the following T-SQL code: declare @dt datetime select @dt = cast(N'2009-07-22T14:19:13' as datetime) EXEC msdb.dbo.sp_delete_backuphistory @dt GO EXEC msdb.dbo.sp_purge_jobhistory @oldest_date='2009-07- 22T14:19:13' GO EXECUTE msdb sp_maintplan_delete_log null,null,'2009-07- 22T14:19:13' Chapter 11: History Cleanup Task 142 As you can see, this task runs three different system-stored procedures, one each to clean up historical data for backups, jobs, and maintenance plans. Each of the three stored procedures has the same value for the oldest_date parameter, which, by default, is set to a date four weeks older than the date the task is run. In other words, it cleans out any msdb data that is more than four weeks old. When a History Cleanup task runs, it produces a text report similar to the following: Microsoft(R) Server Maintenance Utility (Unicode) Version 10.0.2531 Report was generated on "HAWAII." Maintenance Plan: User Databases Maintenance Plan Duration: 00:00:02 Status: Succeeded. Details: Clean Up History (HAWAII) Cleanup history on Local server connection History type: Backup,Job,Maintenance Plan Age: Older than 4 Weeks Task start: 2009-08-19T14:26:20. Task end: 2009-08-19T14:26:22. Success Command:declare @dt datetime select @dt = cast(N''2009-07- 22T14:26:20'' as datetime) exec msdb.dbo.sp_delete_backuphistory @dt GO EXEC msdb.dbo.sp_purge_jobhistory @oldest_date=''2009-07- 22T14:26:20'' GO EXECUTE msdb sp_maintplan_delete_log null,null,''2009-07- 22T14:26:20'' GO Besides the code that runs, one of the key things to look for in the text report is the Age, which indicates how many weeks' worth of data is being kept in msdb. If you want to change the default four-week value, you can, as we will shortly discuss. Chapter 11: History Cleanup Task 143 When and How Often to Clean Up MSDB This task uses very few resources, so it can be run any time you want, even during busy times of the day. I generally run it weekly, along with my other weekly jobs, although running it more often, or less often, won't really make much difference. Schedule it to run at your convenience. Configuring the History Cleanup Task The first thing to notice about the Define History Cleanup Task screen, shown in Figure 11.1, is that you don't have to select a database this time, because only the msdb database is affected by this task. Selecting the Historical Data to Delete Under Select the historical data to delete you have three options, which are all selected by default: • Backup and restore history • SQL Server Agent job history • Maintenance plan history. I don't know of any good reason not to regularly delete each of these types of historical data, so I recommend keeping all three options selected. Next, you just need to specify an age beyond which historical data will be removed by this task. The default is to remove data that is more than four weeks old, and this is suitable for most systems. If you want to keep the data around longer, that's fine, but I wouldn't keep it more than three months, as the older data doesn't serve any practical purpose. Chapter 11: History Cleanup Task 144 Creating the Job Schedule The last step is to set the schedule. Setting this job schedule for this task is identical to all the rest of the tasks. Because this job is lightweight, and because there is no point in running it daily, I generally schedule this job to run once a week, along with my other weekly jobs. While the order of when this job runs is not really important, I have traditionally run it near the end of my weekly jobs, along with the Maintenance Cleanup task, which performs a similar function. The Maintenance Cleanup task will be covered in Chapter 15. Figure 11.1: Completing the Define History Cleanup Task screen is fast and easy. Chapter 11: History Cleanup Task 145 Summary While the History Cleanup task might seem trivial in the larger scope of database maintenance, it still is important. For example, if you have a busy server, with lots of jobs running on it every day, the msdb database grows in size due to all the historical data it is storing, often leading to performance problems when SSMS accesses it. While this won't prevent you from doing your work, it can slow it down. And since most of the historical data is of no value, it might as well be cleaned out, helping msdb and SSMS to run more eciently. In the next three chapters, we learn about the three backup-related tasks available from the Maintenance Plan Wizard. . Historical Data to Delete Under Select the historical data to delete you have three options, which are all selected by default: • Backup and restore history • SQL Server Agent job history • Maintenance. around. An Overview of the History Cleanup Task The msdb database is often referred to as the " ;SQL Agent database," since SQL Server Agent uses it to store all sorts of information. with SQL Server Agent jobs, or Maintenance Plans, then this past msdb data can be used to help troubleshoot what went wrong. Also, SSMS uses the data stored in msdb about backup jobs to make