A Small Collection of DBCC Commands Part 2
DBCC SqlPerf
Description:
DBCC SQLPERF is used to do a couple different things.
- Show a list of all the wait stats on your SQL Server.
- Show a list of the transaction log and the space used in the transaction log.
- Clear the wait stats.
- Clear the latch stats.
DBCC SQLPERF Syntax:
1
2
| dbcc sqlperf ( option ) [ WITH NO_INFOMSGS ] |
Where option can be any of the following:
- WAITSTATS
- LOGSPACE
- ‘sys.dm_os_latch_stats’ , CLEAR
- ‘sys.dm_os_wait_stats’ , CLEAR
Example of Viewing Wait Statistics:
The following example shows the list of wait statistics on your SQL Server since the instance was last restarted, or the stats were last cleared.
1
| DBCC SqlPerf(waitstats); |
The results include 650 rows of wait statistics, many of which are 0.
A better way to format this would be to dump the results into a table variable, then you can run operations like sorting on the result set, or filtering out the 0 values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| declare @WaitStats table ( WaitType varchar (255), Requests int , WaitTime bigint , SignalWaitTime bigint ); insert into @WaitStats execute ( 'dbcc sqlperf(waitstats)' ); select * from @WaitStats WHERE WaitType != 'Total' AND Requests = 0 AND WaitTime = 0 AND SignalWaitTime = 0 ORDER by WaitTime Desc ; |
If you are interested in Wait Stats, another way to track Wait Stats over time is with the Database Health Reports application which includes real time reporting as well as historic (over time) wait stat reporting. With the Database Health Reports application you can even track down the query that is causing the most wait stats.
Example of Clearing Wait Statistics:
The following example will clear the wait stat counters on your SQL Server.
1
| DBCC SqlPerf( 'sys.dm_os_wait_stats' , CLEAR); |
Example of Clearing Latch Statistics:
The following example clears all the latch statistics on your SQL Server.
1
| DBCC SqlPerf( 'sys.dm_os_latch_stats' , CLEAR); |
Example of Viewing Transaction Log Utilization:
The following example lists the raw output from DBCC SqlPerf (logspace), which show the overall usage of the log files for your database.
1
| DBCC SqlPerf(logspace) ; |
If we wanted to clean up the output, we could do something similar to the waitstats above by inserting the results into a table variable, and doing some sorting or filtering. How you sort or filter may be determined by where you are having trouble. For instance if you were running low on disk space, you could sort on LogSize MB.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| declare @LogSpace table ( DB varchar (255), LogSizeMB int , PercentUsed float , Status int ); insert into @LogSpace execute ( 'DBCC SqlPerf(logspace)' ); SELECT * FROM @LogSpace ORDER By LogSizeMB desc ; |
Here we can see that one log file is about two and a half GB, and significantly larger than any other database log file. This one was caused by some load testing that I was doing on the Database Health Reports application. The interesting thing here is that on 1.18% of the log file is being used, so most of that two and a half GB is being wasted.
DBCC ShrinkFile
Description:
If you remember DBCC InputBuffer from last week, DBCC OutputBuffer has a very similar syntax. Rather than seeing what was input, we see what was returned by the server. The output memory buffer contains the data in both hexadecimal and ASCII output.
The parameter is the session_id or the request_id.
DBCC OUTPUTBUFFER Syntax:
1
2
3
4
5
| dbcc outputbuffer ( session_id [ , request_id ] ) [ WITH NO_INFOMSGS ] |
Example:
The following example first will let us find a specific session, from that session id (SPID) we will look up what the input was as well as what the output returned is.
First, find a session.
1
2
3
| SELECT * FROM master.dbo.sysprocesses P ORDER BY last_batch DESC ; |
From here we can see that there is a SPID or session ID associated with a backup command is SPID 63.
Next we can take a look at the input buffer and the output buffer associated with the SPID 63.
1
2
3
4
5
| DECLARE @spid as INTEGER = 63; DBCC InputBuffer(@spid); DBCC OutputBuffer(@spid); |
From the output we see 2 panels, the first one shows the command that was issued on SPID 63, and the second one shows us a hex dump of what was returned in the output buffer from SPID 63. Keep in mind that the result set from the DBCC OutputBuffer command is always 256 lines, but the results from the last command may not fill the entire 256 rows, instead it may only be the beginning of the outout buffer.
For instance since the SPID we are looking at is associated with a backup command we can see that the backup is reporting 40 percent processed. The extra spaces are part of the NVARCHAR type or the extra space used to represent unicode characters.