Blog Post

Toolbox - Which Tables are Using All of My Space?

,

This is the next in a new series of blogs I am going to create talking about useful tools (mostly scripts) that I use frequently in my day-to-day life as a production DBA.  I work as a Work-From-Home DBA for a Remote DBA company, but 90+% of my job functions are the same as any other company DBA.

Many of these scripts come directly from blogs and articles created and shared by other members of the SQL Server community; some of them I have slightly modified and some I have lifted directly from those articles.  I will always give attribution back to the original source and note when I have made modifications.

--

In the previous post in this series "Toolbox - Where Did All My Space Go?" I shared a script for finding which database files consumed the most space and which of those files had free space in them.  The next step after finding out which databases are using the space is determining which tables in those databases are occupying that space to consider purge/archive opportunities.  In many cases you will find a single large table (often called a "mother table") taking up most of the space in your database.

https://www.newslinq.com/wp-content/uploads/2014/05/table.png
(That's a *big* table!)

I found a script created by a developer from Switzerland in a question/answer on StackOverflow.com and modified it slightly to return the specifics I wanted.  Among other things I added the InstanceName and DatabaseName because in my job I frequently create documentation or reports for external clients who don't necessarily know the result set came from a particular instance and a particular database:

--

/*
Object Sizes
Modified from http://stackoverflow.com/questions/15896564/get-table-and-index-storage-size-in-sql-server
*/
SELECT TOP 50
@@SERVERNAME as InstanceName
, DB_NAME() as DatabaseName
, s.NAME AS SchemaName
, t.NAME  AS TableName
, SUM(p.rows) AS RowCounts
--, SUM(a.total_pages) * 8 AS TotalSpaceKB
, SUM(a.total_pages) * 8/1024.0 AS TotalSpaceMB
, SUM(a.total_pages) * 8/1024.0/1024.0 AS TotalSpaceGB
, SUM(a.used_pages) * 8/1024.0 AS UsedSpaceMB
, (SUM(a.total_pages) - SUM(a.used_pages)) * 8/1024.0 AS UnusedSpaceMB
FROM sys.tables t
INNER JOIN sys.schemas s
ON s.schema_id = t.schema_id
INNER JOIN sys.indexes i
ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p
ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a
ON p.partition_id = a.container_id
WHERE t.NAME NOT LIKE 'dt%'    -- filter out system tables for diagramming
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY t.Name
, s.Name
ORDER BY TotalSpaceMB DESC

--

The results look like this:

--

InstanceNameDatabaseNameSchemaNameTableNameRowCountsTotalSpaceMBTotalSpaceGBUsedSpaceMBUnusedSpaceMB
Instance01Database15SI_USERTRANS_DATA1773015016263.7215.8816234.1429.58
Instance01Database15SI_USERWORKFLOW_CONTEXT507856807623.277.447622.520.75
Instance01Database15PTMEI_HISTORY_MSG227045433701.593.613701.190.40
Instance01Database15SI_USERWORKFLOW_LINKAGE726439082657.212.592657.060.15
Instance01Database15SI_USERCORRELATION_SET137622842542.872.482542.590.27
Instance01Database15SI_USERDOCUMENT98336161445.551.411445.320.23
Instance01Database15PTMEI_HISTORY_TRANDTL306736801257.231.231256.990.24
Instance01Database15SI_USERACT_SESSION_GUID187281141246.771.221246.700.07
Instance01Database15SI_USERDATA_FLOW_GUID13635838908.080.89907.980.10
Instance01Database15PTMEI_HISTORY_TRAN18560608699.970.68699.730.24
Instance01Database15SI_USERDATA_TABLE174048460.910.45460.450.46
Instance01Database15SI_USERDOCUMENT_EXTENSION1630855363.540.36363.150.39
Instance01Database15SI_USERACT_NON_XFER1579422284.480.28284.130.35
Instance01Database15SI_USERACT_XFER804270217.980.21217.610.38
Instance01Database15SI_USERACT_SESSION1008875209.040.20208.660.38
Instance01Database15SI_USERARCHIVE_INFO4203976113.340.11113.100.24
Instance01Database15SI_USERWF_INST_S1061373101.520.10101.370.16
Instance01Database15SI_USERACT_AUTHORIZE29890870.270.0770.110.16
Instance01Database15SI_USERDATA_FLOW42093056.590.0656.350.23
Instance01Database15SI_USERACT_AUTHENTICATE26920045.800.0445.310.48
Instance01Database15SI_USEREDIINTDOC18267243.830.0443.690.14
Instance01Database15SI_USERMSGMDNCORRELATION7465627.860.0326.421.44
Instance01Database15SI_USEREDI_DOCUMENT_STATE5749822.190.0218.213.98
Instance01Database15SI_USERENVELOPE_PARMS13469119.500.0219.340.16
Instance01Database15SI_USERSAP_TID8159814.130.0114.000.13
Instance01Database15PTMEI_PARTNER_REPORT7461713.390.0113.380.02
Instance01Database15SI_USEREDI_ELEMENT_CODES8958310.630.0110.550.08
Instance01Database15SI_USEREDI_COMPLIANCE_RPT3750010.230.019.520.70
Instance01Database15SI_USERDOCUMENT_LIFESPAN2945410.100.018.441.66
Instance01Database15SI_USERACTIVITY_INFO432696.000.015.700.30
Instance01Database15SI_USERYFS_USER_ACT_AUDIT140254.900.004.180.72
Instance01Database15SI_USERBPMV_LS_WRK2191104.200.002.701.50
Instance01Database15SI_USERCODELIST_XREF_ITEM143323.500.002.760.74
Instance01Database15SI_USERDOC_STATISTICS89483.430.003.010.42
Instance01Database15SI_USERMAP48482.370.002.260.11
Instance01Database15SI_USERYFS_RESOURCE56281.980.001.820.16
Instance01Database15SI_USERMDLR_PAL_ITEM_DESC47671.380.001.350.03
Instance01Database15SI_USERSERVICE_PARM_LIST62901.280.001.120.16
Instance01Database15SI_USERADMIN_AUDIT21001.150.000.760.39
Instance01Database15SI_USERDOC_STAT_KEY28601.080.000.820.26
Instance01Database15SI_USERMAPPER_ERL_XREF43171.070.001.020.05
Instance01Database15PTMEI_MSG_PROFILE48300.890.000.760.13
Instance01Database15SI_USERWF_INST_S_WRK27080.860.000.780.08
Instance01Database15SI_USERYFS_ORGANIZATION9090.840.000.380.46
Instance01Database15SI_USERYFS_STATISTICS_DETAIL7920.830.000.480.35
Instance01Database15SI_USERRESOURCE_CHECKSUM58270.760.000.730.02
Instance01Database15SI_USERYFS_RESOURCE_PERMISSION16110.730.000.560.16
Instance01Database15PTMEI_COMM_PROFILE_AUDIT15120.720.000.630.09
Instance01Database15SI_USERWFD34060.720.000.630.09
Instance01Database15SI_USERXMLSCHEMAS16780.700.000.690.01

--

This allows you to easily find the largest tables (you can modify the ORDER BY to find the tables with the most free space as well to look for inefficient indexing or design).

Once you have the largest tables in hand you have the starting point for a discussion on potential purges or archives.

Hope this helps!

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating