Blog Post

Toolbox - Why is My Database So Big????

,

I have written previously (here) about how to tell which database files have free space in them when your drive starts to fill.

What if all of your database files are full and you are still running out of space?

DRIVEDATABASE
NAME
FILENAMEFILETYPEFILESIZESPACEFREEPHYSICAL_NAME
FDB03DB03_MDFDATA35.47 GB225.63 MBF:\MSSQL\Data\DB03_Data.mdf
FDB02DB02DataDATA110.25 MB92.38 MBF:\MSSQL\Data\DB02.mdf
FDB01DB01DataDATA142.06 MB72.69 MBF:\MSSQL\Data\DB01.mdf
FDB05DB05_DataDATA35.72 GB71.44 MBF:\MSSQL\Data\DB05_Data.mdf
FDB06DB06_MDF1DATA36.47 GB58.50 MBF:\MSSQL\Data\DB06_Data.mdf
FDB04DB04DataDATA36.47 GB38.00 MBF:\MSSQL\Data\DB04_Data.mdf
http://www.joemartinfitness.com/wp-content/uploads/2013/11/Post-holiday-bloat.jpg

The next step is to see what is taking up the space in the individual databases.  Maybe there's an audit table that never gets purged...or a Sales History that can be archived away, it only there were an archive process...

https://s3.amazonaws.com/lowres.cartoonstock.com/business-commerce-work-workers-employee-employer-staff-ear0117_low.jpg

**ALWAYS CREATE AN ARCHIVE/PURGE PROCESS ** #ThisIsNotAnItDepends

--

There is an easy query to return the free space available in the various tables in your database - I found it in an StackOverflow at https://stackoverflow.com/questions/15896564/get-table-and-index-storage-size-in-sql-server and then modified it somewhat to make the result set cleaner (to me anyway):

--

/*
Object Sizes

Modified from http://stackoverflow.com/questions/15896564/get-table-and-index-storage-size-in-sql-server
*/

SELECT 
@@SERVERNAME as InstanceName
, DB_NAME() as DatabaseName
, ISNULL(s.name+'.'+t.NAME, '**TOTAL**')  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 s.name+'.'+t.Name
WITH ROLLUP
ORDER BY TotalSpaceMB DESC

--

Running the query against an individual database will return all of the tables in the database and their sizes, as well as the total size of the database (from the ROLLUP):

--

InstanceNameDatabaseNameTableNameRowCountsTotalSpaceMBUsedSpaceMBUnusedSpaceMB
Instance01Database99**TOTAL**14840959036,042.06332,890.9223151.141
Instance01Database99dbo.BusinessRulesAuditing2068628017,877.81317,860.93816.875
Instance01Database99dbo.BusinessRulesRuleSet188403,895.766877.9453,017.820
Instance01Database99dbo.ModelWorkbookVersionExt43831,818.4531,806.44512.008
Instance01Database99dbo.EquityOutputVersions350403621,746.6881,739.2817.406
Instance01Database99dbo.NominalOutputs3592710258.305251.0477.258
Instance01Database99dbo.Auditing17349433.39127.5865.805
Instance01Database99dbo.EquityOverrides13105402515.859511.1884.672
Instance01Database99dbo.ContingentOutputs1371465364.641361.7192.922
Instance01Database99dbo.ContingentWSStaging292907333.320329.3593.961
Instance01Database99dbo.TransformedAdminContingent58584876.07073.0003.070

--

There are two different useful cases I normally find with this result set.

The first (highlighted in aqua) is the very large table.  In this sample my 35GB database has one large 17GB table.  This is a situation where you can investigate the table and see *why* it is so large.  In many cases, this is just the way it is - sometimes one large "mother" table is a fact of life (You take the good, you take the bad, you take them both, and there you have..."

https://memegenerator.net/img/instances/11140850/god-im-old.jpg

Often though you will find that this table is an anomaly.  As mentioned above, maybe there is a missing purge or archive process - with a very large table look at the table definitions to see if their are date/datetime columns, and then select the top 10 order by those columns one by one to see how old the oldest records are.  You may find that you are storing years of data when you only need months and can purge the oldest rows.  You may also find that even though you do need years of data, you may not need them live in production, which allows you to periodically archive them away to another database (maybe even another instance) where they can be accessed only when needed.

https://blog.parse.ly/wp-content/uploads/2015/05/say_big_data.jpg

--

The second case (highlighted in yellow) is a table with significant free space contained in the table itself.  In this example a 3.7GB table has 3.0GB of free space in it!

How do you account for this?  There are a few ways - when you delete large numbers of rows from the table, the space usually isn't released until the next time the related indexes are rebuilt.  Another possibility is index fill factor - the amount of free space that is included in indexes when they are rebuilt.  I have run into several instances where a client DBA misunderstood the meaning of the fill factor and did a reverse to themselves, setting the fill factor to 10 thinking it would leave 10% free space when in fact it left 90% free space, resulting not only in exceeding large indexes but also in very poor performance as the SQL Server needs to scan across many more pages to retrieve your data.

To help determine which index(es) may contribute to the problem, you can add the indexname to the query to break the data out that one more level:

--

/*
Object Sizes With Indexes

Modified from http://stackoverflow.com/questions/15896564/get-table-and-index-storage-size-in-sql-server
*/

SELECT 
@@SERVERNAME as InstanceName
, DB_NAME() as DatabaseName
, ISNULL(s.name+'.'+t.NAME, '**TOTAL**')  AS TableName
, ISNULL(i.Name, '**TOTAL**')  AS IndexName
, 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 s.name+'.'+t.Name
, i.Name
WITH ROLLUP

--

InstanceNameDatabaseNameTableNameIndexNameRowCountsTotalSpaceMBUsedSpaceMBUnusedSpaceMB
Instance01Database99**TOTAL****TOTAL**14840959036,042.06335,890.922151.141
Instance01Database99BusinessRulesAuditingPK__BusinessRules2068628017,877.81317,860.93816.875
Instance01Database99BusinessRulesAuditing**TOTAL**2068628017,877.81317,860.93816.875
Instance01Database99BusinessRulesRuleSetIX_RuleSet1188402,100.000137.0001,963.000
Instance01Database99BusinessRulesRuleSetIX_RuleSet218840190.000180.00010.000
Instance01Database99BusinessRulesRuleSetPK_RuleSet188401,600.000560.0001,040.000
Instance01Database99BusinessRulesRuleSet**TOTAL**565203,895.766877.9453,017.820
Instance01Database99EquityOutputVersionsPK_EquityOutputVersions350403621,746.6881,739.2817.406
Instance01Database99EquityOutputVersions**TOTAL**350403621,746.6881,739.2817.406
Instance01Database99EquityOverridesPK_EquityOverrides13105402515.859511.1884.672
Instance01Database99EquityOverrides**TOTAL**13105402515.859511.1884.672
Instance01Database99AuditingPK_Auditing17349433.39127.5865.805
Instance01Database99Auditing**TOTAL**17349433.39127.5865.805

<result set snipped for space>

--

In this case you could look at the fill factor of the IX_RuleSet1 and PK_RuleSet indexes to see why there is so much free space.  Failing that it is possible that these indexes need to be rebuilt to release that space, possibly after a large delete from the table cleared that space.

--

I find I use this query more than I ever would have thought with space issues - I start with the Database Free File query (from the previous post) and then move next to this query.

--

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