13
Maintenance Plans Keith Binford Nebiyu Sorri

Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Embed Size (px)

Citation preview

Page 1: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Maintenance Plans

Keith BinfordNebiyu Sorri

Page 2: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Maintenance Plans

Most plans have at least four steps:

• Database consistency checking

• Database backup and backup retention policy

• Index maintenance

• Statistics maintenance

Page 3: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Consistency Checks

Preforming a database consistency check against a Microsoft SQL Server database involves validating the logical and physical integrity of all database objects.

• Schema

• Data allocations

• Page and storage consistency

Page 4: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

T-SQL Consistency CheckDBCC CHECKDB [

[ ( database_name | database_id | 0 [ , NOINDEX | , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]

) ] [ WITH

{ [ ALL_ERRORMSGS ] [ , EXTENDED_LOGICAL_CHECKS ] [ , NO_INFOMSGS ] [ , TABLOCK ] [ , ESTIMATEONLY ] [ , { PHYSICAL_ONLY | DATA_PURITY } ]

} ]

]

Page 5: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Use the REPAIR options only as a last resort. To repair errors, we recommend restoring from a backup. Repair operations do not consider any of the constraints that may exist on or between tables. If the specified table is involved in one or more constraints, we recommend running DBCC CHECKCONSTRAINTS after a repair operation. If you must use REPAIR, run DBCC CHECKDB without a repair option to find the repair level to use. If you use the REPAIR_ALLOW_DATA_LOSS level, we recommend that you back up the database before you run DBCC CHECKDB with this option.

Consistency Checks

Page 6: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Consistency Checks

-- Check the current database.

DBCC CHECKDB; GO

-- Check the AdventureWorks2012 database without nonclustered indexes.

DBCC CHECKDB (AdventureWorks2012, NOINDEX); GO

Page 7: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Creating Maintenance Plans

•T-SQL Statement

•Maintenance Plan Wizard

Page 8: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

BACKUP DATABASE [AdventureWorks2012] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\AdventureWorks2012_backup_2013_07_23_123130_6012101.bak' WITH NOFORMAT, NOINIT, NAME = N'AdventureWorks2012_backup_2013_07_23_123130_6012101', SKIP, REWIND, NOUNLOAD, STATS = 10

T-SQL Backup Database

Page 9: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

USE msdb; GO -- Adds a new job, executed by the SQL Server Agent service, called "HistoryCleanupTask_1". EXEC dbo.sp_add_job @job_name = N'HistoryCleanupTask_1', @enabled = 1, @description = N'Clean up old task history' ; GO -- Adds a job step for reorganizing all of the indexes in the HumanResources.Employee table to the HistoryCleanupTask_1 job. EXEC dbo.sp_add_jobstep @job_name = N'HistoryCleanupTask_1', @step_name = N'Reorganize all indexes on HumanResources.Employee table', @subsystem = N'TSQL', @command = N'USE AdventureWorks2012 GO ALTER INDEX AK_Employee_LoginID ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON ) GO USE AdventureWorks2012 GO ALTER INDEX AK_Employee_NationalIDNumber ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON ) GO USE AdventureWorks2012 GO ALTER INDEX AK_Employee_rowguid ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON ) GO USE AdventureWorks2012 GO ALTER INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON ) GO USE AdventureWorks2012 GO ALTER INDEX IX_Employee_OrganizationNode ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON ) GO USE AdventureWorks2012 GO ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON ) GO ', @retry_attempts = 5, @retry_interval = 5 ; GO -- Creates a schedule named RunOnce that executes every day when the time on the server is 23:00. EXEC dbo.sp_add_schedule @schedule_name = N'RunOnce', @freq_type = 4, @freq_interval = 1, @active_start_time = 233000 ; GO -- Attaches the RunOnce schedule to the job HistoryCleanupTask_1. EXEC sp_attach_schedule @job_name = N'HistoryCleanupTask_1' @schedule_name = N'RunOnce' ; GO

Page 10: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Creating Maintenance PlansMaintenance Plan Wizard

Page 11: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

Creating Maintenance PlansMaintenance Plan Wizard

Page 12: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

References:

Microsoft SQL Server 2012 by Patrick LeBlanc

MSDN Database Consistency Checkhttp://msdn.microsoft.com/en-us/library/ms176064.aspx

MSDN Creating a Maintenance Planhttp://msdn.microsoft.com/en-us/library/ms191002.aspx

Page 13: Maintenance Plans Keith Binford Nebiyu Sorri. Maintenance Plans Most plans have at least four steps: Database consistency checking Database backup and

END