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

Concept and basics of DBCC Commands in SQL Server

$
0
0

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

DBCCHELP ('?');
Concept and basics of DBCC Commands in SQL Server
Figure 1 DBCC HELP command

When you run it, providing a specific command, it will give you the syntax for that:

DBCCHELP (CHECKDB); GO
Concept and basics of DBCC Commands in SQL Server
Figure 2 DBCC HELP (CHECKDB) command

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:

DBCCTRACESTATUS (); GO

Use this code to list all trace flags enabled globally:

DBCCTRACESTATUS (-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:

DECLARE @BackupPathNVARCHAR (100); SET @BackupPath = 'C:\Backup\SQL_Shack2014_' + REPLACE (CONVERT (nvarchar (19), SYSDATETIME (), 126), ':','') + '.bak'; BACKUPDATABASE [SQL_Shack2014] TO DISK = @BackupPath WITHNOFORMAT, INIT, COMPRESSION, STATS = 05; GO

Then run a transaction log backup operation on the same database:

DECLARE @BackupPathNVARCHAR (100); SET @BackupPath = 'C:\Backup\SQL_Shack2014_' + REPLACE (CONVERT (nvarchar (19), SYSDATETIME (), 126), ':','') + '.trn'; BACKUPLOG [SQL_Shack2014] TO DISK = @BackupPath WITHNOFORMAT, 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:


Concept and basics of DBCC Commands in SQL Server
Figure 3 Log file viewer (1)

We can suppress all successful in SQL Server error log by enabling trace flag 3226 using DBCC TRACEON command:

DBCCTRACEON (3226); GO

Then we can use DBCC TRACESTATUS command to verify that the trace is enabled:

DBCCTRACESTATUS (); GO
Concept and basics of DBCC Commands in SQL Server

Run a transaction log Backup operation again and check SQL Server log to see the impact of enabling this trace flag:

DECLARE @BackupPathNVARCHAR(100); SET @BackupPath = 'C:\Backup\SQL_Shack2014_' + REPLACE (CONVERT (nvarchar (19), SYSDATETIME (), 126), ':','') + '.trn'; BACKUPLOG [SQL_Shack2014] TO DISK = @BackupPath WITHNOFORMAT, 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:


Concept and basics of DBCC Commands in SQL Server
Figure 4 Log file viewer (2)

You can disable this trace flag using the following code:

DBCCTRACEOFF (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:

/* Enable trace flag (3226) globally */ DBCCTRACEON (3226, -1); GO /* Verify again that the trace flag is enabled globally */ DBCCTRACESTATUS (-1); GO /* Turn off 3226 globally */ DBCCTRACEOFF (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:

DBCCSQLPERF (LOGSPACE);
Concept and basics of DBCC Commands in SQL Server
Figure 5 Result of DBCC SQLPERF Command

To clear wait statistics:

DBCCSQLPERF ("sys.dm_os_wait_stats", CLEAR);

To clear latch statistics:

DBCCSQLPERF ("sys.dm_os_latch_stats", CLEAR);

To clear spinlock statistics:

DBCCSQLPERF ("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:

USE [SQL_Shack2014]; GO sp_helpindex '[dbo]. [Students]'; GO
Concept and basics of DBCC Commands in SQL Server

Then you can use DBCC SHOW_STATISTICS against the index name you got in step 1:

DBCCSHOW_STATISTICS ('[dbo]. [Students]','NonClusteredIndex-20170307-095338'); GO

The result displays the header, histogram, and density vector based on data stored in the statistics object.


Concept and basics of DBCC Commands in SQL Server
Figure 6 Result of DBCC SHOW_STATISTICS Command And you can specify what section of

Viewing all articles
Browse latest Browse all 3160

Latest Images

Trending Articles