Академический Документы
Профессиональный Документы
Культура Документы
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 :
3/8
7/16/2014
DBCC Commands
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 :
http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html
4/8
7/16/2014
DBCC Commands
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 :
5/8
7/16/2014
DBCC Commands
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 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.
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.
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
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 :
http://www.sapientier.com/FlexWiki/default.aspx/OdsWiki/DBCCCommands.html
8/8