09. Database Consistency

Page verify (DB level)

ALTER DATABASE db SET PAGE_VERIFY CHECKSUM WITH NO_WAIT;

dbo.suspect_pages

ALTER DATABASE db SET SINGLE_USER WITH NO_WAIT;
DBCC WRITEPAGE
ALTER DATABASE db SET MULTI_USER WITH NO_WAIT;

In-memory cannot be repaired through DBCC CHECKDB. Need to check backup of in-memory table for errors.

Corrupt Master - rebuild sys database

setup.exe /ACTION Rebuilddatabase /INSTANCENAME inst /Q

Restore MDB from backup. Reattach user database.

Corrupt Res DB or binary:

setup.exe /ACTION Repair /INSTANCENAME inst
DBCC CHECKDB WITH ...

Fixing Errors

Repair requires single user mode:

ALTER DATABASE db SET SINGLE_USER;
DBCC CHECKDB(db,opt)
ALTER DATABASE db SET MULTI_USER;

Emergency mode is last resort. Inaccessible pages => non errors in this mode:

ALTER DATABASE db SET EMERGENCY;
ALTER DATABASE db SET SINGLE_USER;
DBCC CHECKDB(db,opt)
ALTER DATABASE db SET MULTI_USER;
DBCC CHECKCATALOG
DBCC CHECKALLOC
DBCC CHECKTABLE
DBCC CHECKFILEGROUP
DBCC CHECKIDENT
DBCC CHECKCONSTRAINTS /* run after CHECKDB */

DBCC CHECKDB with PHYSICAL_ONLY only detects physical I/O errors

Full backup everynight plus PAGE_VERIFY CHECKSUM. Add WITH CHECKSUM to full backup. Then use DBCC CHECKALLOC instead of DBCC CHECKDB PHYSICAL_ONLY. Still need weekly CHECKDB to detect logical and in-memory errors.

With multiple filegroup, split workload by running CHECKFILEGROUP on subsets every night. Still need weekly CHECKDB.