Chapter 1: Why is Database Maintenance Important? 26 The longer the damage remains undiscovered, the more out of date will be the most recent undamaged backup. If you delete older backups on a regular schedule, you may not even have an undamaged copy! In either case, you may end up losing a lot of data, so it is important for DBAs to regularly check the physical integrity of their databases, using the DBCC CHECKDB command. Maintain a Database's Indexes Over time, as indexes are subjected to data modifications (INSERTs, UPDATEs, and DELETEs), index fragmentation can occur in the form of gaps in data pages that create wasted empty space, and in a logical ordering of the data that no longer matches the physical ordering of the data. Both forms of fragmentation are normal byproducts of data modifications but, unfortunately, both can hurt SQL Server's performance. Wasted space reduces the number of rows that can be stored in SQL Server's data cache, which can lead to increased disk I/O. The index page ordering problem also causes extra disk activity, as it often takes more work to find the data on disk and move it to the data cache, than it would if the pages were in physical order. SQL Server doesn't automatically correct index fragmentation problems. The only way to remove wasted space and restore the correct page ordering is to rebuild or reorganize the indexes on a regular basis. This requires the DBA to create a maintenance job to perform these tasks. Maintain Index and Column Statistics The Query Optimizer uses index and column statistics as part of its evaluation process, as it tries to determine an optimal query execution plan. If the statistics are old, or incomplete, then the Query Optimizer might create an inecient execution plan, which substantially slows down a query's performance. In theory, index and column statistics are self- maintaining, but this self-maintaining process is not perfect in practice. In order to ensure that the optimizer has the most complete and current statistics at its disposal, the DBA needs to create a maintenance task to ensure that they are regularly updated, either by rebuilding the indexes, or by updating the statistics using the UPDATE STATISTICS or sp_updatestats commands. Chapter 1: Why is Database Maintenance Important? 27 Remove Older Data from msdb The SQL Server msdb database stores historical data about various activities, such as details about backups, SQL Server Agent jobs, and Maintenance Plan execution. If left unattended, the msdb database can grow over time to a considerable size, wasting disk space, and slowing down operations that use the msdb database. In most cases, this data does not need to be kept for a long period, and should be removed using such commands as sp_delete_ backuphistory, sp_purge_jobhistory, and sp_maintplan_delete_log. Remove Old Backups While making database backups is important, you don't need to keep them for ever. If fact, if you don't clean up older backup files, your SQL Server's hard drives will quickly fill up, causing all sorts of problems. It is the job of the DBA to ensure that unneeded backups are removed from a SQL Server on a regular basis. What's Outside the Scope of the Maintenance Plan Wizard and Designer? While Maintenance Plans are a convenient way to perform much of your database maintenance work, neither the Wizard nor the Designer can do all your work for you. While the tasks included with Maintenance Plans are a good first start, the Wizard and designer aren't really intended to enable you to perform every single maintenance task that could be included in your database maintenance strategy. For example, the following additional important database maintenance tasks are not covered by the Wizard or Designer: • identifying and remove physical file fragmentation • identifying missing, duplicate, or unused indexes • protecting backups so that they are available when needed • verifying that backups are good and can be restored Chapter 1: Why is Database Maintenance Important? 28 • monitoring performance • monitoring SQL Server and operating system error messages • monitoring remaining disk space • and much, much more. The moral of the story is that, while Maintenance Plans are a useful tool for many DBAs, they are not the perfect tool for all DBAs, and will only perform a subset of the required database maintenance tasks. If the Maintenance Plan Wizard or Designer meets your needs, then use them. On the other hand, if they don't properly meet your needs, then don't use them. Custom-created T-SQL or PowerShell scripts instead offer much more power and flexibility. While there may be a steep learning curve to create custom scripts, this is knowledge that you will be able to use elsewhere as a DBA, and it won't go to waste. Summary In this chapter, we have learned what database maintenance is, and why it is important, and we have considered the core database maintenance tasks that will comprise almost every database maintenance plan. We also explored four different ways to perform database maintenance, including use of the Maintenance Plan Wizard and the Maintenance Plan Designer, which are the tools we'll focus on in this book, as well as T-SQL and PowerShell scripts. In the following chapters, we learn how to create Maintenance Plans using the Maintenance Plan Wizard. As we learn how to use the Wizard, we will also be learning a lot of information that applies to the Maintenance Plan Designer, as both tools perform similar tasks and offer similar configuration options. 29 Chapter 2: Before you Create any Maintenance Plans… Both the Maintenance Plan Wizard and the Maintenance Plan Designer have the ability to send e-mails to DBAs, providing them with information on the status of the Maintenance Plans that have executed. The number of e-mails and their contents vary depending on which tool you use to configure these e-mail notifications. In the case of the Maintenance Plan Wizard, you can configure an option that will send you an e-mail every time that a Maintenance Plan has been executed, which includes standard information on what plan was run, and what steps were executed, and reports any errors that occurred. Once this option is configured, it will send an e-mail every time the Maintenance Plan is executed, and it will always include the same standard information. The Maintenance Plan Designer, on the other hand, has more options. First, it allows the DBA to configure the conditions under which e-mail notifications will be sent. For example, instead of always sending an e-mail, as the Maintenance Plan Wizard does when a Maintenance Plan executes, the Maintenance Plan Designer can send e-mails based on conditional logic. So if you only want to receive e-mails if a Maintenance Plan fails, and not all the time, you can configure this. In addition, the Maintenance Plan Designer allows you to create custom e-mail messages, so that when you receive an e-mail, you know exactly what the problem is, instead of having to wade through a long report. In either case, before you can receive e-mail messages from Maintenance Plans, created with either the Maintenance Plan Wizard or the Maintenance Plan Designer, there are two preliminary setup steps you must take: 1. Set up Database Mail. 2. Create one or more SQL Server Agent Operators, who will receive the e-mail notifications. These two steps are described in detail, in this chapter. Having completed them, you'll be able to select the "e-mail report" option when creating a new Maintenance Plan. If you have existing Maintenance Plans that don't report via e-mail, you can modify them to do so using the Maintenance Plan Designer, as described in Chapter 19. Chapter 2: Before you Create any Maintenance Plans… 30 How to Configure Database Mail While there are a couple of different ways to configure Database Mail, the easiest way is to use the Database Mail Configuration Wizard from within SSMS. To start this Wizard, navigate to the Management folder of the appropriate server, right-click on Database Mail, and select Configure Database Mail, as shown in Figure 2.1. Figure 2.1: Using the Database Mail Configuration Wizard to set up Database Mail for the first time. Click Next to get past the splash screen, and the Wizard starts off with the Select Configuration Task screen, as shown in Figure 2.2. . is Database Maintenance Important? 27 Remove Older Data from msdb The SQL Server msdb database stores historical data about various activities, such as details about backups, SQL Server Agent. are good and can be restored Chapter 1: Why is Database Maintenance Important? 28 • monitoring performance • monitoring SQL Server and operating system error messages • monitoring remaining disk. perfect in practice. In order to ensure that the optimizer has the most complete and current statistics at its disposal, the DBA needs to create a maintenance task to ensure that they are regularly