Many DBA’s and database developers aren’t very familiar with DBCC commands (aka Database Console Commands) because they don’t always have the chance to work with them. In this article, we’ll provide a simple primer on DBCC commands with specific examples
Note: These commands many of them require sysadmin or db_owner permission.
Basic Commands:
These are a good place to begin if you have not worked with DBCC commands previously starting from the upcoming ones:
DBCC HELP:
- Provides syntax for a specific DBCC command, or lists all commands.
- By default, only supported commands listed
- This command has no impact on performance or data
- Requires sysadmin role
We have two options to run this DBCC Command:
When you just run it as it is, it will list all of the supported DBCC commands
1 2 3 |
DBCC HELP ('?'); |
When you run it, providing a specific command, it will give you the syntax for that:
1 2 3 4 |
DBCC HELP (CHECKDB); GO |
It will be very helpful if you don’t have internet access to get easy the syntax of any DBCC command just execute DBCC Help.
DBCC TRACEON, DBCC TRACEOFF, and DBCC TRACESTATUS:
If you have worked with trace flags before, you probably know that you can apply them as a startup parameter for the SQL Server service. Well, that is all well and good until you realize that for them to take effect, you have to restart the service. That is not always something you can easily do so that I can list some of the useful features of these commands:
- DBCC TRACEON and DBCC TRACEOFF can be used to enable/disable a trace flag without requiring a service restart.
- With DBCC TRACEON and DBCC TRACEOFF, you can enable/disable trace flags at either the session or the global level.
- Be aware that when you enable or disable a trace flag, it’s quite possible that you can affect performance, depending on what functionality the trace flag changes.
- Both commands (DBCC TRACEON and DBCC TRACEOFF) require the sysadmin role.
- DBCC TRACESTATUS provides status for a specific trace flag, or all of them, and notes whether they are enabled for a session or globally.
- Running DBCC TRACESTATUS does not affect performance or data, nor does it alter the configuration of the instance.
- DBCC TRACESTATUS only needs the public role to be run.
Now, Let us see some examples of using these commands:
Use the following code to list all trace flags enabled just for this connection:
1 2 3 4 |
DBCC TRACESTATUS (); GO |
Use this code to list all trace flags enabled globally:
1 2 3 4 |
DBCC TRACESTATUS (-1); GO |
Follow the following scenario to test both of DBCC TRACEON and DBCC TRACEOFF
Run a full backup operation for one of your databases:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DECLARE @BackupPath NVARCHAR (100); SET @BackupPath = 'C:\Backup\SQL_Shack2014_' + REPLACE (CONVERT (nvarchar (19), SYSDATETIME (), 126), ':','') + '.bak'; BACKUP DATABASE [SQL_Shack2014] TO DISK = @BackupPath WITH NOFORMAT, INIT, COMPRESSION, STATS = 05; GO |
Then run a transaction log backup operation on the same database:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DECLARE @BackupPath NVARCHAR (100); SET @BackupPath = 'C:\Backup\SQL_Shack2014_' + REPLACE (CONVERT (nvarchar (19), SYSDATETIME (), 126), ':','') + '.trn'; BACKUP LOG [SQL_Shack2014] TO DISK = @BackupPath WITH NOFORMAT, INIT, COMPRESSION, STATS = 05; GO |
Now, when you check the ERRORLOG, you will notice entries to the SQL Server error log for every successful backup operation (local connection) as in the following figure:
We can suppress all successful in SQL Server error log by enabling trace flag 3226 using DBCC TRACEON command:
1 2 3 4 |
DBCC TRACEON (3226); GO |
Then we can use DBCC TRACESTATUS command to verify that the trace is enabled:
1 2 3 4 |
DBCC TRACESTATUS (); GO |
Run a transaction log Backup operation again and check SQL Server log to see the impact of enabling this trace flag:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DECLARE @BackupPath NVARCHAR(100); SET @BackupPath = 'C:\Backup\SQL_Shack2014_' + REPLACE (CONVERT (nvarchar (19), SYSDATETIME (), 126), ':','') + '.trn'; BACKUP LOG [SQL_Shack2014] TO DISK = @BackupPath WITH NOFORMAT, INIT, COMPRESSION, STATS = 05; GO |
You will notice that there is a log record for enabling the trace flag 3226, and the last transaction log backup has not logged as the following figure:
You can disable this trace flag using the following code:
1 2 3 4 |
DBCC TRACEOFF (3226); GO |
To make these commands affect on SQL server instance globally which will be effective till the next restart of the instance, you can use the following codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* Enable trace flag (3226) globally */ DBCC TRACEON (3226, -1); GO /* Verify again that the trace flag is enabled globally */ DBCC TRACESTATUS (-1); GO /* Turn off 3226 globally */ DBCC TRACEOFF (3226,-1); GO |
Informational Commands:
These commands will not change the configuration of our environment, but they will give us information about our environment.
DBCC SQLPERF:
- ProvidesTransaction log space usage for all log files on an instance
- Used to clear out data related to weight statistics, latch statistics, or spinlock statistics
- To run this command, you need to have( VIEW SERVER STATE, ALTER SERVER STATE )
- Execution does not affect system performance
To check log space utilization:
1 2 3 |
DBCC SQLPERF (LOGSPACE); |
To clear wait statistics:
1 2 3 |
DBCC SQLPERF ("sys.dm_os_wait_stats", CLEAR); |
To clear latch statistics:
1 2 3 |
DBCC SQLPERF ("sys.dm_os_latch_stats", CLEAR); |
To clear spinlock statistics:
1 2 3 |
DBCC SQLPERF ("sys.dm_os_spinlock_stats", CLEAR); |
DBCC SHOW_STATISTICS:
- Displays statistics object for an index, indexed view, or column statistic
- Use when troubleshooting and understanding the estimates from a plan and comparing against actual values
- Viewing statistics does not introduce a performance load on the system
- Require sysadmin, db_owner, or db_ddladmin roles, or tablet ownership
To implement this DBCC Command go through the following steps:
First, you need to view all indexes contained in the specified table:
1 2 3 4 5 6 |
USE [SQL_Shack2014]; GO sp_helpindex '[dbo]. [Students]'; GO |
Then you can use DBCC SHOW_STATISTICS against the index name you got in step 1:
1 2 3 4 |
DBCC SHOW_STATISTICS ('[dbo]. [Students]','NonClusteredIndex-20170307-095338'); GO |
The result displays the header, histogram, and density vector based on data stored in the statistics object.
And you can specify what section of the result you want to see by using one of the following options:
1 2 3 4 5 6 7 8 9 10 |
DBCC SHOW_STATISTICS ('[dbo]. [Students]','NonClusteredIndex-20170307-095338') WITH STAT_HEADER; GO DBCC SHOW_STATISTICS ('[dbo]. [Students]','NonClusteredIndex-20170307-095338') WITH DENSITY_VECTOR; GO DBCC SHOW_STATISTICS ('[dbo]. [Students]','NonClusteredIndex-20170307-095338') WITH HISTOGRAM; GO |
DBCC USEROPTIONS:
- Returns options set for the current connection like isolation level or QUOTED_IDENTIFIER
- Use to verify setting for connection to confirm they are correct, or consistent across different connect methods
- Only displays information, does not modify settings
- Require public role
1 2 3 4 |
DBCC USEROPTIONS; GO |
Summary
As DBA or Database Developer, you can see the benefits DBCC Commands can offer for you. You may need one or more of them, which can provide you with a simpler way to implement your needs.
In this article, I have covered only top Basic DBCC commands. Hopefully I can cover the more advanced commands in another article. I hope this article has been informative for you.
- Concept and basics of DBCC Commands in SQL Server - March 31, 2017
- Hybrid Cloud and Hekaton Features in SQL Server 2014 - February 28, 2017
- How to analyze Storage Subsystem Performance in SQL Server - February 23, 2017