To maintain your database, you should perform several tasks, either manually or automatically. These tasks form the core of a database maintenance plan. They include
If you choose to have these tasks performed automatically, you can set up and schedule your database maintenance plan with the Database Maintenance Plan Wizard.
This lesson gives details on each task you should consider for your database maintenance plan and takes you through the process of automating your plan.
After this lesson, you will be able to
- Develop a database maintenance plan
- Use the Database Maintenance Plan Wizard to implement a database maintenance plan
Estimated lesson time: 40 minutes
Several tasks can help you maintain your database. The most important tasks, which should be performed for all SQL Server databases, are updating data optimization information, verifying data integrity, performing backups, and keeping a history of maintenance activities. These tasks should be performed on a regular basis. How often you run these tasks depends on the level of database activity and the size of your database. As you learned in Chapter 13, you can create scheduled SQL Server jobs to perform these tasks automatically.
Update Information About Data Optimization
As data and index pages fill up, updating requires more time, and pages can become fragmented. Reorganizing your data and index pages can improve performance.
Maintain Indexes Using the fillfactor Option
You can specify the percentage of available free space (fillfactor) in your index and data pages. This enhances performance: if space is available in existing pages when performing inserts and updates, SQL Server does not have to split pages and allocate new pages. The fillfactor percentage is used when the index is first created and whenever the index is rebuilt. You can specify a percentage or allow SQL Server to select the optimal value automatically.
Update Statistics That Are Used by the Query Optimizer
You should run UPDATE STATISTICS on tables that are being modified. This updates the information about the key value distribution for one or more indexes in a table, which the query optimizer uses to generate optimal query plans.
Remove Unused Space from the Database Files
You can execute DBCC SHRINKDATABASE to recover any unused disk space in the database tables. You can also enable the autoshrink option on databases.
Data integrity tests detect inconsistency in the database caused by hardware or software errors.
Perform Internal Data Integrity Tests
Execute DBCC CHECKALLOC to check the allocation of data and index pages for each table within the extent structures of the database.
Perform Database Integrity Tests
Execute DBCC CHECKDB to check the allocation and structural integrity of the objects in the database. Execute DBCC CHECKTABLE to check the integrity of the data, index, text, and index pages for a table. If DBCC finds an error, you can specify that it repair the error automatically.
DBCC CHECKDB performs all of the checks performed by DBCC CHECKALLOC and DBCC CHECKTABLE on each table in the database. If you run DBCC CHECKDB regularly, it is not necessary to run DBCC CHECKALLOC and DBCC CHECKTABLE as well. If time is limited, you can use DBCC CHECKALLOC and DBCC CHECKTABLE to perform smaller checks at different times rather than running a full DBCC CHECKDB at once. If DBCC CHECKDB reports only allocation errors, you can use DBCC CHECKALLOC to repair the errors. The safest option is to run DBCC CHECKDB with the repair option; this repairs all errors, including allocation errors. While DBCC CHECKDB is running, it is not possible to create, alter, or drop tables.
NOTE
Previous versions of SQL Server had a DBCC NEWALLOC statement. This is supported in SQL Server 7 for backward compatibility only; you should use DBCC CHECKALLOC instead.
Perform backups on a regular basis to protect against data loss. You should back up the transaction log to capture changes to the database between full database backups.
Maintain a history of the maintenance tasks. This history should include what actions were performed, as well as the results of any corrective actions.
You can use either the Database Maintenance Plan Wizard or the sqlmaint utility to automate your database maintenance plan so that it runs on a regularly scheduled basis.
The Database Maintenance Plan Wizard helps you set up the core maintenance tasks that are necessary to ensure that your database performs well, is regularly backed up in the event of a system failure, and is checked for inconsistencies. When you run the wizard, you will specify the following:
Databases That the Plan Maintains
You can define a single maintenance plan for all databases or plans for one or more databases.
Data Optimization Information
You can have the wizard reorganize data and index pages, update the index statistics to ensure that the query optimizer has current information regarding the spread of data in the tables, and compress data files by removing empty database pages.
Data Verification Tests
You can have the wizard perform internal consistency checks of the data and data pages within the database to ensure that a system or rare software problem has not damaged data. You can specify whether indexes should be included in the checks and whether the wizard should attempt to repair minor problems that it finds.
Frequency and Destination of Backups
You can schedule database and transaction log backups and keep backup files for a specified time.
Location of History Files
The results that the maintenance tasks generate can be written as a report to a text file, saved in history tables, or sent in an e-mail message to an operator.
Use the sqlmaint utility to execute DBCC statements, dump a database and transaction logs, update statistics, and rebuild indexes. The sqlmaint utility is a command-prompt utility that performs functions similar to those handled by the Database Maintenance Plan Wizard.
In this exercise, you will create a maintenance plan using the Database Maintenance Plan Wizard.
It is important to regularly perform various maintenance tasks on your databases. Regular maintenance includes keeping table and index statistics up to date, performing database consistency checks, making backups, and organizing database space allocation. You can automate maintenance by creating your own jobs or by creating a database maintenance plan using the Database Maintenance Plan Wizard.