210 likes | 477 Views
Week 7 : Chapter 7 Agenda. Maintenance Plan: Why do maintenance? Overview Maintenance Plan wizard DBCC maintenance commands. SQL 710. Why do Maintenance?. Routinely want to make a backup copy of database in case of hardware problems or data corruption
E N D
Week 7 : Chapter 7 Agenda • Maintenance Plan: • Why do maintenance? • Overview • Maintenance Plan wizard • DBCC maintenance commands SQL 710
Why do Maintenance? • Routinely want to make a backup copy of database in case of hardware problems or data corruption • Data in a database becomes fragmented over time as data is added, modified and deleted • Should verify integrity of data occasionally • Normally want to perform these tasks when you choose – not as the result of a crisis (just like car maintenance; don’t want to be stuck on the side of a highway with no oil in engine)! SQL 710
Maintenance Overview • Day-to-day operation • Very important responsibility of a DBA • If you do maintenance nobody notices but if you don’t do it everyone knows • 2 types of maintenance: • - to keep database running most important • - to improve performance • Database maintenance can be fully automated – there is a wizard for almost every task SQL 710
Database Maintenance Wizard • Primary tool for database maintenance • Graphical tool • Can create a scheduled maintenance plan for each database that performs: • Backups (most important!) • Optimization • Integrity Check • Reporting SQL 710
Maintenance Planning • Normally use wizard to create a maintenance plan for each database • Best to have one plan per database but can choose more than 1 database per plan • Plans are scheduled separately as required by application • Very important to create a separate maintenance plan for critical system databases: master, msdb SQL 710
Maintenance Plan • A Maintenance Plan consists of a group of jobs and schedules called a plan • A plan can be edited after you create it • Very simple tool to use - no excuse not to use it • Within a job you can perform: • Backups • Optimization • Integrity Check • Reporting SQL 710
Database Backups • Most important part of maintenance plan: permits recovery of data from backup copy • Backs up database files or logs to a tape or hard drive • Hard drive location can be broken into several directories for each database • Can automatically delete older backups after a certain time • Wizard records all activities and sends notification • Report can be sent to disk, a central server, a history table or to an operator SQL 710
Optimization • Improves database performance • Database gets fragmented when data is inserted and deleted (same as files on a hard drive, see last slides for illustration) • Optimization causes the following to be performed that can improve database performance: • Examine and minimize fragmentation of data • Update database statistics • Remove excess free space from database SQL 710
Data Fragmentation • Database get fragmented when data is inserted or deleted • You can select how much free space for new data will be left when defragmenting is done • If database is primarily for data entry (OLTP) then it is best to leave significant free space at the end of each page so that related data stays together • If database is composed of data that is read more often than being written (OLAP) such as a report server then leave lower percentage of page space free. SQL 710
Update database statistics • Statistics are samples of data used to assist SQL Server search engine in locating data • As data is added and deleted these statistics become outdated • Can keep statistics updated 2 ways: • Use wizard to schedule update of statistics periodically (this may slow performance if statistics are out of date because data would not be retrieved efficiently) • Set database option that automatically updates statistics (this slows performance because statistics are continually updated) SQL 710
Remove excess free space • This setting in the wizard permits shrinking the database at a specific recurring time. • Automatic shrink of database can be set as an option SQL 710
Database Integrity Check • SQL Server is a very stable RDBMS • However should check integrity of a database periodically • Integrity checks validity of defined database constraints (NN, PK, FK, CK, UN) • Integrity check can correct minor errors automatically • May have to restore data from backup if major integrity error is found (very rare!) SQL 710
T-SQL Maintenance Commands • T-SQL maintenance commands are the Database Consistency Check commands (DBCC) • Command line utility • Maintenance wizard actually uses these commands • Perform maintenance on Database Index and file groups • Commands include: • DBCC CHECKDB • DBCC CHECKTABLE • DBCC DBREINDEX SQL 710
DBCC CHECKDB Command • CHECKDB command examines an entire database for corruption (checks all tables and indexes in database) • Command can be run in diagnostic mode • To correct any problem it has to be run in single user mode • Can use REPAIR_FAST or • REPAIR_ALLOW_DATA_LOSS switch SQL 710
CHECKDB Switches • REPAIR_FAST Switch: • Least amount of damage to database • Quickly fixes any inconsistency • Don’t lose any data • REPAIR_ALLOW_DATA_LOSS Switch: • Most harmful switch but could save overall database • You can use this command on a database that you plan to recover from tape backup SQL 710
DBCC CHECKTABLE Command • Checks for database corruption against a table. • You can use this command while people are using the table except while performing repair SQL 710
DBCC DBREINDEX Command • Command updates the indexes on a server • Indexes are used for faster data access • Indexes are stored like data pages and become fragmented over time • DBCC DBREINDEX command defragments indexes • Time required depends on: • Number of indexes • Size of indexes • Load on server when running command SQL 710
Physical structure of database Insert Order 1 orderlines on Page 1: SQL 710
Physical structure of database (ctd) Insert Order 2’s orderlines on Page 1(blue) and page 2 (white) SQL 710
Physical structure of database (ctd) Add order 1 orderlines – no room on page 1 so place on page 2 – data for order 1 is fragmented SQL 710
Physical structure of database (ctd) Order 2 cancelled- delete Order 2’s orderlines – leaves Order 1’s orderlines fragmented SQL 710