Thursday, January 17, 2008

DBCC

DBCC stood for Database Console Command (Database Consistency Checker prior to SQL2000). It's now divides into four categories: validation, maintenance, informational, and miscellaneous.

To get a list of all the DBCC commands: DBCC help ('?')

To get syntax help with a particular DBCC command: DBCC help ('checkdb')

The Validation commands are the CHECK commands:

DBCC CHECKTABLE
DBCC CHECKDB
DBCC CHECKALLOC
DBCC CHECKFILEGROUP
DBCC CHECKCATALOG
DBCC CHECKCONSTRAINT
DBCC CHECKIDENT

The Informational commands are:

DBCC IND
DBCC PAGE
(you need to turn on trace flag 3604 to see the output. DBCC TRACEON(3604) )

DBCC CheckDB

DBCC CHECKDB runs all of the other DBCC validation commands in this order:

DBCC CHECKALLOC is run on the database;
DBCC CHECKTABLE is run on every table and indexed view in the database;
DBCC CHECKCATALOG is run on the database;
The Service Broker data in the database is verified.

In SQL Server 2005, all of the DBCC validation commands use database snapshot technology to keep the validation operation from interfering with ongoing database operations and to allow the validation operation to see a quiescent, consistent view of the data, no matter how many changes were made to the underlying data while the operation was under way. A snapshot of the database is created at the beginning of the CHECK command, and no locks are acquired on any of the objects being checked. The actual check operation is executed against the snapshot.
The "snapshot file" that DBCC CHECKDB creates with the original page images is not visible to the end user and its location cannot be configured; it always uses space on the same volume as the database being checked. This capability is available only when your data directory is on an NTFS partition.
If you aren't using NTFS, or if you don't want to use the space necessary for the snapshot, you can avoid creating the snapshot by using the WITH TABLOCK option with the DBCC command.
In addition, if you are using one of the REPAIR options to DBCC, a snapshot is not created because the database is in single-user mode, so no other transactions can be altering data. Without the TABLOCK option, the DBCC validation commands are considered online operations because they don't interfere with other work taking place in a database. With the TABLOCK option, however, a Shared Table lock is acquired for each table as it processed, so concurrent modification operations will be blocked.

For databases that have been upgraded from previous SQL Server versions, you must run DBCC CHECKDB with the DATA_PURITY option once, preferably immediately after the upgrade, as follows:

DBCC CHECKDB (<db_name>) WITH DATA_PURITY

SQL Server 2005 includes a set of logical validation checks to verify that data is appropriate for the column's datatype. These checks can be expensive and can affect the server's performance, so you can choose to disable this, along with all the other non-core logical validations by using the PHYSICAL_ONLY option. All new databases created in SQL Server 2005 have the DATA_PURITY logical validations enabled by default.

After the purity check completes without any errors for a database, performing the logical validations is the default behavior in all future executions of DBCC CHECKDB, and there is no way to change this default. You can, of course, override the default with the PHYSICAL_ONLY option. This option not only skips the data purity checks, but it also skips any checks that actually have to analyze the contents of individual rows of data and basically limits the checks that DBCC performs to the integrity of the physical structure of the page and the row headers.

If the CHECKSUM option is enabled for a database, which is the default in all new SQL Server 2005 databases, a checksum will be performed on each allocated page as it is read by the DBCC CHECK commands. When the CHECKSUM option is on, a page checksum is calculated and written on each page as it is written to disk, so only pages that have been written since CHECKSUM was enabled will have this check done. The page checksum value is checked during the read and compared with the original checksum value stored on the page. If they do not match, an error is generated. In addition, pages with errors are recorded in the suspect_pages table in the msdb database.

Using the following script to see the progress reporting for DBCC CHECKDB, DBCC CHECKTABLE, and DBCC CHECKFILEGROUP:

select command,percent_complete,estimated_completion_time

from sys.dm_exec_requests
where command like 'dbcc %'

DBCC CHECKDB Best Practices:

· Use CHECKDB with the CHECKSUM database option and a sound backup strategy to protect the integrity of the data from hardware-caused corruption.

· Perform DBCC CHECKDB with the DATA_PURITY option after upgrading a database to SQL 2005 to check for invalid data values.

· Make sure there is enough disk space availabe to accommodate the database snapshot that will be created.

· Make sure there is enough space available in tempdb to allow the DBCC CHECKDB command to run.

SET NOCOUNT ON;
DBCC CHECKDB ('<DB NAME>') WITH ESTIMATEONLY;
use tempdb;
sp_helpfile;

You can see the current size of the log file for all databases, as well as the percentage of the log file space that has been used, by running the DBCC command DBCC SQLPERF('logspace'). The following code, supplied by Cliff Dibble from Microsoft, returns the same information as DBCC SQLPERF('logspace') in a tabular format that can be further filtered or easily embedded into a stored procedure, table-valued function, or view:

  SELECT rtrim(pc1.instance_name) AS [Database Name]
   ,      pc1.cntr_value/1024.0 AS [Log Size (MB)]
   ,      cast(pc2.cntr_value*100.0/pc1.cntr_value as dec(5,2))
           as [Log Space Used (%)]
   FROM    sys.dm_os_performance_counters as pc1
   JOIN    sys.dm_os_performance_counters as pc2
   ON      pc1.instance_name = pc2.instance_name
   WHERE   pc1.object_name LIKE '%Databases%'
   AND     pc2.object_name LIKE '%Databases%'
   AND     pc1.counter_name = 'Log File(s) Size (KB)'
   AND     pc2.counter_name = 'Log File(s) Used Size (KB)'
   AND     pc1.instance_name not in ('_Total', 'mssqlsystemresource')
   AND     pc1.cntr_value > 0
go

The final condition is needed to filter out databases that have no log file size reported. This includes any database that is unavailable because it has not been recovered or is in a suspect state, as well as any database snapshots, which have no transaction log.

Snapshots

Considerations regarding database snapshots:

· Snapshots cannot be created for the model, master, or tempdb databases. (Internally, snapshots can be created to run the online DBCC checks on the master database, but they cannot be explicitly created).

· A snapshot inherits the security constraints of its source database, and because it is read-only, you cannot change the permissions.

· If you drop a user from the source database, the user is still in the snapshot.

· Snapshots cannot be backed up or restored; Snapshot will not affect source database backup.

· Snapshots cannot be attached or detached.

· Full-text indexing is not supported on database snapshots, and full-text catalogs are not propagated from the source database.

Server 'DHW01' is not configured for DATA ACCESS

When you try to run an openquery script against a local database, you received the following error:

Msg 7411, Level 16, State 1, Line 1
Server 'DHW01' is not configured for DATA ACCESS.

To rectify the issue, run the following script:

exec sp_serveroption 'DHW01', 'DATA ACCESS', 'TRUE'

Login failed for user 'Test'.

Following are reasons of the Login failed for user 'Test':

1. If you receive the following error

Cannot connect to DHW01

Login failed for user 'Test'. The user is not associated with a trusted SQL Server connection. (Microsoft SQL Server, Error: 18452)

This is because the Server authentication property is set to "Windows Authentication mode" (SQL2005) or "Windows only" (SQL2000). Change the server authentication property to "SQL Server and Windows Authentication mode" (SQL2005) or "SQL Server and Windows" (SQL2000) will fix the issue. The option is located under Security of the server property.