Getting the Disk Usage Statistics for all Tables in a Database

Getting the Disk Usage Statistics for all Tables in a Database

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0
I know, there are standard reports and other GUI tools for doing this task for SQL Server 2005 or later but what about when you need to extract such information from a SQL Server 2000 instance or when you just want to write some code? 🙂

The undocumented stored procedure sp_msforeachtable is here to help you (along with the stored procedure sp_spaceused)!

Here’s how:

–Select the database to be scanned for table disk usage
USE [DATABASE_NAME]
GO

–Create temporary table 1 – Sizes will be in strings/KB
create table #tmpSizes(
[name] nvarchar(200),
[rows] varchar(50),
reserved varchar(50),
data varchar(50),
index_size varchar(50),
unused varchar(50)
)

–Create temporary table 1 – Sizes will be in KB
create table #tmpSizesFinalKB(
[name] nvarchar(200),
[rows] int,
reserved int,
data int,
index_size int,
unused int
)

–Get the disk usage per table and store in temp table 1
INSERT INTO #tmpSizes
EXEC sp_MSforeachtable @command1=”sp_spaceused ‘?'”

–Indirect casting and copying of the information in temp table 2
–This is only when you want to store the disk usage statistics
–in a form that allows sorting operations etc.  
insert into #tmpSizesFinalKB
select 
[name],
replace([rows],’KB’,”),
replace(reserved,’KB’,”),
replace(data,’KB’,”),
replace(index_size,’KB’,”),
replace(unused,’KB’,”)
from #tmpSizes

–Access the disk usage results (in KB)
select * 
from #tmpSizesFinalKB
order by reserved desc

Now you can easily manipulate the disk usage statistics in temp table #tmpSizesFinalKB in order to represent them in the form you may like!

Useful? Please comment/like/tweet!


My Latest Projects:



Recommended eBooks on SQL Server:

Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.