Chapter 3: Getting Started with the Maintenance Plan Wizard 56 Reorganize Index The Reorganize Index task runs the ALTER INDEX statement with the REORGANIZE option on the indexes in the selected databases. This task helps to remove index fragmentation, but does not update index and column statistics. If you use this option to remove index fragmentation, then you will also need to run the Update Statistics task as part of the same Maintenance Plan. In addition, you won't need to run the Rebuild Index task, as the use of Reorganize Index task (followed by the Update Statistics task) renders redundant the Rebuild Index task. Update Statistics The Update Statistics task runs the sp_updatestats system stored procedure against the tables of the selected databases, updating index and column statistics. It is normally run after the Reorganize Index task is run. Don't run it after running the Rebuild Index task, as the Rebuild Index task performs this same task automatically. Execute SQL Server Agent Job The Execute SQL Server Agent Job task allows you to select SQL Server Agent jobs (ones you have previously created), and to execute them as part of a Maintenance Plan. This feature offers you additional flexibility when performing database maintenance using the Maintenance Plan Wizard. History Cleanup The History Cleanup task deletes historical data from the msdb database, including historical data regarding backup and restore, SQL Server Agent and Maintenance Plans. If you don’t perform this task periodically then, over time, the msdb database can grow very large. Back Up Database (Full) The Back Up Database (Full) task executes the BACKUP DATABASE statement and creates a full backup of the database. You will probably want to run this task daily against your system and production databases. In most cases, the databases you will be backing up with this task use the Full Recovery model, and you will also want to run the Backup Database (Transaction Log) task as part of your Maintenance Plan. Chapter 3: Getting Started with the Maintenance Plan Wizard 57 Back Up Database (Dierential) The Back Up Database (Differential) task executes the BACKUP DATABASE statement using the DIFFERENTIAL option. This task should only be used if you need to create differential backups. Backup Database (Transaction Log) The Backup Database (Transaction Log) task executes the BACKUP LOG statement, and, in most cases, should be part of any Maintenance Plan that uses the Back Up Database (Full) task. It is a common practice to run this task every hour or so, depending upon your needs. Maintenance Cleanup Task The Maintenance Cleanup task is problematic as it does not really do what it is supposed to do. In theory, it is designed to delete older backup files (BAK and TRN), along with older Maintenance Plan text file reports (TXT) files that you no longer need. The problem is that it can only delete one type of file at a time within a single Maintenance Plan. For example, if you choose to delete older BAK files, it won't delete older TRN or TXT files; if you choose to delete older TRN files, it won't delete older BAK or TXT files. What we really need is a task that performs all three inside the same Maintenance Plan, but we don't have it. So, what is the best way to delete old BAK, TRN, and TXT files? One way is to use the Maintenance Plan Designer, which allows you to create three separate subplans that will take care of deleting each of these three kinds of files within a single Maintenance Plan (see Chapter 17). However, if you want to use the Maintenance Plan Wizard exclusively to delete all three file types, you must create three different plans to accomplish your goal. Selecting Core Maintenance Tasks Now that we know a little bit about each of the eleven maintenance tasks available to us from within the Maintenance Plan Wizard, let's take a closer look at the Select Maintenance Tasks screen, which allows you to select the specific maintenance tasks that you'd like to include as part of the plan. Which options you choose will depend on your goals for the Maintenance Plan, along with any special needs of your databases. To keep the example simple, I am going to only select the tasks illustrated below in Figure 3.6. Don't worry if I have left out one of your favorites, as I will discuss each task in later chapters. The goal, for now, is to provide only a high-level overview of the Maintenance Plan Wizard. Chapter 3: Getting Started with the Maintenance Plan Wizard 58 Figure 3.6: The above tasks are commonly selected when creating a Maintenance Plan using the Maintenance Plan Wizard. Note that, despite the previous discussion regarding its limitations, I chose to include the Maintenance Cleanup task, for illustrative purposes. Maintenance Task Order Having chosen the maintenance tasks you want to include in your Maintenance Plan, click Next, and the Select Maintenance Task Order screen appears, as shown in Figure 3.7. Chapter 3: Getting Started with the Maintenance Plan Wizard 59 Figure 3.7: You must tell the Maintenance Plan Wizard the order in which you want it to perform its tasks. In this screen, you must tell the Wizard the order in which you want the maintenance tasks to execute. Figure 3.7 shows the six tasks we previously selected, in default order. In many cases, the default order will be fine, but you can move them up or down by selecting any one of the tasks, one at a time, and then clicking on either the "Move Up" or the "Move Down" button. This option is really only useful if you choose the Single schedule for the entire plan or no schedule option instead of the Separate schedules for each task that I recommend you use. Why? If you choose Single schedule for the entire plan or no schedule, this option is important because there is only one schedule, and each task within a Maintenance Plan will run one after another (based on your ordering), until the plan is complete. If you choose Separate schedules for each task, the order of the tasks is dependent on the order you schedule them, overriding any order you specify in this screen. This is because each task has its own schedule. More on this in a moment. Chapter 3: Getting Started with the Maintenance Plan Wizard 60 Whichever scheduling option you choose, consider the following when selecting the order of execution of the maintenance tasks. • Logical Task Ordering. A task such as Clean Up History can be performed at any point in the plan but, for other tasks, there is a certain logical order in which they should be performed. • It makes sense to start the Maintenance Plan with the Check Database Integrity task, because there is no point in running the rest of the maintenance tasks if the integrity of your database is in question. • The Back Up Database (Full) should come before the Backup Database (Transaction Log) task as we can't perform a transaction log backup before we perform a full database backup. If we were to try, we would get an error. • If a Rebuild Index task (or the Reorganize Index and Update Statistics tasks) is performed during the same maintenance window as the Back Up Database (Full) task, then I always perform the Rebuild Index task first. Why? This way, should I need to perform a restore of the backup, it will have its indexes defragmented and will be ready for production. • The Maintenance Cleanup task, if selected (see previous discussion), should be performed only after the Back Up Database (Full) has been completed. This way, you can ensure that a good backup has been made before deleting any older backups. • Task Scheduling. If you choose Separate schedules for each task, the scheduling of these tasks (covered later) will determine the actual order in which they occur. For example, it is possible to schedule the tasks in such a way that the Backup Database (Transaction Log) task runs before the first ever Back Up Database (Full) task for a given database, although this would cause the Maintenance Plan to fail. The key thing to remember is that the logical task order you select in Figure 3.7 is not absolute, and that it can be overridden by the schedules you set for each one. In this example, we will accept the default order, and click Next. Configuring Individual Tasks At this point, if we were continue with this example, we would be presented with six Define…Task screens, one after the other, allowing you to configure the exact characteristics of each task. The first screen presented in this example, will be the Define Database Check Integrity Taskscreen, shown in Figure 3.8. . this same task automatically. Execute SQL Server Agent Job The Execute SQL Server Agent Job task allows you to select SQL Server Agent jobs (ones you have previously created), and to execute them. Cleanup The History Cleanup task deletes historical data from the msdb database, including historical data regarding backup and restore, SQL Server Agent and Maintenance Plans. If you don’t perform. them as part of a Maintenance Plan. This feature offers you additional flexibility when performing database maintenance using the Maintenance Plan Wizard. History Cleanup The History Cleanup task