Chapter 12. Database Maintenance Plans

In Chapters 10 and 11, we talked a lot about ways in which you can execute both interactive and batch-scheduled database administrative tasks. In this chapter, we will introduce the SQL Server maintenance plan. SQL Server's maintenance plans give you a way to perform an array of database administrative tasks on a defined schedule. There are two ways to create SQL Server maintenance plans:

  • The first is by using the Maintenance Plan Wizard, which we will walk you through in detail in this chapter.

  • The second is to use the maintenance plan authoring environment in Management Studio. Figure 12-1 shows that environment.

Maintenance plan authoring environment in Management Studio

Figure 12.1. Maintenance plan authoring environment in Management Studio

It is no surprise that the authoring environment has a high degree of similarity to SQL Server Integration Services (SSIS); as a matter of fact, the maintenance plans are executed from SQL Server Agent as SQL Server integration packages, and the tasks in the maintenance plan designer are part of the SSIS Control Flow toolbox. You can use SSIS packages that perform maintenance tasks as well. Figure 12-2 shows the database maintenance tasks that are part of the SSIS Control Flow toolbox.

SSIS Package designer in Visual Studio

Figure 12.2. SSIS Package designer in Visual Studio

Understanding the Fundamentals

To create or manage maintenance plans, you must be a member of the sysadmin fixed server role. Management Studio's Object Explorer will display maintenance plans only for users who are members of this role.

A SQL Server maintenance plan allows you to execute the following tasks:

  • Back up the database (full, differential, or transaction log)

  • Check the database integrity

  • Execute a SQL Server Agent job

  • Execute T-SQL statements

  • Clean up the history

  • Do maintenance cleanup

  • Notify the operator

  • Rebuild an index

  • Reorganize an index

  • Shrink a database

  • Update statistics

We've covered some of these tasks already in Chapters 10 and 11. Table 12-1 briefly describes the tasks that are specific to maintenance plans that we haven't yet covered in any detail.

Table 12.1. Maintenance Plan Tasks Unique to the Maintenance Plan Environment

Maintenance Plan Task

Function

History Cleanup

Deletes history information from:

Backups and restores (via sp_delete_backuphistory)

SQL Server Agent (via sp_purge_jobhistory)

Maintenance plans (via sp_maintplan_delete_log)

Execute SQL Server Agent Job

Allows you to include SQL Server Agent Jobs in a maintenance plan. This gives you additional extensibility.

Notify Operator

Sends an e-mail to a SQL Server Agent operator. This is available only in the maintenance plan authoring environment (not the wizard).

Maintenance Cleanup

Deletes files that are created as part of the maintenance plan execution.

Execute T-SQL Statement

Allows you to execute Transact-SQL statements as part of the maintenance plan. This is available only in the maintenance plan authoring environment (not the wizard).

As you can see, the tasks in Table 12-1 focus on providing a comprehensive approach to creating a maintenance plan by doing the following:

  • Including extensibility options by allowing you to execute SQL Server Agent jobs and Transact-SQL statements

  • Providing cleanup utilities for the maintenance plan environment and SQL Server backup and job history

  • Integrating operator notification capabilities directly into the plan definition

Keep in mind that not all tasks that we talk about need to be executed as part of your regular maintenance. For example, you should not shrink your database on a scheduled basis, and you should not update statistics unless you have changed the data distribution in a table significantly.

Creating a Maintenance Plan

You can create a maintenance plan using the Maintenance Plan Wizard. The following sections walk you through the process. Basically, first you start the wizard. Then you specify some plan and job properties. Next you lay out the tasks to perform. Then you define the order in which to perform those tasks. Lastly, you configure each task, but we'll cover that in the "Configuring Individual Tasks" section.

Ready? Let's begin.

Starting the Maintenance Plan Wizard

Here's how to invoke the Maintenance Plan Wizard from Management Studio:

  1. Expand the server.

  2. Expand the Management folder.

  3. Right-click Maintenance Plans, and select Maintenance Plan Wizard.

Step 3 launches the wizard, which you can see in Figure 12-3. You can now step through and create a plan customized to meet your maintenance requirements.

SQL Server Maintenance Plan Wizard

Figure 12.3. SQL Server Maintenance Plan Wizard

Specifying Plan Properties

The next step in the process is to specify the plan properties, as shown in Figure 12-4. This is where you specify whether each task in the maintenance plan will run as a separate job on a separate schedule (referred to as subplans) or whether all the tasks will run as one job (one subplan) in SQL Server Agent. You also have the option to select a schedule for this maintenance plan job only in the case where you select a single schedule for the entire plan.

Maintenance Plan Wizard—Select Plan Properties page

Figure 12.4. Maintenance Plan Wizard—Select Plan Properties page

Specifying Job Schedule Properties

Job schedule properties allow you create a schedule for the maintenance plan. See Figure 12-5 for an example of a schedule. This schedule will be used in the SQL Server Agent job definition that will run the maintenance plan according to the schedule you define.

Maintenance Plan Wizard—Job Schedule Properties page

Figure 12.5. Maintenance Plan Wizard—Job Schedule Properties page

Selecting Maintenance Tasks

At this point in the Maintenance Plan Wizard, you can select the maintenance tasks that will be included in the maintenance plan. In this example, we've selected most of the tasks so that you can see the configuration settings for each of them, as shown in Figure 12-6.

Maintenance Plan Wizard—Select Maintenance Tasks page

Figure 12.6. Maintenance Plan Wizard—Select Maintenance Tasks page

Selecting Maintenance Task Order

Specifying task order is very important, especially for tasks where there is a dependency relationship. For example, reorganizing indexes and then shrinking a database in that order can undo the work of the index reorganize since a database shrink does not preserve the fragmentation state of indexes in a database. In addition, as a best practice, I recommend performing a Check Database Integrity task prior to a Database Backup task. Figure 12-7 shows the Select Maintenance Task Order screen, which allows you to move tasks up or down to set a specific order of execution.

Maintenance Plan Wizard—Select Maintenance Task Order page

Figure 12.7. Maintenance Plan Wizard—Select Maintenance Task Order page

Configuring Individual Tasks

After specifying the tasks to perform and their order, your next bit of business is to go through each task in turn and specify whatever properties or other information are needed in order for the task to perform as you intend. We've selected most of the available tasks in this chapter's example plan. The following sections show you how to configure each one of them.

Check Database Integrity

Check Database Integrity allows you to select the databases that you would like to perform a database consistency check on (by using the DBCC CHECKDB command) for the databases that you select. Figure 12-8 and Figure 12-9 show the two steps involved in selecting a set of databases to perform integrity checks on using the Check Database Integrity task.

Maintenance Plan Wizard—Define Database Check Integrity Task page

Figure 12.8. Maintenance Plan Wizard—Define Database Check Integrity Task page

Maintenance Plan Wizard—selecting databases

Figure 12.9. Maintenance Plan Wizard—selecting databases

Shrink Database

The Shrink Database task allows you to select the databases that you would like to shrink (by using the DBCC SHRINKDATABASE command), as shown in Figure 12-10. Notice that there is a condition that you can set to control when the shrink is performed even though it is part of the overall maintenance plan. Also note that you have the ability to shrink only a database; if you need to shrink files, you will have to use the Execute T-SQL Statement task.

Maintenance Plan Wizard—Define Shrink Database Task page

Figure 12.10. Maintenance Plan Wizard—Define Shrink Database Task page

Reorganize Index

The Reorganize Index task allows you to select the set of indexes you would like to reorganize indexes on from the database level down to the table or view level, as shown in Figure 12-11. One thing to note here is that you do not have the ability to specify individual indexes with this task. This task generates the ALTER INDEX Transact-SQL command for each of the indexes included in the selected set of objects. If you need to reorganize individual indexes, you will have to use the Execute T-SQL Statement task.

Maintenance Plan Wizard—Define Reorganize Index Task page

Figure 12.11. Maintenance Plan Wizard—Define Reorganize Index Task page

Rebuild Index

The Rebuild Index task allows you to select the set of indexes you would like to reorganize indexes on from the database level down to the table or view level, as shown in Figure 12-12. One thing to note here is that you do not have the ability to specify individual indexes with this task. This task generates the ALTER INDEX Transact-SQL command for each of the indexes included in the selected set of objects. If you need to rebuild individual indexes, you will have to use the Execute T-SQL Statement task.

Maintenance Plan Wizard—Define Rebuild Index Task

Figure 12.12. Maintenance Plan Wizard—Define Rebuild Index Task

Update Statistics

The Update Statistics task allows you to select the set of statistics you would like to update from the database level down to the table or view level, as shown in Figure 12-13. One thing to note here is that you do not have the ability to specify individual statistics with this task. However, you do have the ability to specify the type of statistics to update (column or index), as well as the scan type. This task generates the UPDATE STATISTICS Transact-SQL command for each of the statistics included in the selected set of objects. If you need to update individual statistics, you will have to use the Execute T-SQL Statement task.

Maintenance Plan Wizard—Define Update Statistics Task page

Figure 12.13. Maintenance Plan Wizard—Define Update Statistics Task page

History Cleanup

The History Cleanup task allows you to select the historical data you would like to remove (backup, SQL Server Agent, or maintenance plan) based on age of the log information, as shown in Figure 12-14. This task generates the Transact-SQL commands for each of the statistics included in the selected set of objects.

Maintenance Plan Wizard—Define History Cleanup Task page

Figure 12.14. Maintenance Plan Wizard—Define History Cleanup Task page

Back Up Database (Full)

The Back Up Database (Full) task allows you to perform the three types of backup mentioned earlier (full, differential, and log) for an individual database or set of databases, as shown in Figure 12-15. This task generates the Transact-SQL commands for each of the databases included in the selected set of objects. This task provides a rich set of options; however, if there are specific options that you require that are not selectable in this task (for example, "Perform checksum before writing to media"), then you can use the Execute T-SQL Statement task.

Maintenance Plan Wizard—Define Back Up Database (Full) Task page

Figure 12.15. Maintenance Plan Wizard—Define Back Up Database (Full) Task page

Maintenance Cleanup

The Maintenance Cleanup task allows you to remove backup files and reports that were created as part of the maintenance plan execution based on the file location and the file age, as shown in Figure 12-16.

Maintenance Plan Wizard—Define Maintenance Cleanup Task page

Figure 12.16. Maintenance Plan Wizard—Define Maintenance Cleanup Task page

Select Report Options

The Select Report Options task allows you to select whether you would like a report of maintenance plan actions and how you would like them distributed. See Figure 12-17 for an example of writing a report to a text file.

Maintenance Plan Wizard—Define Select Report Options page

Figure 12.17. Maintenance Plan Wizard—Define Select Report Options page

Completing the Maintenance Plan Wizard

Next is the verification step in the wizard prior to the actual creation of the maintenance plan that you are defining. The page shown in Figure 12-18 displays all the choices you have made in the wizard. Once you have verified your choices, click Finish to start the maintenance plan build process.

Maintenance Plan Wizard—Complete the Wizard page

Figure 12.18. Maintenance Plan Wizard—Complete the Wizard page

Figure 12-19 shows the progress report that you'll see while SQL Server builds your maintenance plan. (Think of "build" in this case as analogous to "compile.") You will see an action step for each of the major options you have selected. Verify that each step completes successfully, as shown in the figure.

Maintenance Plan Wizard— Maintenance Plan Wizard Progress page

Figure 12.19. Maintenance Plan Wizard— Maintenance Plan Wizard Progress page

Modifying an Existing Maintenance Plan

Once you have created your maintenance plans, you can modify them using SQL Server Management Studio. To modify an existing maintenance plan from Management Studio, perform the following steps:

  1. Expand the server.

  2. Expand the Management folder.

  3. Expand the Maintenance Plans folder

  4. Right-click the maintenance plan you would like to update, in this case SampleMaintenancePlan, and select Modify.

  5. Your menu selection launches the maintenance plan authoring environment from which you can make modifications to your maintenance plans. Figure 12-20 shows this environment, which looks very similar to the SQL Server Integration Services authoring environment.

SQL Server maintenance plan designer in Management Studio

Figure 12.20. SQL Server maintenance plan designer in Management Studio

Summary

You can create multiple maintenance plans that have different objectives and goals that run on different schedule. Just make sure that the schedules do not overlap or conflict. For example, if you have a weekly full backup and cleanup maintenance plan that runs on Sunday, make sure that the daily differential backup with no cleanup maintenance plan runs only Monday through Saturday.

The following set of tasks and order of those tasks are the ones we recommend using as a starting point. You can then add tasks based on the observed behavior of your database.

  1. Check database integrity.

  2. Back up the database.

  3. Determine the fragmentation levels of indexes you care about.

  4. Reorganize those with low levels of fragmentation.

  5. Rebuild those with high levels of fragmentation.

  6. Update the statistics of those you reorganized in step 4 (rebuilding automatically updates statistics).

  7. Update any statistics not for tables that had large data distribution changes because bulk-type operations.

Maintenance plans represent a powerful feature in SQL Server and contribute greatly to its ease of use. Be sure to take advantage of them for their value in automating the drudgework involved in database administration. Good, well-designed maintenance plans are one of the key stepping-stones to success for any DBA.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset