0% found this document useful (0 votes)
125 views4 pages

Find Size of SQL Server Tables and Other Objects With Stored Procedure

The document describes a stored procedure called sp_SOS that provides an expanded functionality over sp_spaceused for determining the size of SQL Server database objects. Sp_SOS takes parameters like database name, schema name, object name, object type to return disk space used by objects. It orders the results and calculates totals. Screenshots demonstrate its use on SQL Server 2000, 2005 and 2008 to get breakdown of space used by tables, indexes and other objects.

Uploaded by

divandann
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views4 pages

Find Size of SQL Server Tables and Other Objects With Stored Procedure

The document describes a stored procedure called sp_SOS that provides an expanded functionality over sp_spaceused for determining the size of SQL Server database objects. Sp_SOS takes parameters like database name, schema name, object name, object type to return disk space used by objects. It orders the results and calculates totals. Screenshots demonstrate its use on SQL Server 2000, 2005 and 2008 to get breakdown of space used by tables, indexes and other objects.

Uploaded by

divandann
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Find size of SQL Server tables and other objects with stored procedure

Richard Ding 04.09.2008 Rating: -4.38- (out of 5)


Sp_spaceused is a stored procedure that ships with SQL Server and is used to display the disk space taken by a SQL Server object. Very often, I find it inadequate to eet y intentions. !or instance, when I want an overview of user table si"es in a #iven SQL Server database, or wish to see the top $% bi##est inde&ed objects or need to su ari"e the total space owned by a #roup of tables, sp_spaceused is of no use. So, I created stored procedure sp_S'S, an e&panded version of sp_spaceused, which can be used to calculate SQL Server object space and to perfor other functions. (ith sp_S'S I wanted to retain the core functionality of sp_spaceused )) i.e., the al#orith to su up data, inde& and reserved and unused spaces for an object. I knocked out the database si"in# part and will co e up with a separate stored procedure solely for a database si"e report.

Click here to do nload !i"ting #: $he co%&lete $-'(! definition for "&)'*'. '&)'*' take" eight input parameters +riefl, de"cri+ed in $a+le #. Variable -D+.a%e -'che%a.a%e -*+2ect.a%e -$o&Clau"e -*+2ect$,&e Data type ","na%e ","na%e ","na%e Nullable Default Default implication /e" /e" /e" .0!! Current data+a"e .0!! 1ll "che%a" 3 4ncluding all o+2ect" in 5!4675 clau"e .0!! 1ll o+2ect". Can +e 5$*9 .5 or 5$*9 . 97RC7.$5 1ll o+2ect" that can +e "i:ed. ;alid 8alue" are '(","te%)< 0(u"er)< ;(inde=ed 8ie )< '(("er8ice .0!! +roker >ueue)< 4$(internal ta+le) or an, co%+ination of the% .0!! 4nclude" internal ta+le. $he 9arent e=clude" it in "i:e ?, o+2ect na%e< can +e an, "i:e related colu%n. ;alid .0!! "hort ter%" are .(na%e)< R(ro )< $(total)< 0(u"ed)< 4(inde=)< D(data)< @(free or unu"ed) and /(t,&e) 0 Do not run 5D?CC 09D1$70'1A75

n8archar(20) /e" n8archar(50) /e" /e"

-'ho 4nternal$a+le n8archar(3) -*rder?, -0&date0"age

n8archar(#00) /e" +it /e"

Table ! 9ara%eter 8aria+le" for "&)'*' and their characteri"tic".


I prefer to use a for ula)like style that akes it easy to interpret the fi#ure relationships.

*he for ula is "Total(MB) - Unused(MB) == Used(MB) = Index(MB) + Data(MB) +, i.e., the used space is the si ultaneous result of total inus unused as well as inde& plus data. *he +reserved+ colu n in sp_spaceused is actually equivalent to the total si"e in sp_S'S. *here,s a new concept of the internal table in SQL Server -%%. and SQL Server -%%/. *hey are inter ediate posredni0ka tables for parent objects to process 12L pri ary inde&es, Service 3roker queues, fullte&t inde&es and query notification subscriptions. (hen su in# up parent objects, the internal tables of type -%- 4& l_inde&_nodes5 and -%6 4fullte&t_catalo#_ ap5 should be co puted to add to the total si"e. 3ecause of this, I desi#ned a te porary base object table 4773'5 in sp_S'S in two parts. *he left half co prises si& colu ns representin# the dau#hter objects. *he ri#ht half is for the parent objects. (hen a parent owns a dau#hter object, it shows different sche a, sche a id, object na e and object id. 'therwise, the na es and ids are identical. (hen an internal table is shown, their parent na e is included in parenthesis for clear correlation. *he object type initials are consistent with 2icrosoft SQL Server 3ooks 'nline. *hey can be syste table

4S5, user table 485, view 4V5, service queue 4SQ5 or internal table 4I*5. 9ach type is separated by space4s5, co a4s5 or se icolon4s5. *he nu ber or order of the separators does not atter. If you provide characters other than those allowed, sp_S'S will raise an error and quit. :nd the stored procedure is 8nicode)co patible and case)insensitive. Sp_S'S works on SQL Server -%%%, -%%. and -%%/. In SQL Server -%%%, values reported by sp_S'S ay not be updated. *he ;8pdate8sa#e para eter can dictate a +<3== 8><:*98S:?9+ pre)run to ensure the values are current in the followin# report. 3ut be cautious as it could i pact perfor ance in lar#e databases. Startin# in SQL Server -%%., sp_spaceused always reports correct fi#ures, which akes the <3== co and obsolete. @ere are several typical scenarios to illustrate how to use sp_S'SA ;<bBa e is the database na e where you want to find object space in SQL Server. If it,s not supplied, it will use the current database. !or e&a ple, if you want a quick overview of space taken by all database objects in :dventure(orks database, you ay run the *)SQL state ent in Listin# -A 8S9 AdventureWorks; EXEC dbo.sp_SOS; Listing 2: 'verview of all object space in :dventure(orks database. Bote all para eters are by default. *he result displays all objects owned by all sche as in alphabetical order. 3oth ;Sche aBa e and ;'bjectBa e can take wildcard C as the default. *his allows you to su up a #roup of objects that bear a si ilar pattern in na es or owner na es. (hile we are still usin# :dventure(orks as the sa ple database, we want to list all Sales sche a)owned objects that look like +Sales+ in the order of used disk space. (e don,t want to show internal tables yet. I like to run +<3== 8><:*98S:?9+ first to update any incorrect values. *he *)SQL state ent looks like this in Listin# D. sp_S'S 'AdventureWorks', 'Sales%', 'Sales%', NULL, ' S ,;u v ;!";', 'no', 'U', # Listing 3A List objects owned by Sales sche a and na ed like +Sales+, in a descendin# order of used disk space. Si ilar to Listin# D, in Listin# 6 the co and can be run to retrieve the si"e infor ation for a particular object. Bote that the si"es account for the discrepancy between that displayed on the object properties ?8I and that returned by either sp_S'S or sp_spaceused. Bote, too, that the supple ented parent na e is placed behind the 12L inde& to show its affiliation. sp_S'S 'AdventureWorks', NULL, '$%l_!nde$_nodes_&'()*+#,#_&-'''', NULL, '."', '/es', 'N', ' Listing : =heck space for a specified object. Since it,s an internal table, the sche a na e is i#nored. 'bviously one object does not need to be ordered, so the ;'rder3y is skipped too. 3elow are two screenshots for sp_S'S e&ecutin# in one of y ana#ed SQL Server -%%% databasesA

Eou can see two #roups of tables, :F=@IV9<_@IS*'FE and @IS*'FE sets of tables in !i#ure $. I often have to #et the #rand total of the @IS*'FE tables. (ith sp_S'S, I can su up the values at the botto of the result. !i#ure - shows the details of <3== 8><:*98S:?9 for each table involved. *he arrow 4G GH5 indicates the actual co ands followed by updatin# details.

Fi"ure ! "&)'*' "ho " the grou&ing of u"er ta+le" that ha" "i%ilar na%e" and the "u+total "&ace for that grou& in a '(! 'er8er 2000 u"er data+a"e. (Click on image for enlarged view.)

Fi"ure #! '(! 'er8er 2000 (uer, 1nal,:er "ho " the "&)'*' "ni&&et and detailed re"ult of 5D?CC 09D1$70'1A75 co%%and. 4n thi" ca"e< the %a2orit, of o+2ect" need to u&date their "i:e infor%ation. (Click on image for enlarged view.) !a"tl,< hereB" ho "&)'*' ork" on '(! 'er8er 2008 C$9. 0"ing the "cri&t in !i"ting 5< e o+tained the re"ult "ho n in @igure 3.
EXEC dbo.sp_SOS 01bNa%e 2 N'AdventureWorks-''3', 0S45e%aNa%e 2 NULL, 0Ob6e4tNa%e 2 NULL, 0"opClause 2 NULL, 0Ob6e4t"/pe 2 N'U7."S ', 0S5o8.nternal"able 2 N'9es', 0Order:/ 2 N'9', 0UpdateUsa;e 2 #

Listin" $! Rank all u"er ta+le"< 8ie "< internal ta+le" and "er8ice >ueue" fro% 1d8entureCork"2008 data+a"e on '(! 'er8er 2008 C$9 in the order of o+2ect t,&e". Run D?CC 09D1$70'1A7 +efore finali:ing the re&ort. 'ho internal o+2ect" a" ell.

Fi"ure %! "&)'*'< co%&ati+le ith '(! 'er8er 2008 C$9< run" in 1d8entureCork"2008 data+a"e to "ho a li"t of all o+2ect" ordered +, o+2ect t,&e". (Click on image for enlarged view.) 4 canBt "ho ,ou all in"tance" of differentl, &ara%eteri:ed "&)'*' e=ecution". 4f ,ou find "o%ething intere"ting or conflicting ith it" e=&ected +eha8ior< &lea"e dro& %e a line and 4Bll "ee if 4 can i%&ro8e it. Check out %, follo -u& ti& on the "tored &rocedure "&)'D'. .ot onl, ill "&)'D' deter%ine 5'(! Data+a"e '&ace<5 +ut it can al"o +e u"ed to %onitor data+a"e gro th< alert a D?1 on data or log file gro th< e=ecute a tran"action log +acku& and e8en &ro8ide a detailed +reakdo n at the file le8el "o a D?1 can then "hrink file" ith the %o"t e%&t, "&ace. &'()T T*+ &)T*(,! data+a"e ad%ini"trator at .orthea"tern 0ni8er"it, in ?o"ton< ha" orked ith '(! 'er8er "ince the late #990". Di" intere"t" co8er data+a"e ad%ini"tration< $-'(! de8elo&%ent< di"a"ter reco8er,< re&lication and &erfor%ance tuning. De rite" for "e8eral %aga:ine" for the '(! 'er8er &roduct< including SQL Server Magazine and SQL Server Standard. De i" a freelancer at 'earch'(!'er8er.co% and a %e%+er of 8ariou" '(! 'er8er online foru%". De"&ite an earlier degree in %edicine and a doctorate in ?io 'cience< Richard a&&arentl, en2o," +eing a creati8e '(! 'er8er &rofe""ional. Contact Richard Ding at rding-rcn.co%. -opyri"ht #../ $ech$arget
,ichard Din"<

You might also like