Checks the integrity of all the pages and structures that make up the table or indexed view.
Note |
---|
To perform DBCC CHECKTABLE on every table in the database, use DBCC CHECKDB. |
- Index, in-row, LOB, and row-overflow data pages are correctly linked.
- Indexes are in their correct sort order.
- Pointers are consistent.
- The data on each page is reasonable, included computed columns.
- Page offsets are reasonable.
- Every row in the base table has a matching row in each nonclustered index, and vice-versa.
- Every row in a partitioned table or index is in the correct partition.
- Link-level consistency between the file system and table when storing varbinary(max) data in the file system using FILESTREAM.
Performing Logical Consistency Checks on Indexes
Logical consistency checking on indexes varies according to the compatibility level of the database, as follows:
- If the compatibility level is 100 (SQL Server 2008) or higher:
- Unless NOINDEX is specified, DBCC CHECKTABLE performs both physical and logical consistency checks on a single table and on all its nonclustered indexes. However, on XML indexes, spatial indexes, and indexed views only physical consistency checks are performed by default.
- If
WITH EXTENDED_LOGICAL_CHECKS is specified, logical checks are performed
on an indexed view, XML indexes, and spatial indexes, where present. By
default, physical consistency checks are performed before the logical
consistency checks. If NOINDEX is also specified, only the logical
checks are performed.
These logical consistency checks cross check the internal index table of the index object with the user table that it is referencing. To find outlying rows, an internal query is constructed to perform a full intersection of the internal and user tables. Running this query can have a very high effect on performance, and its progress cannot be tracked. Therefore, we recommend that you specify WITH EXTENDED_LOGICAL_CHECKS only if you suspect index issues that are unrelated to physical corruption, or if page-level checksums have been turned off and you suspect column-level hardware corruption. - If the index is a filtered index, DBCC CHECKDB performs consistency checks to verify that the index entries satisfy the filter predicate.
- If the compatibility level is 90 or less, unless NOINDEX is specified, DBCC CHECKTABLE performs both physical and logical consistency checks on a single table or indexed view and on all its nonclustered and XML indexes. Spatial indexes are not supported.
Internal Database Snapshot
DBCC
CHECKTABLE uses an internal database snapshot to provide the
transactional consistency that it must have to perform these checks. For
more information, see Understanding Sparse File Sizes in Database Snapshots and the "DBCC Internal Database Snapshot Usage" section in DBCC (Transact-SQL).
If a snapshot cannot be created, or TABLOCK is specified, DBCC CHECKTABLE acquires a shared table lock to obtain the required consistency.
If a snapshot cannot be created, or TABLOCK is specified, DBCC CHECKTABLE acquires a shared table lock to obtain the required consistency.
Note |
---|
If DBCC CHECKTABLE is run against tempdb, it must acquire a shared table lock. This is because, for performance reasons, database snapshots are not available on tempdb. This means that the required transactional consistency cannot be obtained. |
Checking and Repairing FILESTREAM Data
When FILESTREAM is enabled for a database and table, you can optionally store varbinary(max)
binary large objects (BLOBs) in the file system. When using DBCC
CHECKTABLE on a table that stores BLOBs in the file system, DBCC checks
link-level consistency between the file system and database.
For example, if a table contains a varbinary(max) column that uses the FILESTREAM attribute, DBCC CHECKTABLE will check that there is a one-to-one mapping between file system directories and files and table rows, columns, and column values. DBCC CHECKTABLE can repair corruption if you specify the REPAIR_ALLOW_DATA_LOSS option. To repair FILESTREAM corruption, DBCC will delete any table rows that are missing file system data and will delete any directories and files that do not map to a table row, column, or column value.
For example, if a table contains a varbinary(max) column that uses the FILESTREAM attribute, DBCC CHECKTABLE will check that there is a one-to-one mapping between file system directories and files and table rows, columns, and column values. DBCC CHECKTABLE can repair corruption if you specify the REPAIR_ALLOW_DATA_LOSS option. To repair FILESTREAM corruption, DBCC will delete any table rows that are missing file system data and will delete any directories and files that do not map to a table row, column, or column value.
Checking Objects in Parallel
By
default, DBCC CHECKTABLE performs parallel checking of objects. The
degree of parallelism is automatically determined by the query
processor. The maximum degree of parallelism is configured in the same
manner as that of parallel queries. To restrict the maximum number of
processors available for DBCC checking, use sp_configure. For more information, see max degree of parallelism Option.
Parallel checking can be disabled by using trace flag 2528. For more information, see Trace Flags (Transact-SQL).
Parallel checking can be disabled by using trace flag 2528. For more information, see Trace Flags (Transact-SQL).
Note |
---|
During a DBCC CHECKTABLE operation, the bytes that are stored in a byte-ordered user-defined type column must be equal to the computed serialization of the user-defined type value. If this is not true, the DBCC CHECKTABLE routine will report a consistency error. |
Understanding DBCC Error Messages
After
the DBCC CHECKTABLE command finishes, a message is written to the SQL
Server error log. If the DBCC command successfully executes, the message
indicates a successful completion and the amount of time that the
command ran. If the DBCC command stops before completing the check
because of an error, the message indicates the command was terminated, a
state value, and the amount of time the command ran. The following
table lists and describes the state values that can be included in the
message.
State | Description |
---|---|
0 | Error number 8930 was raised. This indicates a metadata corruption that caused the DBCC command to terminate. |
1 | Error number 8967 was raised. There was an internal DBCC error. |
2 | A failure occurred during emergency mode database repair. |
3 | This indicates a metadata corruption that caused the DBCC command to terminate. |
4 | An assert or access violation was detected. |
5 | An unknown error occurred that terminated the DBCC command. |
Error Reporting
A mini-dump file (SQLDUMPnnnn.txt)
is created in the SQL Server LOG directory whenever DBCC CHECKTABLE
detects a corruption error. When the Feature Usage data collection and
Error Reporting features are enabled for the instance of SQL Server, the
file is automatically forwarded to Microsoft. The collected data is
used to improve SQL Server functionality.
The dump file contains the results of the DBCC CHECKTABLE command and additional diagnostic output. The file has restricted discretionary access-control lists (DACLs). Access is limited to the SQL Server service account and members of the sysadmin role. By default, the sysadmin role contains all members of the Windows BUILTIN\Administrators group and the local administrator's group. The DBCC command does not fail if the data collection process fails.
The dump file contains the results of the DBCC CHECKTABLE command and additional diagnostic output. The file has restricted discretionary access-control lists (DACLs). Access is limited to the SQL Server service account and members of the sysadmin role. By default, the sysadmin role contains all members of the Windows BUILTIN\Administrators group and the local administrator's group. The DBCC command does not fail if the data collection process fails.
Resolving Errors
If
DBCC CHECKTABLE reports any errors, we recommend restoring the database
from the database backup instead of running REPAIR with one of the
REPAIR options. If no backup exists, running REPAIR can correct the
errors that are reported. The REPAIR option to use is specified at the
end of the list of reported errors. However, that correcting the errors
by using the REPAIR_ALLOW_DATA_LOSS option might require that some
pages, and therefore data, be deleted.
The repair can be performed under a user transaction to allow the user to roll back the changes that have been made. If repairs are rolled back, the database will still contain errors and must be restored from a backup. After you have completed all repairs, back up the database.
The repair can be performed under a user transaction to allow the user to roll back the changes that have been made. If repairs are rolled back, the database will still contain errors and must be restored from a backup. After you have completed all repairs, back up the database.
DBCC
CHECKTABLE returns the following result set. The same result set is
returned if you specify only the table name or any of the options.
DBCC CHECKTABLE returns the following result set if the ESTIMATEONLY option is specified:
DBCC results for 'HumanResources.Employee'. There are 288 rows in 13 pages for object 'Employee'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Estimated TEMPDB space needed for CHECKTABLES (KB) -------------------------------------------------- 21 (1 row(s) affected) DBCC execution completed. If DBCC printed error messages, contact your system administrator.
A. Checking a specific table
The following example checks the data page integrity of the HumanResources.Employee table in the AdventureWorks database.
USE AdventureWorks; GO DBCC CHECKTABLE ("HumanResources.Employee"); GO
B. Performing a low-overhead check of the table
The following example performs a low overhead check of the Employee table in the AdventureWorks database.
USE AdventureWorks; GO DBCC CHECKTABLE ("HumanResources.Employee") WITH PHYSICAL_ONLY; GO
C. Checking a specific index
The following example checks a specific index, obtained by accessing sys.indexes.
USE AdventureWorks; GO DECLARE @indid int; SET @indid = (SELECT index_id FROM sys.indexes WHERE object_id = OBJECT_ID('Production.Product') AND name = 'AK_Product_Name'); DBCC CHECKTABLE ("Production.Product", @indid);
No comments:
Post a Comment