Planning database maintenance Database maintenance plans include the following routine tasks: ■ Checking database integrity ■ Performing index maintenance ■ Updating database statistics
Trang 1■ DBCC dllname FREE): Unloads the specified dll from memory
■ DBCC MEMORYSTATUS: Provides a snapshot of the current memory status of SQL Server
This command is very useful in troubleshooting issues that relate to SQL Server memory consumption
■ DBCC USEROPTIONS: Returns the activeSEToptions for the current connections The following three DBCC commands are used with trace flags Trace flags are used temporarily to set
certain specific SQL Server characteristics or turn off some behavior For example, trace flag 1204 is
used to enabled deadlock reporting to SQL Server ErrorLog
■ DBCC TRACEON: Turns on the specified trace flags
■ DBCC TRACEOFF: Turns off the specified trace flags
■ DBCC TRACESTATUS: Displays the active trace flags
Managing Database Maintenance
SQL Server provides a host of database maintenance commands Fortunately, it also provides the DBA
with ways to schedule maintenance tasks
Planning database maintenance
Database maintenance plans include the following routine tasks:
■ Checking database integrity
■ Performing index maintenance
■ Updating database statistics
■ Performing database backups
■ Shrinking the database
■ Checking file sizes and free disk space These maintenance tasks can be automated and scheduled using the SQL Server Agent service The
maintenance tasks are referred to as SQL Server Agent jobs
Maintenance plan
There are two ways to create a maintenance plan:
■ Maintenance Plan Wizard: Used to quickly create a basic maintenance plan, this wizard enables you to choose typical predefined maintenance tasks such as performing database backup, rebuilding indexes, updating statistics, checking data integrity, and shrinking the database It does not allow you to add any custom tasks
■ Maintenance Plan Design Surface: Used for designing maintenance plans with more flexi-bility, this option enables you to create a workflow of typical maintenance tasks and custom maintenance tasks using T-SQL scripts It also allows extended logging, which can be very useful for troubleshooting purposes
All of the maintenance plans appear in theManagement\Maintenance Plansfolder of SQL Server
Management Studio To launch the Maintenance Plan Wizard, right-click theMaintenance Plans
1002
www.getcoolebook.com
Trang 2folder and choose Maintenance Plan Wizard Follow the simple step-by-step wizard to create the
maintenance plan
To launch the Maintenance Plan Design Surface, right-click theMaintenance Plansfolder and
choose New Maintenance Plan from the context menu Type a name for the maintenance plan in the
New Maintenance Plan dialog and click OK
Once you assign a name to your maintenance plan, Management Studio opens a new window that
includes the maintenance plan name, a description, the time you want to schedule the maintenance
plan, and a list of tasks to perform Figure 42-1 shows a sample maintenance plan with some tasks
already entered
Adding a task
A maintenance plan must contain at least one task Fortunately, it isn’t difficult to add one Simply drag
and drop the appropriate task from the Toolbox, shown in Figure 42-2, to the design area of the
main-tenance plan The resulting task will appear as a square (refer to Figure 42-1)
FIGURE 42-1
Create a maintenance plan by adding some tasks and scheduling a time to perform the tasks
Trang 3FIGURE 42-2
Use the Toolbox to add new tasks to your maintenance plan
FIGURE 42-3
Define the task specifics using the Properties window
1004
www.getcoolebook.com
Trang 4The task isn’t configured yet Use the Properties window, shown in Figure 42-3, to configure the task.
The content of the Properties window varies by task For example, when you select Back Up Database
Task, you’ll see properties for choosing the kind of database, the backup device type, and other
infor-mation associated with backing up the database The upper portion of the Properties window contains
the name of the selected task You can choose other tasks using the drop-down list box The middle of
the Properties window contains a list of properties for the selected task You can organize the
proper-ties by category or in alphabetical order The lower part of the Properproper-ties window contains a description
for the selected property Once you have worked with tasks for a while, the description usually provides
enough information to help you remember how to use the selected property
Sometimes you may not want to use the Properties window For example, you might be working with a
new task In this case, you can bypass the Properties window by double-clicking the task A task-specific
window like the one shown in Figure 42-4 for a backup task will appear Unlike the Properties window,
this dialog displays the task properties in context In addition, it grays out task elements until you define
the correct functionality For example, when you choose to back up specific databases, the dialog won’t
let you perform any other configuration task until you choose one or more databases for the backup
FIGURE 42-4
In addition to the Properties window, you can use the task-related dialog for configuration
Trang 5It’s interesting to click View T-SQL when you finish configuring the task The resulting window, like the
one shown in Figure 42-5, displays the T-SQL that the task will use The Transact SQL (Task
Gener-ated) window won’t let you edit the command, but seeing how the Maintenance Plan Wizard generates
the code can prove helpful when you need to create T-SQL commands of your own
FIGURE 42-5
The View T-SQL button enables you to see the Transact SQL command
You know when a task is complete by the appearance of the square in the design area Incomplete tasks,
those that won’t execute correctly, display a red circle with an X through it on the right side of the
window
After you define a task, add the next task in your list You can add the tasks in any order and place
them in any order onscreen Connect the tasks in the order in which you want SQL Server to
exe-cute them Figure 42-1 showed two tasks, with one task connected to the next task in the list In
that example, first the integrity of theAdventureWorks2008database is checked, and then it is
backed up
Defining the schedule
You can perform tasks on demand or schedule them to run automatically Generally, it’s a good idea to
set standard tasks such as backup to run automatically Click the ellipses next to the Schedule field to
display the Job Schedule Properties dialog, shown in Figure 42-6, when you want to change the task
schedule
Select the scheduling requirements for the task For example, you can set a task to run daily, weekly, or
monthly The dialog also enables you to select a specific starting time for the task and determine when
the scheduling begins and ends
The one form of scheduling that this dialog doesn’t provide is on demand To set the maintenance task
to run on demand, simply leave the Job Schedule Properties empty
1006
www.getcoolebook.com
Trang 6FIGURE 42-6
Define a schedule that meets the task requirements; use off-hours scheduling for resource-intensive
tasks
Creating new connections
Depending on your database setup, you might need to create multiple connections to perform a
particular task The maintenance plan always provides a connection to the default instance of the local
database Click Manage Connections to add more connections to the current maintenance plan (any
additions won’t affect other maintenance plans you create) The Manage Connections dialog shown in
Figure 42-7 will appear
Click Add in the Manage Connections dialog to add a new connection The New Connection dialog will
appear A connection consists of three elements: connection name, server name, and server security
Choose a connection name that reflects the server and instance Using a name such as ‘‘My Connection’’
won’t be particularly helpful when you need to troubleshoot the maintenance task later You can either
type the server and instance name or select it from a list that the maintenance plan provides when you
click the ellipses next to the Select or Enter a Server Name field Finally, choose between Windows
integrated or SQL Server security Click OK to add the new connection to the list
Trang 7FIGURE 42-7
Add new connections as needed to perform maintenance tasks
The Manage Connections dialog also enables you to edit existing connections or remove old connections
When you edit an existing connection, you see a New Connection dialog in which you can change the
logon arguments or the server name for the connection The dialog grays out the other fields Simply
click Remove to delete a connection you no longer need from the list
Connection deletion is a one-way process, and it happens quite quickly Make sure you have the correct connection selected before you click Remove because the maintenance plan won’t ask for confirmation before deleting the connection.
Logging the maintenance progress
Many of the maintenance tasks that you automate will execute during off hours, when you’re unlikely
to be around to monitor the system Fortunately, you can set maintenance tasks to log and report their
actions so that you don’t need to watch them every moment To use this feature, click the Reporting
and Logging icon next to the Manage Connections icon The Reporting and Logging dialog shown in
Figure 42-8 will appear
The Reporting and Logging dialog includes an option that enables you to send the report to a file or
your e-mail The e-mail feature is quite handy and saves you the time of searching for the file on the
hard drive Conversely, the text file provides an archive that could prove helpful long after you’ve
deleted the e-mail from your inbox Once you configure the reporting options, click OK, and the
maintenance plan will record its actions for you
Best Practice
You normally want to provide extended information about all maintenance tasks that the system performs
without your supervision Even the smallest piece of information can help you determine where a
particular maintenance action went awry
1008
www.getcoolebook.com
Trang 8FIGURE 42-8
Use the Reporting and Logging dialog to monitor your maintenance tasks
Command-line maintenance
Database maintenance is normally performed within SQL Server Management Studio or automated with
SQL Server Agent, but maintenance can be performed from the DOS command prompt by means of the
SQLMaintutility This utility has numerous options that can perform backups, update statistics, and
runDBCC
This feature will be removed in the next version of SQL Server Do not use this feature in
new development work, and plan to change applications that use this feature To run SQL
Server maintenance plans from the command line, use the dtexec utility.
Specific information onSQLMaintanddtexeccan be found in SQL Server 2008 Books Online
Monitoring database maintenance
It’s not enough to simply schedule tasks; they must be monitored as well In larger installations with
dozens of SQL Servers spread around the globe, monitoring the health of SQL Server and the databases
Trang 9is itself a full-time job Table 42-1 provides a sample DBA daily checklist that can be used as a starting
point for developing a database monitoring plan
TABLE 42-1
DBA Daily Checklist
System Databases Backup
Production User Databases Backup
SQL Agent, SQL Maint, & DTC Running
Database Size, Growth, Disk Free Space
Batch Jobs Execute OK
DBCC Jobs Execute OK
SQL Log Errors
Replication Log Agent Running
Replication Distribution Cleanup Job Execute OK
SQL Server Last Reboot
Depending on the number of servers and the complexity, the DBA daily checklist can be maintained
manually with an Excel spreadsheet or tracked in a SQL Server table
Summary
This chapter covered database maintenance in detail SQL Server offers a rich set of commands and
utilities that can be used to monitor the health of, and perform maintenance on, SQL Server The
Maintenance Plan Wizard is also available to streamline database maintenance All installations of SQL
Server should also include a database maintenance schedule to assist the DBA in keeping track of
maintenance performed
The next chapter explains how to use SQL Server Agent, which may be used to schedule jobs and create
custom maintenance jobs, and SQL Server Database Mail, for sending e-mail from SQL Server
1010
www.getcoolebook.com
Trang 10Automating Database
Maintenance with SQL
Server Agent
IN THIS CHAPTER
Setting up SQL Server Agent Understanding alerts, operators, and jobs Managing alerts Managing operators Managing jobs Setting up Database Mail
The automation of database maintenance is crucial to ensuring that a
database is regularly checked, maintained, and optimized Automated
checking consists of monitoring database size to identify issues before
they generate mayhem; maintenance includes frequent backups; and optimization
involves tweaking the index configuration for optimal performance Automation
ensures that these activities do not consume too much of your time, so you can
focus on more pressing issues (such as improving your golf game, perhaps)
Ideally, SQL Server can monitor itself and send alerts when it encounters a
crit-ical condition Luckily Microsoft grants this specific wish, because SQL Server
includes a powerful component that can send alerts when specific critical
condi-tions occur Better still, this same component also enables the scheduling of
rou-tine maintenance tasks either on a one-time basis or on a recurring basis — for
example, once a month or, say, on the first Saturday of every month SQL Server
Agent is the service responsible for processing alerts and running scheduled jobs
SQL Server Agent service is not available in SQL Server Express Edition, for which scheduling of jobs has to be done using Windows Scheduler For more information on Windows Scheduler,
search the web, using your preferred search engine or consult Microsoft Windows
documentation.
Setting Up SQL Server Agent
Setting up SQL Server Agent is straightforward, as long as you avoid two pitfalls:
The first is rather elementary (a startup issue); the second (a security issue) is a
bit more subtle It makes sense to start with the easy one Because SQL Server
Agent is a Windows service, make sure that the service is restarted if anybody
reboots the server (Microsoft sets this service not to start by default when you