![]() |
|
|
|
By: Greg Robidoux | Read Comments (11) | Related Tips: 1 | 2 | 3 | More > Monitoring |
Problem
One thing that is often handy to know is how much space your tables are using within your database. It is helpful to know the number of rows, the data space used as well as the index space used. There are several ways that you can get this information, by reading the system tables, using the built-in reports in SQL Server 2005 or by using the Taskpad view in SQL Server 2000, but what is a simple way of retrieving this data without using the GUI or without having to write a query to gather this data from the system tables?
Solution
All the data that is retrieved for SQL Server Management Studio and SQL Server Enterprise Manager is pulled from queries that access the data in the system tables. In a previous tip, How does Enterprise Manager get its data we took a look at how to use Profiler or Trace to find out what is occurring under the covers. For this tip, basically the same thing was done where we capture the query that SQL Server is using and then modified it to meet our needs.
SQL Server 2000 or 2005
Here is a script that can be used for SQL Server 2000 or SQL Server 2005. This is a modified version of what SQL Server Management Studio uses for SQL Server 2005. The process creates a few temporary tables, determines a list of the tables, and then uses sp_spaceused to gather the stats into a temporary table and then outputs the results.
DECLARE @table_name VARCHAR(500) |
SQL Server 2005
Here is another version of this same process. The overall process is the same, but it uses the new tables in SQL Server 2005. It also uses the Try Catch processing which was discussed in this previous tip, SQL Server 2005 - Try Catch Exception Handling. Both of the examples will produce the same ouput.
BEGIN try |
Here is some sample output after running this against the AdventureWorks database.

Next Steps
| Thursday, April 02, 2009 - 11:00:37 AM - JeffSkvorc | Read The Tip |
|
Hey, great tip! It helped a lot. I made a minor mod below which simply rips out the ' KB' and casts the data as INT. Then, I sort the list by Reserved DESC since I am mostly trying to find which tables are taking up the most space. I also wrapped into a SPROC for easy access. Here is the changes below. Again, thank you. Saved me some time... SET ANSI_NULLS ON -- Insert statements for procedure here INSERT INTO @tab1 DECLARE c1 CURSOR FOR OPEN c1; -- make sure the object exists before calling sp_spacedused SELECT END try |
|
| Wednesday, March 10, 2010 - 8:57:42 AM - smarch | Read The Tip |
|
Or how about ... ?
-- Show the size of all the tables in a database set @cmd1 = exec sp_msforeachtable @command1=@cmd1 |
|
| Monday, June 21, 2010 - 1:35:28 PM - dpaproskijr | Read The Tip |
|
Here's another version. The original didn't work properly with repeated table names (i.e. schema1.table, schema2.table would result in more than 2 rows of output). This one also sorts by descending size. BEGIN try |
|
| Monday, January 30, 2012 - 1:06:58 PM - Vojtech Machacek | Read The Tip |
|
This is briliant I have to say. Im just wondering if I can create procedure with will save those data into regular table with date of execution - like snapshot in time and save file sizes as numbers. Then I can make charts how DB was progressing and very nice other stuff. Can anybody help we with this task, Im not really database guy :(
|
|
| Monday, January 30, 2012 - 1:39:24 PM - Vojtech Machacek | Read The Tip |
|
Well, I tried my best and this is what I came up with:
USE SECREADER
It work with table
CREATE TABLE [dbo].[DbStatistics](
Maybe somebody will find it usefull, Maybe Im missing something by saving those data... |
|
| Monday, March 26, 2012 - 5:59:55 AM - Nazer Mohamed | Read The Tip |
|
http://nazermohamed.blogspot.co.uk/2012/03/quick-dump-of-all-table-sizes-of.html
|
|
| Wednesday, June 13, 2012 - 6:05:04 AM - steve | Read The Tip |
|
set nocount on declare @objname sysname select top 1 @objname = '[' + s.name + '].[' + o.name + ']' while @@ROWCOUNT > 0 if @objtype not in ('SQL_STORED_PROCEDURE','SQL_INLINE_TABLE_VALUED_FUNCTION','PRIMARY_KEY_CONSTRAINT','SQL_TABLE_VALUED_FUNCTION','SQL_SCALAR_FUNCTION','UNIQUE_CONSTRAINT') end update @spaceused select * |
|
| Thursday, August 16, 2012 - 4:55:42 PM - PraveenYamani | Read The Tip |
|
We can get this result like this too.
DECLARE @SpaceUsed TABLE( TableName VARCHAR(100) DECLARE @TabCount INT
|
|
| Thursday, August 16, 2012 - 5:07:38 PM - PraveenYamani | Read The Tip |
|
OR Simply use this.. DECLARE @SpaceUsed TABLE( TableName VARCHAR(100) |
|
| Thursday, August 16, 2012 - 5:08:59 PM - PraveenYamani | Read The Tip |
|
Oops! Use this..
|
|
| Monday, November 05, 2012 - 2:10:38 AM - Rendy | Read The Tip |
|
Thanks very much for the script!I'm a dummy in SQL and the script is very useful! Now i can find out which table is using the most space with ease! |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |