Search This Blog

January 12, 2012

How to know the space used by each table in SQL SERVER and generate summary report

I want to know the following things.

How many rows are present in each table in a database
What is the index size of each table
What is the data size of each table.
Total how many rows are present in all the tables in a database.
Total hom much space is used by data and index by all tables in a database.

In SQL Server sp_spaceused stored procedure is used to know the size and rows count for a single table. If you want to know the summary of all tables you need to use a cursor to loop all the tables.

We can use sp_MSforeachtable stored procedure to execute a command. But this will give you resultset for each execution.

Combined these two commands in the following way you can get a summary of each table in the current database.

This will help us in understanding Production data sizes and plan for the capacity planning.


/*Declare temp table to hold the data*/
DECLARE @t TABLE(Name NVARCHAR(MAX),
Rows BIGINT,
Reserved NVARCHAR(MAX),
Data NVARCHAR(MAX),
Index_Size NVARCHAR(MAX),
Unused NVARCHAR(MAX))

/*Insert the data output from sp_spaceused command to temp table */
insert into @t
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

/*Display the raw data for all the tables*/
select * from @t order by name

/*Remove the 'KB' present in the data*/
update @t
Set reserved = RTRIM((REPLACE(reserved,'KB','')))
,data = RTRIM((REPLACE(DATA,'KB','')))
,index_size = RTRIM((REPLACE(index_size,'KB','')))
,unused = RTRIM((REPLACE(unused,'KB','')))

/* Get the total of all tables data to know the summary*/
SELECT 'Total' AS TOTAL
, SUM(rows) AS 'Rows'
, SUM(CONVERT(BIGINT, reserved)) AS 'Reserved (KB)'
, SUM(CONVERT(BIGINT, data)) AS 'Data (KB)'
, SUM(CONVERT(BIGINT, index_size)) AS 'Index Size (KB)'
, SUM(CONVERT(BIGINT, unused)) AS 'Unused (KB)'
FROM @t



January 06, 2012

Free SQL training by Microsoft on Feb 25 2012 (Saturday) in Redmond WA USA

Microsoft is conducting free SQL & Data warehouse training in Redmond for one day.

You can register this event online at the following location

SQL Saturday

Event Date: 25th February 2012 Saturday

Location: 15255 NE 40th Street, North Commons, Redmond, WA 98052

For Topics and Schedule check the link Schedule

For future events and locations, check the following link

Future Events

January 05, 2012

Creating Bing Maps Developer Account

Go to Bing Maps Portal Site and click on Create link.
bingmapsportal

Click on "Create or View Keys" after creating/login to bingmapsportal site.

Provide the application name and the url.

Use the generated Credentials in developing the applications.

For additional resources, you can check the following link

Bing Maps

January 04, 2012

Microsoft SQL Server 2012 RC0 released

SQL Server 2012 is a cloud-ready information platform that will help organizations unlock breakthrough insights across the organization and quickly build solutions to extend data across on-premises and public cloud backed by mission critical confidence.

You can download SQL SERVER 2012 RC0 from the following link.

Download Sql server 2012 RC0




Developer Tools are available at the following location.

Developer Tools