A common attribute among all successful
database-monitoring regimes is a strong automation and alerting
component. A DBA may be able to manually manage and monitor a handful
of instances with moderate success, but as the number of instances
increases, actions become more and more reactive, eventually
culminating in complete chaos.
Automation
delivers a crucial advantage to a DBA. It enables more things to be
achieved with fewer mistakes in a given amount of time, therefore
enabling a DBA to pick and choose the things to spend time on, be it
research, design, or reading the newspaper. Either way, having the
luxury to choose is a valuable asset.
1. Maintenance plans
A
SQL Server maintenance plan allows the creation and scheduling of
maintenance tasks through either a wizard-driven interface or using the
Maintenance Plan design surface.
Right-clicking
Maintenance Plans under Management in SQL Server Management Studio
allows you to select the Maintenance Plan Wizard option. The purpose of
this wizard is to create and schedule tasks covering the major
administration items such as backups, DBCC checks, and index
maintenance. The steps in this wizard enable the selection and
scheduling of common maintenance tasks, as shown in figure 1.
Despite
the ease of use of the Maintenance Plan Wizard, it lacks a certain
level of flexibility, particularly in executing custom T-SQL tasks and
flow control, both of which are catered to in the Maintenance Plan
design surface.
As with the Maintenance
Plan Wizard, the design surface is accessed by right-clicking
Maintenance Plans under the Management node in SQL Server Management
Studio. Selecting New Maintenance Plan prompts you for a maintenance
plan name before opening the design surface, as shown in figure 2.
Let's walk through the major components of a maintenance plan design, beginning with connections.
Connections
As shown in figure 2,
the toolbar on top of the design surface includes a Manage Connections
button. By default, each plan is created with a local server
connection, which each task uses as the default connection. Additional
connections can be created to remote server instances as required, with
each connection defined with either Windows integrated security or a
SQL Server username and password.
Subplans and schedules
Each maintenance plan is defined with one or more subplans,
with each subplan having its own tasks and schedule. For example, we
can have a weekday schedule containing differential backups and index
reorganization with a weekly subplan containing full backups and index
rebuilds.
In our example in figure 2,
we have two subplans, one called Weekday and the other Weekend. The
design surface shown is for the Weekend subplan, which is set torun at
11:00 p.m. every Sunday. The Weekday subplan operates Monday through
Friday at 1:00 a.m. and has its own design surface and task definition.
You can create additional subplans as required by clicking the Add
Subplan button on the toolbar.
Tasks and precedence
For
each subplan, you can click and drag tasks onto the design surface
using the Maintenance Plan Tasks toolbox. Tasks available for selection
include Back Up Database, Rebuild Index, Reorganize Index, and Check
Database Integrity.
Once a task has been
added, you can access and customize its properties by double-clicking
it. For example, the properties of the Back Up Database Task, as shown
in figure 3,
permit the selection of which databases to back up, the backup
location, compression settings, and so forth. One of the nice things
about the task properties window is being able to click the View T-SQL
button to view how the selected options will be implemented by SQL
Server when the OK button is clicked. An alternative to clicking OK is
to save (and optionally customize) the displayed T-SQL script for later
execution.
When multiple tasks are added,
precedence constraints can be added between the tasks to control the
execution flow. For example, in the design surface shown earlier in figure 2, we set the Failure action of the Check Database Integrity Task (DBCC CHECKDB)
to execute the Notify Operator Task, with a successful Integrity check
flowing down the left-hand side to execute the Back Up Database and
Reorganize Index Tasks. You can add and modify precedence constraints
by clicking a task and dragging the green arrow to the appropriate
destination task. Once it's connected, you can change the constraint to
Error, Completion, or Success by right-clicking the green arrow and
selecting the appropriate action.
In
addition to the classic maintenance tasks such as backups, integrity
checks, and index-related maintenance, plans can be highly customized
through the use of tasks such as Execute SQL Server Agent Job and
Execute T-SQL Statement. Further, the Maintenance Cleanup and History
Cleanup Tasks can be added to control the deletion of old backup files,
SQL Server Agent history, and Maintenance Plan reports. These tasks
allow a retention period to be specified, which is particularly useful
in automatically deleting old disk-based backup files.
Despite
the power and flexibility of maintenance plans, some limitations
prevent or restrict their use in certain environments. For example,
despite the existence of both the Rebuild Index and Reorganize Index
Tasks, there's no option to rebuild only if fragmentation exceeds a
certain level. Conditional index
maintenance of this sort is important both from a maintenance duration
perspective and for controlling the size of the transaction log,
particularly when using synchronous database mirroring.
For maximum control over job scheduling and automation, we can use SQL Server Agent jobs.