ptg 1074 option for the AdventureWorks2008 database. Multiple versions of backups can be retained with this option, and the identification of the backup is simple because of the naming convention. CAUTION You should use the option Back Up Databases Across One or More Files with caution. The pitfall with overwriting the file with this option is that only one backup is available for restoration. When this option is used with the Append option, you can eat up all your disk space if the file is not cleaned up. In addition, if multiple databases are backed up with the plan, all these backups will be spread across the file or files speci- fied for the destination. A separate backup for each database is not created with this option. This can lead to confusion and complicate the restoration process. Generally speaking, you should steer clear of backing up the database to a single file or set of files. Instead, you should choose the option Create a Backup File for Every Database. This option has fewer pitfalls and requires little attendance. When you use the Create a Backup File for Every Database option, you need to specify a folder for the database backups to be written to. You can use the default folder, or you can change it to a folder of your choice. It is a good practice to choose a folder on a drive that is different from the drive where your database files reside. Having backups on the same drive as your data could be a big problem if that drive fails and your only backups are on that drive. If you select the option Create a Sub-directory for Each Database, each database has a separate subfolder under the folder specified for the backup. CAUTION The main pitfall associated with the option Create a Backup File for Every Database is that many backup files can be created and are not automatically deleted by default. This point has been mentioned already, but it is a critical consideration. The good news is that you can now add the deletion of the older backups to the maintenance plan using the Maintenance Plan Wizard. To accomplish this, you need to select the Maintenance Cleanup task and provide the desired retention information that deter- mines when the older backups are removed. Another useful option on the Define Back Up Database screen is Verify Backup Integrity. If you select this option, SQL Server checks the integrity of the backup files that were written as part of the backup operation. Selecting this option extends the execution time for the backup plan but is generally a good idea to ensure that you have a viable backup for recovery. It is particularly useful when backups have been written across multiple files. Unfortunately, the backup task does not allow you to utilize the checksum options avail- able with the SQL Server 2008 BACKUP command, but the basic VERIFY option suffices in most instances. CHAPTER 33 Database Maintenance Download from www.wowebook.com ptg 1075 The Maintenance Plan Wizard 33 New to SQL Server 2008 is an option to define the compression for the database backup file. The Set Backup Compression drop-down at the bottom of the screen determines whether compression will be used. If the default for the server is set to compress backup or the specific option Compress Backup is selected, the backup file is created in a compressed format that will reduce the size of the backup file and save disk space. The trade-off when using compressed backups is that the creation of these backups takes additional CPU resources during their creation. However, the additional CPU processing time is typically offset by the faster I/O as a result of the reduced size of the backup file. NOTE In SQL Server 2008, compressed backups can only be performed in the Enterprise Edition. Beginning in SQL Server 2008 R2, compressed backups can be created in the Standard and all higher editions of SQL Server 2008 R2. Every edition of SQL Server 2008 and later can restore compressed backups, however. Checking Database Integrity The Define Database Check Integrity Task screen of the Maintenance Plan Wizard, shown in Figure 33.5, allows you to schedule the database consistency command DBCC CHECKDB, which checks the data pages for inconsistencies and is a good tool for ensuring that a database is healthy. The integrity checks can be made before each backup or on an inde- pendent schedule. The options available for checking database integrity via the wizard are limited. Checking the Include Indexes check box causes integrity checks to be performed on the index pages as well. Checking the index pages for each table extends the amount of time FIGURE 33.5 The Define Database Check Integrity Task screen of the Maintenance Plan Wizard. Download from www.wowebook.com ptg 1076 CHAPTER 33 Database Maintenance FIGURE 33.6 The Define Shrink Database Task page of the Maintenance Plan Wizard. the task runs, but it is the most thorough way to perform an integrity check. If problems are found, you can run the DBCC CHECKDB command manually with additional options to repair the problems. For more information on resolving DBCC errors, see Chapter 56, “SQL Server Disaster Recovery Planning” (on the CD). In some cases, the problems cannot be fixed without the possibility of data loss. You should consider contacting Microsoft support if you receive consistency errors in a critical database. Shrinking Databases The Define Shrink Database Task page of the Maintenance Plan Wizard, shown in Figure 33.6, can be useful for keeping the size of your databases manageable. As its name implies, this task is used to reduce the overall size of a database. This task’s execution is essentially the equivalent to running the DBCC SHRINKDATABASE command, and it contains task options that mirror the options available with the DBCC command. The setting Shrink Database When It Grows Beyond specifies the overall database size that must be exceeded for the shrink operation to occur. You set the size in megabytes, and it must be a whole number. If the database, including data and log files, is smaller than this size, the shrink operation does not occur. The remaining options determine how the shrink operation runs when the shrink thresh- old is exceeded. The Amount of Free Space to Remain After Shrink option determines how much space is left in the database files after the shrink operation is finished. This is a target percentage and may not be feasible if the amount of disk space is limited. SQL Server does its best to achieve the target percentage, but it is not guaranteed. Generally, in environments where you have abundant disk space, it is best to leave at least 10% free after the operation so that the database can grow without the need for expanding the size of the database files. Download from www.wowebook.com ptg 1077 The Maintenance Plan Wizard 33 The last settings on the screen determine how free space beyond the target percentage is handled. For example, let’s assume that a large number of rows were deleted from a data- base and the target free space percentage is set to 10%. The shrink operation is run and is able to shrink the database such that 40% is now free. You can choose to retain in the database files the 30% beyond the target that is free by selecting the Retain Freed Space in Database Files option. Choosing this option is the same as running the DBCC SHRINK DATA- BASE command with the NOTRUNCATE option. With this option, you do not see any changes to the size of the database files, and the free space on the disk remains unchanged. The other option, Return Freed Space to Operating System, can reduce the size of the data- base files and return that space to the operating system. This option utilizes the TRUNCATEONLY option that comes with the DBCC SHRINK DATABASE command and is the option needed to free up disk space on a server. TIP Running the Shrink Database task for every database is not necessarily a good idea. With the Shrink Database task, the database is condensed so that the data is located on contiguous pages in the database data file(s). This involves the movement of pages from one part of the file to another. This movement can cause fragmentation in tables and indexes. The fragmentation can, in turn, cause performance problems and undo work that may have been done with other tasks, such as rebuilding the indexes. The other problem with shrinking the database relates to the cost of expanding the database at a later time. For example, let’s say you have a database that has grown to 1GB. You shrink the database so that it is now only 800MB, but normal use of the database causes it to expand again. The expansion of the database files can be expensive and cause performance problems during the actual expansion, especially on high-volume production systems. The best solution is to purchase the appropriate amount of disk space and size the database so that database files do not need to expand frequently and the shrink operation is not needed. This is easier said than done, but it is the right answer nonetheless. Maintaining Indexes and Statistics Maintaining indexes and statistics is essential in most database environments, including those that have frequent changes to the data. These changes can cause tables and their indexes to become fragmented and inefficient. These types of environments can also lead to outdated statistics on indexes. Outdated statistics can cause the query engine to make less-than-optimal choices when determining the best access path to the data. The maintenance of indexes and statistics is facilitated through the use of three different tasks in the Maintenance Plan Wizard: Reorganize Index, Rebuild Index, and Update Statistics. Using the Reorganize Index task is equivalent to running the ALTER INDEX REORGANIZE command. This task defragments and compacts clustered and nonclustered indexes on tables and views. This helps improve index-scanning performance and should improve overall response time. The operation is always done online and is also equivalent to running the DBCC INDEXDEFRAG command. Download from www.wowebook.com ptg 1078 CHAPTER 33 Database Maintenance FIGURE 33.7 The Reorganize Index task options in the Maintenance Plan Wizard. Figure 33.7 shows the screen you use to define the Reorganize Index task. This screen allows you select tables, views, or tables and views. You can also select specific tables or views that you want to reorganize. The Compact Large Objects option is equivalent to ALTER INDEX LOB_COMPACTION = ON. It causes data in large object (LOB) data types, such as image or text objects, to be compacted. The Reorganize Index task moves the leaf-level pages so that they match the logical order- ing of the index. This behavior improves performance, but it is not as extensive as the Rebuild Index task, which is equivalent to the ALTER INDEX REBUILD command. It is also equivalent to the DBCC DBREINDEX command. When the Rebuild Index task is executed, it rebuilds the indexes from scratch. This rebuilding can achieve the best performance results, but it also has the most impact on users of the database. Figure 33.8 shows the options available for rebuilding an index with the Maintenance Plan Wizard. The options for rebuilding are separated into two sections: Free Space Options and Advanced Options. The Free Space Options section pertains to the amount of free space left in the index pages after the rebuild operation completes. This free space is defined by the fill factor for the index. When the Reorganize Pages with the Default Amount of Free Space option is used, the fill factor is reset to the value used when the index was created. The other option, Change Free Space per Page Percentage To, allows you to choose a new fill factor value to be used for all indexes that have been selected for the rebuild operation. Download from www.wowebook.com ptg 1079 The Maintenance Plan Wizard 33 FIGURE 33.8 The Rebuild Index task options in the Maintenance Plan Wizard. The following advanced Rebuild Index task options are available: . Sort Results in tempdb—This option is equivalent to the SORT_IN_TEMPDB option for the index. It causes tempdb to be used to store intermediate results while rebuilding the index. If this option is not used, these intermediate results are stored in the data- base in which the index resides. Storing the results in tempdb can help prevent unnecessary growth of the user database in which the index is being rebuilt. . Keep Index Online While Reindexing—This option is equivalent to the ONLINE option for the index. It allows users to access the underlying table and associated indexes during the index rebuild operation. If this option is not used, the index rebuild is on offline operation, and a table lock is held on the table that is having its indexes rebuilt. These index options and further information regarding indexes are discussed in Chapter 25, “Creating and Managing Indexes.” Refer to Chapter 34, “Data Structures, Indexes, and Performance,” for details on the performance impact of some of the index options discussed. The maintenance of statistics can be just as important as the maintenance of indexes on a table. Statistics contain information about the distribution of data in tables and indexes and provide valuable information to the SQL Server query engine. When the statistics are outdated, the query engine may not make the best decisions for getting the data. Download from www.wowebook.com ptg 1080 CHAPTER 33 Database Maintenance Fortunately, there are database options that cause statistics to be automatically updated. The AUTO UPDATE STATISTICS and AUTO UPDATE STATISTICS ASYNCHRONOUSLY options cause index statistics to be created automatically. However, in some situations the automatic update of statistics does not happen often enough, or the update happens at inopportune times and can cause performance issues. You can address these situations by scheduling the updating of statistics via a maintenance plan, using the Update Statistics task. Figure 33.9 shows the Maintenance Plan Wizard screen for setting the Update Statistics task options. The top portion of the Define Update Statistics Task screen is much like the option screens for maintaining indexes. You can choose the type of objects (tables or views) on which you want to update statistics, or you can focus on specific tables or views. The Update options at the bottom of the screen identify the types of statistics to be updated. If the All Existing Statistics option is selected, statistics for both indexes and columns are updated. Statistics on columns exist if the AUTO CREATE STATISTICS option has been set to ON or the statistics were manually created. The other two update options on the screen allow you to focus the update of statistics on columns only or indexes only. Scheduling a Maintenance Plan One of the greatest features of a maintenance plan is that you can schedule it. Scheduling takes manual work off your plate and provides consistency that might be missed if the plan had to be run manually. History is kept for each of the scheduled executions, which provides an audit trail, and notifications can be tied to the scheduled plans to allow a user to respond to failures or other results from the plan. FIGURE 33.9 The Update Statistics task options in the Maintenance Plan Wizard. Download from www.wowebook.com ptg 1081 The Maintenance Plan Wizard 33 A schedule can be created for an entire maintenance plan or individual schedules can be created for each task in the plan. The scheduling selection is available on the Select Plan Properties screen, which is one of the first screens displayed while using the Maintenance Plan Wizard (refer to Figure 33.1). Choose the Separate Schedules for Each Task option to create a schedule for each task. The default option is Single Schedule for the Entire Plan or No Schedule. If you choose the option for separate schedules, the Schedule Change button is enabled on the task definition screen and this schedule is tied to that specific task. Both scheduling options utilize the same scheduling screen. The screen to set scheduling options, the Job Schedule Properties dialog box shown in Figure 33.10, appears when you click on the Schedule Change button. This screen contains the same flexible scheduling features available in the SQL Server Agent. When a maintenance plan is saved, a scheduled job with the same name as the mainte- nance plan is created. The job schedule defined for the maintenance plan is applied to the scheduled job, and the SQL Server Agent manages the execution of the job, based on the schedule. Scheduling changes made to the maintenance plan are automatically reflected in the scheduled job. In addition, if the name of the maintenance plan is changed, the name of the scheduled job is changed as well. If an attempt is made to delete the sched- uled job related to the maintenance plan, an error is returned, disallowing the deletion. FIGURE 33.10 Scheduling options in the Maintenance Plan Wizard. Download from www.wowebook.com ptg 1082 CHAPTER 33 Database Maintenance TIP Scheduling in the Maintenance Plan Wizard is limited to one schedule per plan or task depending on which option you choose. You can surpass this limitation by adding addi- tional schedules to the scheduled job associated with the maintenance plan. To do so, you simply open the associated scheduled job located in the SQL Server Agent node in SSMS and create the additional schedules. This capability is handy when you want a varied execution, such as a weekly schedule combined with daily executions of the same plan. The scheduled job associated with a maintenance plan executes an SSIS package. Figure 33.11 shows an example of the scheduled job step for a SQL Server 2008 maintenance plan. The utilization of SSIS in the execution of maintenance plans was a significant change in SQL Server 2005. SSIS provides added workflow capabilities and extends the feature set for maintenance plans. The scheduled job step that executes an SSIS package for the mainte- nance plan shows some of the options and flexibility of SSIS, but the real power is in the maintenance plan editor and the Business Intelligence Design Studio (BIDS) used to manage all SSIS packages. Chapter 52, “SQL Server Integration Services,” provides further details on SSIS. The maintenance plan editor is discussed in the following section. FIGURE 33.11 Scheduling jobs for a maintenance plan. Download from www.wowebook.com ptg 1083 The Maintenance Plan Wizard 33 An integral part of a scheduled maintenance plan is the notification and reporting capa- bilities. The Select Report Options screen is displayed at the end of the Maintenance Plan Wizard (see Figure 33.12). The option Write a Report to a Text File provides details about the execution of each main- tenance plan. This option should be selected for most plans, and it provides excellent information for researching past executions and diagnosing any maintenance plan failures. The E-mail Report option provides a means for notifying a SQL Server operator when a task fails. You must have Database Mail enabled to be able to use this option, and the operator selected must have a valid email address to receive the notification. You can also edit the job associated with the maintenance plan after it has been created and set up notification there. The notification options on the scheduled job are more extensive than those in the Maintenance Plan Wizard. CAUTION If you have a maintenance plan generate a report, you need to make sure you have a means for cleaning up the files. The wizard does not create a plan that deletes the older report files. You can address this situation by modifying the plan after the wizard has created it and adding a Maintenance Cleanup task. This same task can be used to delete old database backup files. The modification of a maintenance plan and addition of the Maintenance Cleanup task are discussed in the following section. FIGURE 33.12 Reporting options in the Maintenance Plan Wizard. Download from www.wowebook.com . reduced size of the backup file. NOTE In SQL Server 2008, compressed backups can only be performed in the Enterprise Edition. Beginning in SQL Server 2008 R2, compressed backups can be created. compressed backups can be created in the Standard and all higher editions of SQL Server 2008 R2. Every edition of SQL Server 2008 and later can restore compressed backups, however. Checking Database. of the scheduled job step for a SQL Server 2008 maintenance plan. The utilization of SSIS in the execution of maintenance plans was a significant change in SQL Server 2005. SSIS provides added