Sunteți pe pagina 1din 8

7/16/2014

DBCC Commands

DBCC Commands
Summary
Use ful SQ L Se rve r DBC C C om m ands
The following SQ L Se rve r DBC C com m ands, som e docum e nte d and som e not docum e nte d, can com e in handy whe n
you are trying to optim ize your SQ L Se rve rs.

DBCC CACHESTATS:
Displays inform ation about the obje ct curre ntly in the buffe r cache , such as hit rate s, com pile d obje cts and plans, e tc.
Note in the sam ple re sults be low that e ach of the se SQ L Se rve r obje cts can be cache d in the buffe r cache of SQ L
Se rve r.
Ex am ple :

DBCC CACHESTATS
Sam ple R e sults (abbre viate d):

Object Name
------------

Hit Ratio
-------------

Proc
Prepared
Adhoc
ReplProc
Trigger
Cursor
Exec Cxt
View
Default
UsrTab
SysTab
Check
Rule
Summary

0.86420054765378507
0.99988494930394334
0.93237136647793051
0.0
0.99843452831887947
0.42319205924058612
0.65279111666076906
0.95740334726893905
0.60895011346896522
0.94985969576133511
0.0
0.67021276595744683
0.0
0.80056155581812771

He re 's what som e of the k e y statistics from this com m and m e an:
Hit R atio: Displays the pe rce ntage of tim e that this particular obje ct was found in SQ L Se rve r's cache . The bigge r
this num be r, the be tte r.
O bje ct C ount: Displays the total num be r of obje cts of the spe cifie d type that are cache d.
Avg. C ost: A value use d by SQ L Se rve r that m e asure s how long it tak e s to com pile a plan, along with the
am ount of m e m ory ne e de d by the plan. This value is use d by SQ L Se rve r to de te rm ine if the plan should be
cache d or not.
Avg. Page s: Me asure s the total num be r of 8K page s use d, on ave rage , for cache d obje cts.
LW O bje ct C ount, LW Avg C ost, W L Avg Stay, LW Ave Use : All the se colum ns indicate how m any of the spe cifie d
obje cts have be e n re m ove d from the cache by the Lazy W rite r. The lowe r the figure , the be tte r.

DBCC DROPCLEANBUFFERS:
Use this com m and to re m ove all the te st data from SQ L Se rve r's data cache (buffe r) be twe e n te sts to e nsure fair
te sting. Ke e p in m ind that this com m and only re m ove s cle an buffe rs, not dirty buffe rs. Be cause of this, be fore
running the DBC C DR O PC LEANBUFFER S com m and, you m ay first want to run the C HEC KPO INT com m and first.
R unning C HEC KPO INT will write all dirty buffe rs to disk . And the n whe n you run DBC C DR O PC LEANBUFFER S, you can
be assure d that all data buffe rs are cle ane d out, not just the cle an one s.
Ex am ple :
http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

1/8

7/16/2014

DBCC Commands

DBCC DROPCLEANBUFFERS

DBCC ERRORLOG
If you rare ly re start the m ssqlse rve r se rvice , you m ay find that your se rve r log ge ts ve ry large and tak e s a long tim e
to load and vie w. You can truncate (e sse ntially cre ate a ne w log) the C urre nt Se rve r log by running DBC C ER R O R LO G.
You m ight want to conside r sche duling a re gular job that runs this com m and once a we e k to autom atically truncate
the se rve r log. As a rule , I do this for all of m y SQ L Se rve rs. Also, you can accom plish the sam e thing using this
store d proce dure : sp_cycle _e rrorlog.
Ex am ple :

DBCC ERRORLOG

DBCC FLUSHPROCINDB
Use d to cle ar out the store d proce dure cache for a spe cific database on a SQ L Se rve r, not the e ntire SQ L Se rve r. The
database ID num be r to be affe cte d m ust be e nte re d as part of the com m and.
You m ay want to use this com m and be fore te sting to e nsure that pre vious store d proce dure plans won't ne gative ly
affe ct te sting re sults.
Ex am ple :

DECLARE @intDBID INTEGER SET @intDBID = (SELECT dbid FROM master.dbo.sysdatabases WHERE name = 'dat
DBCC FLUSHPROCINDB (@intDBID)

DBCC INDEXDEFRAG
In SQ L Se rve r 2000, Microsoft introduce d DBC C INDEXDEFR AG to he lp re duce logical disk fragm e ntation. W he n this
com m and runs, it re duce s fragm e ntation and doe s not lock the table , allowing othe r use rs to acce ss the table whe n
the de fragm e ntation proce ss is running. Unfortunate ly, this com m and doe sn't do a gre at job of logical
de fragm e ntation.
The only way to truly re duce logical fragm e ntation is to re build your table 's inde x e s. W hile this will re duce all
fragm e ntation, unfortunate ly it will lock the table , pre ve nting use rs from acce ssing it during this proce ss. This m e ans
that you will ne e d to find a tim e whe n this will not pre se nt a proble m to your use rs.
O f course , if you are unable to find a tim e to re inde x your inde x e s, the n running DBC C INDEXDEFR AG is be tte r than
doing nothing.
Ex am ple :
DBC C INDEXDEFR AG (Database _Nam e , Table _Nam e , Inde x _Nam e )

DBCC FREEPROCCACHE
Use d to cle ar out the store d proce dure cache for all SQ L Se rve r database s. You m ay want to use this com m and
be fore te sting to e nsure that pre vious store d proce dure plans won't ne gative ly affe ct te sting re sults.
Ex am ple :

DBCC FREEPROCCACHE

http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

2/8

7/16/2014

DBCC Commands

DBCC MEMORYSTATUS
Lists a bre ak down of how the SQ L Se rve r buffe r cache is divide d up, including buffe r activity. Undocum e nte d
com m and, and one that m ay be droppe d in future ve rsions of SQ L Se rve r.
Ex am ple :

DBCC MEMORYSTATUS

DBCC OPENTRAN
An ope n transaction can le ave lock s ope n, pre ve nting othe rs from acce ssing the data the y ne e d in a database . This
com m and is use d to ide ntify the olde st ope n transaction in a spe cific database .
Ex am ple :

DBCC OPENTRAN('database_name')

DBCC PAGE
Use this com m and to look at conte nts of a data page store d in SQ L Se rve r.
Ex am ple :

DBCC PAGE ({dbid|dbname}, pagenum [,print option] [,cache] [,logical])


whe re :
Dbid or dbnam e : Ente r e ithe r the dbid or the nam e of the database in que stion.
Page num : Ente r the page num be r of the SQ L Se rve r page that is to be e x am ine d.
Print option: (O ptional) Print option can be e ithe r 0, 1, or 2. 0 - (De fault) This option cause s DBC C PAGE to print
out only the page he ade r inform ation. 1 - This option cause s DBC C PAGE to print out the page he ade r
inform ation, e ach row of inform ation from the page , and the page 's offse t table . Each of the rows printe d out will
be se parate d from e ach othe r. 2 - This option is the sam e as option 1, e x ce pt it prints the page rows as a single
block of inform ation rathe r than se parating the individual rows. The offse t and he ade r will also be displaye d.
C ache : (O ptional) This param e te r allows e ithe r a 1 or a 0 to be e nte re d. 0 - This option cause s DBC C PAGE to
re trie ve the page num be r from disk rathe r than che ck ing to se e if it is in cache . 1 - (De fault) This option tak e s
the page from cache if it is in cache rathe r than ge tting it from disk only.
Logical: (O ptional) This param e te r is for use if the page num be r that is to be re trie ve d is a virtual page rathe r
the n a logical page . It can be e ithe r 0 or 1. 0 - If the page is to be a virtual page num be r. 1 - (De fault) If the
page is the logical page num be r.

DBCC PINTABLE & DBCC


UNPINTABLE
By de fault, SQ L Se rve r autom atically brings into its data cache the page s it ne e ds to work with. The se data page s will
stay in the data cache until the re is no room for the m , and assum ing the y are not ne e de d, the se page s will be
flushe d out of the data cache onto disk . At som e point in the future whe n SQ L Se rve r ne e ds the se data page s again,
it will have to go to disk in orde r to re ad the m again into the data cache for use . If SQ L Se rve r som e how had the
ability to k e e p the data page s in the data cache all the tim e , the n SQ L Se rve r's pe rform ance would be incre ase d
be cause I/O could be significantly re duce d on the se rve r.
The proce ss of "pinning a table " is a way to te ll SQ L Se rve r that we don't want it to flush out data page s for spe cific
nam e d table s once the y are re ad in in the first place . This in e ffe ct k e e ps the se database page s in the data cache
all the tim e , which e lim inate s the proce ss of SQ L Se rve r from having to re ad the data page s, flush the m out, and
re re ad the m again whe n the tim e arrive s. As you can im agine , this can re duce I/O for the se pinne d table s, boosting
SQ L Se rve r's pe rform ance .
To pin a table , the com m and DBC C PINTABLE is use d. For e x am ple , the script be low can be run to pin a table in SQ L
Se rve r:
http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

3/8

7/16/2014

DBCC Commands

DECLARE @db_id int, @tbl_id int


USE Northwind
SET @db_id = DB_ID('Northwind')
SET @tbl_id = OBJECT_ID('Northwind..categories')
DBCC PINTABLE (@db_id, @tbl_id)
W hile you can use the DBC C PINTABLE dire ctly, without the re st of the above script, you will find the script handy
be cause the DBC C PINTABLE's param e te rs re fe r to the database and table ID that you want to pin, not by the ir
database and table nam e . This script m ak e s it a little e asie r to pin a table . You m ust run this com m and for e ve ry
table you want to pin.
O nce a table is pinne d in the data cache , this doe s not m e an that the e ntire table is autom atically loade d into the
data cache . It only m e ans that as data page s from that table are ne e de d by SQ L Se rve r, the y are loade d into the
data cache , and the n stay the re , not e ve r be ing flushe d out to disk until you give the com m and to unpin the table
using the DBC C UNPINTABLE. It is possible that part of a table , and not all of it, will be all that is pinne d.
W he n you are done with a table and you no longe r want it pinne d, you will want to unpin your table . To do so, run
this e x am ple code :

DECLARE @db_id int, @tbl_id int


USE Northwind
SET @db_id = DB_ID('Northwind')
SET @tbl_id = OBJECT_ID('Northwind..categories')
DBCC UNPINTABLE (@db_id, @tbl_id)

DBCC PROCCACHE
Displays inform ation about how the store d proce dure cache is be ing use d.
Ex am ple :

DBCC PROCCACHE

DBCC REINDEX
Pe riodically (we e k ly or m onthly) pe rform a database re organization on all the inde x e s on all the table s in your
database . This will re build the inde x e s so that the data is no longe r fragm e nte d. Fragm e nte d data can cause SQ L
Se rve r to pe rform unne ce ssary data re ads, slowing down SQ L Se rve r's pe rform ance .
If you do a re organization on a table with a cluste re d inde x , any non-cluste re d inde x e s on that sam e table will
autom atically be re built.
Database re organizations can be done sche duling SQ LMAINT.EXE to run using the SQ L Se rve r Age nt, or if by running
your own custom script via the SQ L Se rve r Age nt (se e be low).
Unfortunate ly, the DBC C DBR EINDEX com m and will not autom atically re build all of the inde x e s on all the table s in a
database , it can only work on one table at a tim e . But if you run the following script, you can inde x all the table s in a
database with e ase .
Ex am ple :

DBCC DBREINDEX('table_name', fillfactor)


or

--Script to automatically reindex all tables in a database


USE DatabaseName --Enter the name of the database you want to reindex
DECLARE @TableName varchar(255)

http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

4/8

7/16/2014

DBCC Commands

DECLARE TableCursor CURSOR FOR


SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT "Reindexing " + @TableName
DBCC DBREINDEX(@TableName,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
The script will autom atically re inde x e ve ry inde x in e ve ry table of any database you se le ct, and provide a fillfactor of
90%. You can substitute any num be r you want for the 90 in the above script.
W he n DBC C DBR EINDEX is use d to re build inde x e s, k e e p in m ind that as the inde x e s on a table are be ing re built,
that the table be com e s unavailable for use by your use rs. For e x am ple , whe n a non-cluste re d inde x is re built, a
share d table lock is put on the table , pre ve nting all but SELEC T ope rations to be pe rform e d on it. W he n a cluste re d
inde x is re built, an e x clusive table lock is put on the table , pre ve nting any table acce ss by your use rs. Be cause of
this, you should only run this com m and whe n use rs don't ne e d acce ss to the table s be ing re organize d.

DBCC SHOWCONTIG
Use d to show how fragm e nte d data and inde x e s are in a spe cifie d table . If data page s storing data or inde x
inform ation be com e s fragm e nte d, it tak e s m ore disk I/O to find and m ove the data to the SQ L Se rve r cache buffe r,
hurting pe rform ance . This com m and te lls you how fragm e nte d the se data page s are . If you find that fragm e ntation
is a proble m , you can re inde x the table s to e lim inate the fragm e ntation. Note , this fragm e ntation is fragm e ntation
of data page s within the SQ L Se rve r MDB file , not of the physical file itse lf.
Since this com m and re quire s you to k now the ID of both the table and inde x be ing analyze d, you m ay want to run
the following script so you don't have to m anually look up the table nam e ID num be r and the inde x ID num be r.
Ex am ple :

DBCC SHOWCONTIG (Table_id, IndexID)


or

--Script to identify table fragmentation


--Declare variables
DECLARE
@ID int,
@IndexID int,
@IndexName varchar(128)
--Set the table and index to be examined
SELECT @IndexName = 'index_name'
SET @ID = OBJECT_ID('table_name')

--enter name of index


--enter name of table

--Get the Index Values


SELECT @IndexID = IndID
FROM sysindexes
http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

5/8

7/16/2014

DBCC Commands

WHERE id = @ID AND name = @IndexName


--Display the fragmentation
DBCC SHOWCONTIG (@id, @IndexID)
W hile the DBC C SHO W C O NTIG com m and provide s se ve ral m e asure m e nts, the k e y one is Scan De nsity. This figure
should be as close to 100% as possible . If the scan de nsity is le ss than 75%, the n you m ay want to re inde x the
table s in your database .

DBCC SHOW_STATISTICS
Use d to find out the se le ctivity of an inde x . Ge ne rally spe ak ing, the highe r highe r the se le ctivity of an inde x , the
gre ate r the lik e lihood it will be use d by the que ry optim ize r. You have to spe cify both the table nam e and the inde x
nam e you want to find the statistics on.
Ex am ple :

DBCC SHOW_STATISTICS (table_name, index_name)

DBCC SQLMGRSTATS
Use d to produce thre e diffe re nt value s that can som e tim e s be use ful whe n you want to find out how we ll caching is
be ing pe rform e d on ad-hoc and pre pare d Transact-SQ L state m e nts.
Ex am ple :

DBCC SQLMGRSTATS
Sam ple R e sults:

Item
Status
------------------------- ----------Memory Used (8k Pages)
5446
Number CSql Objects
29098
Number False Hits
425490
He re 's what the above m e ans:
Me m ory Use d (8k Page s): If the am ount of m e m ory page s is ve ry large , this m ay be an indication that som e
use r conne ction is pre paring m any Transact-SQ L state m e nts, but it not un-pre paring the m .
Num be r C Sql O bje cts: Me asure s the total num be r of cache d Transact-SQ L state m e nts.
Num be r False Hits: Som e tim e s, false hits occur whe n SQ L Se rve r goe s to m atch pre -e x isting cache d TransactSQ L state m e nts. Ide ally, this figure should be as low as possible .

DBCC SQLPERF()
This com m and include s both docum e nte d and undocum e nte d options. Le t's tak e a look at all of the m and se e what
the y do.

DBCC SQLPERF (LOGSPACE)


This option (docum e nte d) re turns data about the transaction log for all of the database s on the SQ L Se rve r,
including Database Nam e , Log Size (MB), Log Space Use d (%), and Status.

DBCC SQLPERF (UMSSTATS)


http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

6/8

7/16/2014

DBCC Commands

This option (undocum e nte d) re turns data about SQ L Se rve r thre ad m anage m e nt.

DBCC SQLPERF (WAITSTATS)


This option (undocum e nte d) re turns data about wait type s for SQ L Se rve r re source s.

DBCC SQLPERF (IOSTATS)


This option (undocum e nte d) re turns data about outstanding SQ L Se rve r re ads and write s.

DBCC SQLPERF (RASTATS)


This option (undocum e nte d) re turns data about SQ L Se rve r re ad-ahe ad activity.

DBCC SQLPERF (THREADS)


This option (undocum e nte d) re turns data about I/O , C PU, and m e m ory usage pe r SQ L Se rve r thre ad.

DBCC SQLPERF (UMSSTATS)


W he n you run this com m and, you ge t output lik e this. (Note , this e x am ple was run on a 4 C PU se rve r. The re is 1
Sche dule r ID pe r available C PU.)

Statistic
Value
-------------------------------- -----------------------Scheduler ID
0.0
num users
18.0
num runnable
0.0
num workers
13.0
idle workers
11.0
work queued
0.0
cntxt switches
2.2994396E+7
cntxt switches(idle)
1.7793976E+7
Scheduler ID
1.0
num users
15.0
num runnable
0.0
num workers
13.0
idle workers
10.0
work queued
0.0
cntxt switches
2.4836728E+7
cntxt switches(idle)
1.6275707E+7
Scheduler ID
2.0
num users
17.0
num runnable
0.0
num workers
12.0
idle workers
11.0
work queued
0.0
cntxt switches
1.1331447E+7
cntxt switches(idle)
1.6273097E+7
Scheduler ID
3.0
num users
16.0
num runnable
0.0
num workers
12.0
idle workers
11.0
http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

7/8

7/16/2014

work queued
cntxt switches
cntxt switches(idle)
Scheduler Switches
Total Work

DBCC Commands

0.0
1.1110251E+7
1.624729E+7
0.0
3.1632352E+7

Be low is an e x planation of som e of the k e y statistics above :


num use rs: This is the num be r of SQ L Se rve r thre ads curre ntly in the sche dule r.
num runnable : This is the num be r of actual SQ L Se rve r thre ads that are runnable .
num work e rs: This is the actual num be r of work e r the re are to proce ss thre ads. This is the size of the thre ad
pool.
idle work e rs: The num be r of work e rs that are curre ntly idle .
cntx t switche s: The num be r of conte x t switche s be twe e n runnable thre ads.
cntx t switche s (idle ): The num be r of conte x t switche s to the idle thre ad.

DBCC TRACEON & DBCC TRACEOFF


Use d to turn on and off trace flags. Trace flags are ofte n use d to te m porarily turn on and off spe cific se rve r be havior
or se rve r characte ristics. In rare occasions, the y can be use ful to trouble shooting SQ L Se rve r pe rform ance proble m s.
Ex am ple :
To use the DBC C TR AC EO N com m and to turn on a spe cifie d trace flag, use this syntax :

DBCC TRACEON (trace# [,...n])


To use the DBC C TR AC EO N com m and to turn off a spe cifie d trace flag, use this syntax :

DBCC TRACEOFF (trace# [,...n])


You can also use the DBC C TR AC ESTATUS com m and to find out which trace flags are curre ntly turne d on in your
se rve r using this syntax :

DBCC TRACESTATUS (trace# [,...n])


For spe cific inform ation on the diffe re nt k inds of trace flags available , se arch this we bsite or look the m up in Book s
O nline . [6.5, 7.0, 2000] More inform ation on SQ L Se rve r 7.0 trace flags, and m ore info on SQ L Se rve r 2000 trace
flags.

DBCC UPDATEUSAGE
The official use for this com m and is to re port and corre ct inaccuracie s in the sysinde x e s table , which m ay re sult in
incorre ct space usage re ports. Appare ntly, it can also fix the proble m of unre claim e d data page s in SQ L Se rve r. You
m ay want to conside r running this com m and pe riodically to cle an up pote ntial proble m s. This com m and can tak e
som e tim e to run, and you want to run it during off tim e s be cause it will ne gative ly affe ct SQ L Se rve r's pe rform ance
whe n running. W he n you run this com m and, you m ust spe cify the nam e of the database that you want affe cte d.
Ex am ple :

DBCC UPDATEUSAGE ('databasename')

http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html

8/8

S-ar putea să vă placă și