Solved

calculate space used by each table in a database

Posted on 2002-05-01
5
684 Views
Last Modified: 2011-09-20
How to find out space used by each table in a database.
I know we can use sp_spaceused "tablename" but this gives for only one table at a time is there any way to get for all the tables at a time.

Thanks,

hmk
0
Comment
Question by:hmk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6984661
hmk,

you can use the following query to get information about all the tables in the database ( I just extracted this piece of code from sp_spaceused :-) Please let me know if it solves your purpose.

You can even convert this into a stored proc.

Thanks.

                select  name = o.name,
                        iname = i.name,
                        low = d.low,
                        rowtotal = rowcnt(i.doampg),
                        reserved = convert(numeric(20, 9),
                                (reserved_pgs(i.id, i.doampg) +
                                reserved_pgs(i.id, i.ioampg))),
                        data = convert(numeric(20, 9), data_pgs(i.id, i.doampg)),
                        index_size =  convert(numeric(20, 9),
                                data_pgs(i.id, i.ioampg)),
                        unused = convert(numeric(20, 9),
                                ((reserved_pgs(i.id, i.doampg) +
                                reserved_pgs(i.id, i.ioampg)) -
                                (data_pgs(i.id, i.doampg) +
                                data_pgs(i.id, i.ioampg))))
                into #pagecounts
                from sysobjects o, sysindexes i, master.dbo.spt_values d
                                where i.id = o.id
                                        and o.type = "U"
                                        and d.number = 1
                                        and d.type = "E"
 
  select distinct name,
                    rowtotal = convert(char(11), sum(rowtotal)),
                    reserved = convert(char(15), convert(varchar(11),
                               convert(numeric(11, 0), sum(reserved) *
                               (low / 1024))) + " " + "KB"),
                    data = convert(char(15), convert(varchar(11),
                               convert(numeric(11, 0), sum(data) * (low / 1024)))
                               + " " + "KB"),
                    index_size = convert(char(15), convert(varchar(11),
                                convert(numeric(11, 0), sum(index_size) *
                                (low / 1024))) + " " + "KB"),
                    unused = convert(char(15), convert(varchar(11),
                                convert(numeric(11, 0), sum(unused) *
                                (low / 1024))) + " " + "KB")
                from #pagecounts
 
group by name
order by 1
 
drop table #pagecounts
0
 

Author Comment

by:hmk
ID: 6986083
Excellent, I got it.
But I have one more question. How to get total space used/allocated/free for the database in the same above statements. If we sum up data+index_size gives the total space used by database.

Thanks,

hmk
0
 
LVL 5

Accepted Solution

by:
amitpagarwal earned 100 total points
ID: 6986756
here is what is want..

 
set nocount on
 
               select  name = o.name,
                       iname = i.name,
                       low = d.low,
                       rowtotal = rowcnt(i.doampg),
                       reserved = convert(numeric(20, 9),
                               (reserved_pgs(i.id, i.doampg) +
                               reserved_pgs(i.id, i.ioampg))),
                       data = convert(numeric(20, 9), data_pgs(i.id, i.doampg)),
                       index_size =  convert(numeric(20, 9),
                               data_pgs(i.id, i.ioampg)),
                       unused = convert(numeric(20, 9),
                               ((reserved_pgs(i.id, i.doampg) +
                               reserved_pgs(i.id, i.ioampg)) -
                               (data_pgs(i.id, i.doampg) +
                               data_pgs(i.id, i.ioampg))))
               into #a
               from sysobjects o, sysindexes i, master.dbo.spt_values d
                               where i.id = o.id
                                       and o.type = "U"
                                       and d.number = 1
                                       and d.type = "E"
 
 select distinct name,
                   rowtotal = sum(rowtotal),
                   reserved = convert(numeric(11, 0), sum(reserved) * (low / 1024)),
                   data     = convert(numeric(11, 0), sum(data) * (low / 1024)) ,
                   index_size = convert(numeric(11, 0), sum(index_size) * (low / 1024)),
                   unused = convert(numeric(11, 0), sum(unused) * (low / 1024))
into #y
               from #a
group by name
 
 select distinct name,
                   rowtotal = convert(char(11), sum(rowtotal)),
                   reserved = convert(char(15), convert(varchar(11),
                              convert(numeric(11, 0), sum(reserved) *
                              (low / 1024))) + " " + "KB"),
                   data = convert(char(15), convert(varchar(11),
                              convert(numeric(11, 0), sum(data) * (low / 1024)))
                              + " " + "KB"),
                   index_size = convert(char(15), convert(varchar(11),
                               convert(numeric(11, 0), sum(index_size) *
                               (low / 1024))) + " " + "KB"),
                   unused = convert(char(15), convert(varchar(11),
                               convert(numeric(11, 0), sum(unused) *
                               (low / 1024))) + " " + "KB")
               from #a
group by name
order by 1
 
select
                   reserved = sum(reserved),
                   data =  sum(data) ,
                   index_size = sum(index_size) ,
                   unused = sum(unused)
from #y
0
 

Author Comment

by:hmk
ID: 6986888
amit, i posted one more question put these statements in a procedure(which will stay in one database and pass data base name as parameter to get table info in other database) and I don't know how to use use database in a procedure.
I hope u have solution for this.

Thanks,

hmk  
0
 

Expert Comment

by:JohnCoupe
ID: 12729379
System stored procedures, SSPs, always execute within the context of the database from which they are executed. The easy fix would be to put "use" statements inside the SSP but this isn't allowed. However, you can execute an SSP within the context of another database by prefixing the SSP with the target database name and "..". For example, the following SSP simply returns the current database name:

USE sybsystemprocs
GO

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id('dbo.sp_what_is_my_name') and type = 'P')
DROP PROCEDURE sp_what_is_my_name
GO

CREATE PROCEDURE sp_what_is_my_name
AS
SELECT db_name()
GO

E.g.
SET NOCOUNT ON
USE master
GO
EXEC sp_what_is_my_name
GO

 ------------------------------
 master
(return status = 0)

To run it in a different database context then use:
EXEC tempdb..sp_what_is_my_name
GO

 ------------------------------
 tempdb
(return status = 0)


The next example SSP uses this context switching facility to run the above SSP in each database:

USE sybsystemprocs
GO

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id('dbo.sp_what_are_my_names') and type = 'P')
DROP PROCEDURE sp_what_are_my_names
GO

CREATE PROCEDURE sp_what_are_my_names
@dbname_like varchar(50) = "%"
AS
DECLARE database_cur CURSOR FOR
SELECT d.name
FROM   master.dbo.sysdatabases d
WHERE  d.name LIKE @dbname_like
FOR READ ONLY
DECLARE @name VARCHAR(30)
DECLARE @sql  VARCHAR(50)

OPEN database_cur
FETCH database_cur INTO @name
WHILE (@@sqlstatus = 0)
BEGIN
  SELECT @sql = @name + '..sp_what_is_my_name'
  EXEC @sql
  FETCH database_cur INTO @name
END
CLOSE database_cur
DEALLOCATE CURSOR database_cur
GO

EXEC sp_what_are_my_names
GO

 ------------------------------
 master

 ------------------------------
 model

 ------------------------------
 sybsystemdb

 ------------------------------
 sybsystemprocs

 ------------------------------
 tempdb
(return status = 0)

EXEC sp_what_are_my_names '%db'
GO

 ------------------------------
 sybsystemdb

 ------------------------------
 tempdb
(return status = 0)


So, to answer your original question simply replace sp_what_is_my_name in the above SSP with the name of your procedure and you can find out how much space is used in any number of databases. An alternative solution is to use dynamic SQL in the originally posted solution but I think that it's far easier to read and maintain if you have two fairly simple SSPs rather than one quite complicated one. Also, the above method works in more versions of Sybase since dynamic SQL only became available relatively recently.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question