Overview
SQL Server Agent schedules jobs (backups, index and statistics maintenance, DBCC CHECKDB, ETL steps), sends alerts and notifications, and records history in msdb. A healthy instance needs a small, well-chosen set of maintenance tasks, not “rebuild everything nightly”.
Core concepts
- Jobs have steps (T-SQL, PowerShell, SSIS, OS command), schedules, operators (who to notify) and alerts (fired by error numbers, severities or performance conditions). History:
msdb.dbo.sysjobs,sysjobhistory. - Availability: Standard and Enterprise (and Developer). Express has no Agent and uses an external scheduler. On Linux containers set
MSSQL_AGENT_ENABLED=true(the test image runs without Agent, so jobs here are not executed). - Database Mail sends notifications. Alert on severity 19-25 errors, error 823/824/825 (I/O corruption), 1205 deadlocks if wanted, failed jobs and low disk space.
- Core maintenance set:
- Full, differential and log backups (sqlserver-backup-recovery).
DBCC CHECKDBat least weekly, or on a restored copy.- Statistics updates (sqlserver-statistics-cardinality), more often on volatile tables.
- Index maintenance only where measured useful: reorganise at moderate fragmentation and rebuild at high fragmentation, skip small indexes.
- Cleanup of
msdbhistory and old backup files.
- The widely used community solution is Ola Hallengren’s Maintenance Solution (
IndexOptimize,DatabaseBackup,DatabaseIntegrityCheck); prefer it to hand-written plans.
Worked example
Creating a job with T-SQL (documentation procedures; not executed here):
USE msdb;
EXEC dbo.sp_add_job @job_name = N'Nightly stats update';
EXEC dbo.sp_add_jobstep @job_name = N'Nightly stats update', @step_name = N'update',
@subsystem = N'TSQL', @database_name = N'kb',
@command = N'EXEC sp_updatestats;';
EXEC dbo.sp_add_schedule @schedule_name = N'daily 02:00', @freq_type = 4, @freq_interval = 1,
@active_start_time = 020000;
EXEC dbo.sp_attach_schedule @job_name = N'Nightly stats update', @schedule_name = N'daily 02:00';
EXEC dbo.sp_add_jobserver @job_name = N'Nightly stats update';Failed jobs in the last day:
SELECT j.name, h.run_date, h.run_time, h.message
FROM msdb.dbo.sysjobhistory h JOIN msdb.dbo.sysjobs j ON j.job_id = h.job_id
WHERE h.run_status = 0 AND h.step_id = 0 AND h.run_date >= CONVERT(int, FORMAT(DATEADD(day,-1,GETDATE()),'yyyyMMdd'));Pitfalls and production notes
- Jobs must be idempotent and safe to rerun; give each step a retry policy and a failure alert.
- Overlapping jobs (backup + index rebuild + CHECKDB) compete for I/O; stagger them.
- Agent jobs live in
msdband are not part of an AG failover; script them to every replica (sqlserver-always-on). - Own the job as a dedicated principal, not a personal login that leaves the company.
- Do not shrink files on a schedule.
- External orchestration (Airflow, Kubernetes CronJobs) can call
sqlcmdinstead of using Agent on containers.
Practice questions
- Which maintenance task would you run on a restored copy and why?
- When is rebuilding an index pointless?
- How do you ensure Agent jobs exist on the new primary after a failover?
References
- Microsoft Learn, SQL Server Agent
- Ola Hallengren, SQL Server Maintenance Solution: https://ola.hallengren.com
Related
sqlserver-backup-recovery · sqlserver-monitoring-dmvs · sqlserver-always-on