Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL Server DBCC CHECKDB Overview

$
0
0

By:Brady Upton || Related Tips:More > Database Consistency Checks DBCCs

Problem

SQL Server database corruption can be a problem and can cause serious damage to a database. If youre an experienced DBA then you probably have safeguards in place to detect this, but over the years Ive seen hundreds of SQL Servers with no detection methods at all and this is a problem. There are a few ways to detect database corruption, but this tip will focus more on DBCC CHECKDB.

Solution

You may or may not have heard of DBCC (database console commands) statements. These statements are used to perform different operations in your database and can be broken down into four categories: Maintenance, Miscellaneous, Informational, and Validation. I use some of the DBCC statements on a daily basis, but none more than DBCC CHECKDB.

What is SQL Server DBCC CHECKDB

DBCC CHECKDB, from Microsoft MSDN Library , checks logical and physical integrity of all the objects in the specified database by performing the following operations:

Runs DBCC CHECKALLOC on the database Checks consistency of disk space allocation structures for a specified database. Runs DBCC CHECKTABLE on every table and view in the database Checks the integrity of all the pages and structures that make up the table or indexed view. Runs DBCC CHECKCATALOG on the database Checks for catalog consistency within the database. Validates the contents of every indexed view in the database. Validates link-level consistency between table metadata and file system directories and files when storing varbinary(max) data in the file system using FILESTREAM. Validates the Service Broker data in the database

If youve ever ran DBCC CHECKDB you know it takes some time for large databases. Now that you know all of the steps that are run, you can see why it takes time to complete.

How can SQL Server DBCC CHECKDB help me?

Data corruption is bad. It can cause all sorts of issues within the database that may include incorrect data results, failed SQL statements, and in some cases can take down the entire SQL instance. DBCC CHECKDB warns you of corruption so that you can fix it before (hopefully) it gets too bad.

How do I use SQL Server DBCC CHECKDB?

DBCC CHECKDB is pretty straightforward. There are a few options you can use with the statement and well go over some of those in the next section, but the basic syntax looks like this:

DBCC CHECKDB ('DatabaseName')

Pretty simple.

Automate SQL Server DBCC CHECKDB

Obviously, you dont want to log in every morning and run this statement on each database, so you can automate this process using a few different methods:

SQL Server Maintenance plans Maintenance plans are part of SQL Server out of the box (unless youre running Express Edition). I dont like using maintenance plans for the most part, but I dont mind using them for this type of task. In the Maintenance Plan toolbox youll need to use the Check Database Integrity task. The only configurable option is to include indexes so its not really user friendly, but in some cases this is all you need. Again, well talk about other options in the next section.
SQL Server DBCC CHECKDB Overview
Custom scripts Custom scripts are usually what I use and offer the best flexibility as far as adding the options you want. My go-to scripts are already created and free to use from Ola Hallengren . Hes done a wonderful job of creating these and sharing them to the world. Thanks Ola! Check out the scripts on mssqlTips.com: Perform Maintenance with SQL Server Databases in Full Recovery mode SQL Server Database Maintenance Plans and Backup File Management Performing SQL Server Maintenance with No Maintenance Window SQL Server DBCC CHECKDB Options

There are a few options to use with DBCC CHECKDB and Ill go over a few of the more popular ones here:

NOINDEX - Specifies that intensive checks of nonclustered indexes for user tables should not be performed. This decreases the overall execution time. NOINDEX does not affect system tables because integrity checks are always performed on system table indexes. NO_INFOMSGS - Suppresses all information messages. PHYSICAL_ONLY - Limits the checking to the integrity of the physical structure of the page and record headers and the allocation consistency of the database. This check is designed to provide a small overhead check of the physical consistency of the database, but it can also detect torn pages, checksum failures, and common hardware failures that can compromise a user's data. TABLOCK - Causes DBCC CHECKDB to obtain locks instead of using an internal database snapshot. This includes a short-term exclusive (X) lock on the database. TABLOCK will cause DBCC CHECKDB to run faster on a database under heavy load, but decreases the concurrency available on the database while DBCC CHECKDB is running. DATA_PURITY - Causes DBCC CHECKDB to check the database for column values that are not valid or out-of-range. For example, DBCC CHECKDB detects columns with date and time values that are larger than or less than the acceptable range for the datetime data type; or decimal or approximate-numeric data type columns with scale or precision values that are not valid.

Well go over some of the REPAIR options in a different section below.

How often should I check for SQL Server corruption?

Every minute of every day. Just kidding.

If you have a daily maintenance window, it would be nice to check for data corruption daily. The faster you can catch it, the less harm it may do. Ive noticed a lot of people run this on the weekend especially with larger databases. Theres no right or wrong with this, just make sure you have it scheduled periodically.

Do I have to run DBCC CHECKDB in my Production environment?

No, well Yes. Sort of.

To check for data corruption it does no good to run DBCC CHECKDB on your test environment.UNLESS you restore a copy of your production environment to test and then run it. BRILLIANT!

You may be questioning if some HA options are suitable such as AlwaysOn, Log Shipping, etc.? No, you need to check your production *live* environment.

Now that DBCC CHECKDB is configured and running, what am I looking for? Congratulations!

Viewing all articles
Browse latest Browse all 3160

Trending Articles