MSSQL Maintenance
Ola Hallengren Maintenance Solution
Install
Download and run the single install script. This will install default scheduled jobs (but will not schedule them). The default jobs may not be setup as shown below. Review and update as necessary....
DatabaseBackup
DatabaseBackup - SYSTEM_DATABASES - FULL
Run daily
Compression defaults to the setting of 'backup compression default' in sys.configurations
EXECUTE [dbo].[DatabaseBackup]
@Databases = 'SYSTEM_DATABASES',
@Directory = NULL,
@BackupType = 'FULL',
@Verify = 'Y',
@CleanupTime = 48,
@CheckSum = 'Y',
@LogToTable = 'Y'
DatabaseBackup - USER_DATABASES - FULL
If total size of all databases is small then run daily
If total size of all databases is large then consider running weekly (in conjunction with DatabaseBackup - USER_DATABASES - DIFF).
Compression defaults to the setting of 'backup compression default' in sys.configurations
EXECUTE [dbo].[DatabaseBackup]
@Databases = 'USER_DATABASES',
@Directory = NULL,
@BackupType = 'FULL',
@Verify = 'Y',
@CleanupTime = 27,
@CheckSum = 'Y',
@LogToTable = 'Y'
DatabaseBackup - USER_DATABASES - LOG
Schedule hourly (unless there is a good reason to schedule more or less often)
Compression defaults to the setting of 'backup compression default' in sys.configurations
EXECUTE [dbo].[DatabaseBackup]
@Databases = 'USER_DATABASES',
@Directory = NULL,
@BackupType = 'LOG',
@Verify = 'Y',
@CleanupTime = 50,
@CheckSum = 'Y',
@LogToTable = 'Y'
DatabaseIntegrityCheck
DatabaseIntegrityCheck - SYSTEM_DATABASES
Schedule at least weekly
EXECUTE [dbo].[DatabaseIntegrityCheck]
@Databases = 'SYSTEM_DATABASES',
@LogToTable = 'Y'
DatabaseIntegrityCheck - USER_DATABASES
Schedule at least weekly
EXECUTE [dbo].[DatabaseIntegrityCheck]
@Databases = 'USER_DATABASES',
@LogToTable = 'Y'
IndexOptimize
IndexOptimize - USER_DATABASES
Run weekly (or less frequently).
Don’t use defaults. Consider 50% for REORGANIZE, 80% or even 90% for REBUILD (1).
EXECUTE dbo.IndexOptimize
@Databases = 'USER_DATABASES',
@FragmentationLow = NULL,
@FragmentationMedium = 'INDEX_REORGANIZE',
@FragmentationHigh = 'INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationLevel1 = 50,
@FragmentationLevel2 = 80,
@LogToTable = 'Y'
UpdateStats - USER_DATABASES
Run daily
This is a new job (not one of the defaults set up by the maintenance solution install script)
EXECUTE dbo.IndexOptimize
@Databases = 'USER_DATABASES',
@FragmentationLow = NULL,
@FragmentationMedium = NULL,
@FragmentationHigh = NULL,
@UpdateStatistics = 'ALL',
@OnlyModifiedStatistics = 'Y',
@LogToTable = 'Y';
Housekeeping Jobs
CommandLog Cleanup
Output File Cleanup
sp_delete_backuphistory
sp_purge_jobhistory
syspolicy_purge_history
Check
SELECT sysjobs.name job_name,
sysjobs.enabled job_enabled,
sysschedules.name schedule_name,
sysschedules.enabled schedule_enabled,
sysschedules.schedule_id,
sysjobschedules.next_run_date,
sysjobschedules.next_run_time,
"Freq" = CASE sysschedules.freq_type
WHEN '1' THEN 'Once'
WHEN '4' THEN 'Daily'
WHEN '8' THEN 'Weekly'
WHEN '16' THEN 'Monthly'
WHEN '32' THEN 'Monthly (relative to freq_interval)'
WHEN '64' THEN 'At Agent Start'
WHEN '128' THEN 'When Idle'
ELSE 'Unexpected'
END,
sysschedules.freq_interval,
sysschedules.freq_subday_type,
sysschedules.freq_subday_interval,
sysschedules.freq_recurrence_factor,
sysschedules.active_start_time
FROM msdb.dbo.sysjobs
INNER JOIN msdb.dbo.sysjobschedules ON sysjobs.job_id = sysjobschedules.job_id
INNER JOIN msdb.dbo.sysschedules ON sysjobschedules.schedule_id = sysschedules.schedule_id
WHERE sysjobs.name = 'IndexOptimize - USER_DATABASES'
ORDER BY sysjobs.enabled desc
USE [msdb]
SELECT sj.name JobName,
sj.enabled,
sj.start_step_id,
sjs.step_id,
sjs.step_name,
sjs.subsystem,
sjs.command,
CASE on_success_action
WHEN 1 THEN 'Quit with success'
WHEN 2 THEN 'Quit with failure'
WHEN 3 THEN 'Go to next step'
WHEN 4 THEN 'Go to step ' + CAST(on_success_step_id AS VARCHAR(3))
END On_Success,
CASE on_fail_action
WHEN 1 THEN 'Quit with success'
WHEN 2 THEN 'Quit with failure'
WHEN 3 THEN 'Go to next step'
WHEN 4 THEN 'Go to step ' + CAST(on_fail_step_id AS VARCHAR(3))
END On_Failure
FROM dbo.sysjobs sj
INNER JOIN dbo.sysjobsteps sjs ON sj.job_id = sjs.job_id
WHERE sj.name = 'DatabaseIntegrityCheck - SYSTEM_DATABASES'
Bibliography
https://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/ (1) https://www.brentozar.com/archive/2017/12/index-maintenance-madness/(2) https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.htmlhttps://www.brentozar.com/archive/2013/10/how-much-is-offline-during-an-index-rebuild/https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html