How to check database corruption in SQL Server?

Database corruption is a serious issue that can have significant consequences if not detected and repaired promptly. Corruption can occur due to software bugs, hardware failures, or human errors, and often manifests as incorrect data, inconsistencies, and database errors or crashes. SQL Server databases are complex systems with many interdependent components, making them susceptible to corruption.

Database corruption impacts performance and data integrity. Queries and transactions may start failing inexplicably. Data can be lost or damaged beyond repair. In the worst cases, the entire database may become inaccessible. Organizations rely on database uptime and accuracy for crucial business operations, making corruption highly disruptive. Early detection and repair is key to minimizing downtime and data loss.

DBCC diagnostic commands like CHECKDB and CHECKTABLE allow DBAs to proactively monitor for corruption. Extended options provide deeper levels of checking. Knowing how to analyze errors and utilize repair tools is critical for restoring database stability and recovering damaged data. Careful configuration and maintenance can also help avoid and prevent corruption issues.

Causes of Corruption

There are several common causes of database corruption in SQL Server:

Hardware failures like bad sectors, disk drive failures, memory errors, etc. can lead to data pages getting corrupted or lost (Source: Common SQL Database Corruption Errors, Causes, Solutions). Replacing faulty hardware components is important to prevent continued corruption issues.

Bugs in the SQL Server software can also cause corruption, especially in complex queries or transactions. Upgrading to the latest stable version helps avoid known bugs (Source: SQL Server Database Corruption, Part I: What Is Corruption?).

Improper shutdown of the SQL Server service while databases are still open can cause corruption of open data pages and log files. A clean shutdown should always be performed (Source: The Ultimate Guide to Database Corruption: Part 1).

Symptoms

There are several common symptoms that can indicate database corruption in SQL Server. Some of the most notable symptoms include:

Errors on read/write operations: You may encounter errors when trying to read or write data, such as “Error 9002”, “8967”, or “8963”. These errors indicate problems accessing pages or objects within the database (Source).

Incorrect query results: Corrupted databases can return incomplete, incorrect, or inconsistent results for queries that previously worked fine. This happens because the underlying data has become invalid or inaccessible (Source).

Data loss: In severe corruption cases, you may find entire tables, objects, or pages missing from the database altogether. This indicates structural damage and loss of data (Source).

Other symptoms like database startup errors, checksum failures, and system instability can also signal corruption.

Check DBCC CHECKDB

DBCC CHECKDB performs checks on the structural and data integrity of databases. It analyzes, checks, and repairs any inconsistencies in database objects like indexes, tables, views etc. Some key things it checks for include:

  • Index, heap, catalog consistency errors
  • Database and allocation consistency errors
  • Table and record consistency errors
  • Link consistency between tables

To run DBCC CHECKDB, you specify the database name after the command like:

DBCC CHECKDB ([‘database name’]);

DBCC CHECKDB can take some time to run on large databases. You can use WITH options to control what gets checked. For example, PHYSICAL_ONLY will only check the integrity of the physical structure, not the logical consistency of objects.

The output of DBCC CHECKDB provides information on any found errors or corruption. It categorizes issues by severity – Informational, Warning or Error. Issues shown under Error require attention to avoid bigger problems. The output also provides instructions on how to repair any found errors.

It’s recommended to run DBCC CHECKDB regularly to catch any corruption issues early before they can spread. MSDN recommends running it at least weekly (https://www.brentozar.com/archive/2016/02/how-often-should-i-run-dbcc-checkdb/). The frequency depends on factors like database size, rate of changes, criticality of data etc.

CHECKTABLE

DBCC CHECKTABLE is an alternative to running the full DBCC CHECKDB, and is less resource intensive since it only checks the integrity of a single specified table, rather than the entire database. As noted in the DBA Stack Exchange, CHECKTABLE performs extensive checks on the pages belonging to the specified table, but does not check database-wide allocation metadata.

According to the Microsoft Docs, DBCC CHECKTABLE verifies the integrity of all the pages and structures that comprise the table or indexed view. So while it focuses on a single table, it still does thorough checking of that object.

The CHECKTABLE command can be useful for spot checking suspected corruption in a specific table, without needing to check unrelated tables. It can also be used periodically to validate key tables, in between running full database checks.

Check DBCC PAGE

DBCC PAGE performs a low-level check of the integrity of specific pages. It allows you to dump the contents of a page so you can diagnose corruption issues at the page level. This is useful when DBCC CHECKDB has flagged a corrupt page but not provided details on which page is corrupt. DBCC PAGE can be used to inspect the suspect pages.

Some key things DBCC PAGE checks for include:

  • Valid page header and alignment
  • Data consistency within the page
  • Index and data correlations
  • Text/image columns stored within the page

To use DBCC PAGE, you specify the database, file id, page id and optional parameters. For example:

DBCC PAGE(AdventureWorks,1,23456,3)

This will check page 23456 in file 1 of the AdventureWorks database and output the contents in a detailed format. The page id can be obtained from sys.dm_db_database_page_allocations. DBCC PAGE provides low level details that can help precisely identify corrupt pages. However, it is more cumbersome than CHECKDB for performing a database-wide check.

For more details see: How to use DBCC PAGE

Third Party Tools

In addition to the built-in DBCC commands in SQL Server, there are some excellent third party tools available for detecting and repairing database corruption. Some popular options include:

  • ApexSQL Recover (https://www.ssw.com.au/ssw/standards/developergeneral/sqlservertools.aspx) – Provides comprehensive database recovery features, including analysis, repair, and data extraction from corrupt databases. Very customizable and easy to use.
  • Red Gate SQL Monitor (https://theqalead.com/tools/sql-editor/html) – Real-time monitoring and alerts for SQL Server performance issues and failures. Helps identify corruption early before it causes major problems.
  • SQL Examiner Suite (https://techcommunity.microsoft.com/t5/sql-server-blog/checkdb-part-6-consistency-checking-options-for-a-vldb/ba-p/383171) – Performs deep analysis of database corruption with repair guidance. Supports many repair options not available in native DBCC commands.

The major advantage of third party tools is they provide more robust corruption detection and repair capabilities than native SQL Server options. They can analyze issues deeper, extract damaged data, and support advanced repair scenarios. The downside is they require purchasing an additional license.

Prevention

There are several best practices that can help prevent and minimize SQL Server database corruption:

Backups – Performing regular backups of databases is crucial. Backups allow you to restore data if corruption occurs. Set up a backup schedule and test restores periodically.

Integrity checks – Running frequent DBCC CHECKDB commands checks for corruption and bad sectors on data files. Set these checks to run automatically at regular intervals.

Hardware redundancy – Use RAID configurations and redundant power supplies. This provides fault tolerance if drives fail. Also ensure proper ventilation and temperature control.

Additional preventative measures include patching and updating SQL Server regularly, monitoring disk space usage, using page verify checksums, and restricting access to reduce human error.

Repair Options

If corruption is found in your SQL Server database, you have a few options to repair it and restore full functionality:

One option is to use the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS option. This will attempt to repair any corruption, but may result in some data loss in the process. According to the Systools Group, this option should only be used as a last resort when the database absolutely needs to be online and a restore is not possible.

The safest and most reliable repair option is to restore the database from a known good backup. Before restoring, you should verify the backup file itself is not corrupted by using RESTORE VERIFYONLY. Once verified, you can fully restore the backup database and recover all data up to your last backup point. This avoids any potential data loss from DBCC CHECKDB repair options.

Third party SQL recovery tools are another option if the built-in DBCC and restore options are unable to successfully repair the database corruption. Tools like SQL Recovery Toolbox provide specialized recovery algorithms that can rebuild corrupt database objects and recover data that native SQL Server commands cannot.

Conclusion

Database corruption can cause a lot of headaches, but being proactive about checking for issues and catching them early is key. Regularly running DBCC CHECKDB, CHECKTABLE, and other diagnostic commands can help spot problems before they escalate or cause data loss. Leveraging third party tools as an additional check can also help.

The most important takeaway is having a solid prevention and backup plan. Following best practices for database configuration, applying patches promptly, using the right hardware, and performing regular backups will go a long way toward avoiding corruption issues.

If corruption does occur, options like repair, restore, and rollback are available to fix the problems. But catching issues early and having good backups is always preferable. By following the recommendations in this guide, DBAs can confidently monitor database health, catch corruption quickly, and resolve it with minimal disruption.