Skip to content

DBCC Commands

DBCC ShrinkDatabase – I want to shrink my database.

TL;DR summary: Don’t do it. Stop reading here if you want, but just don’t do it.

This post refers to shrinking your database files (mdf, or ndf files), not shrinking the log file. The log file is a completely different conversation, however shrink database does shrink the log file.

Not shrinking your database is one of the more counter intuitive things out there. You might think that a smaller database is a good thing, however there are some negative side effect if you shrink your database regularly, or have the autoshrink option enabled. Side effects of shrinking your database include:

  • Excessive I/O due to the shrink.
  • Index fragmentation (most likely all of your indexes).
  • Excessive I/O to defragment your indexes.
  • After the shrink is complete, inserting or updating rows that require more space in your database will be slowed due to the time involved with growing your data file.

Read More »DBCC ShrinkDatabase – I want to shrink my database.

DBCC CheckDB All Databases

If you use the SQL Server maintenance plans to run DBCC Check DB, you are not allowed to include TempDB in your DBCC CheckDB sequence. For a while I was using maintenance plans, and ending up adding a separate job step to run TSQL to just check TempDB.

Rather than using two different steps, the regular maintenance plan, and a special step for Temp DB, I have instead dumped the regular maintenance plan step, and just used the following TSQL code to run DBCC CheckDB against all databases on the SQL Server.

Option 1: DBCC CheckDB All Databases using sp_msforeachdb

I have included Option 2 below since there are some pretty serious flaws in the sp_msforeachdb script as shown in option 1, specifically sp_msforeachdb will occasionally skip databases. It also has trouble with databases created with certain characters.  Anyway if you want it, here is, but I would recommend paging down to Option 2 which is much more reliable. I would like to thank Patrick Flynn who showed me the flaws in sp_msforeachdb.

The stored procedure sp_msforeachdb takes a parameter of query that will get run against all databases on your SQL Server Instance.


EXEC sp_msforeachdb 'DBCC CHECKDB(''?'') WITH NO_INFOMSGS;';

You can set it up as a maintenance plan like this:

Read More »DBCC CheckDB All Databases

SQL Sunday Fun – Word Search with SQL Server DBCC Commands

SQL_DBCC_Commands

The following DBCC commands are all hidden in the word search.  Be aware, some of the DBCC commands are undocumented.

CACHESTATS CHECKCATALOG CHECKCONSTRAINTS
CHECKDB CHECKFILEGROUP CHECKIDENT
CLEANTABLE DROPCLEANBUFFERS FLUSHPROCINDB
FREE FREEPROCCACHE FREESESSIONCACHE
FREESYSTEMCACHE INDEXDEFRAG OPENTRAN
OUTPUTBUFFER PAGE PROCCACHE
SHOWCONTIG SHRINKDATABASE TRACEON
TRACESTATUS UNPINTABLE UPDATEUSAGE
USEROPTIONS

END OF PUZZLE ————————-

See also:

For more information on DBCC Commands: