Connecting Tech Pros Worldwide Forums | Help | Site Map

Finding table size (physical disk space)

Member
 
Join Date: May 2007
Posts: 101
#1: Oct 4 '07
How do I find how much space a table takes up on a hard drive? Is there any call you can make that returns a size?

If so, how can I display this with asp?

Thanks for any help and insights.

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Oct 4 '07

re: Finding table size (physical disk space)


Heya, Zensunni.

Check this out:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `DATA_LENGTH`
  3.     FROM
  4.         `information_schema`.`TABLES`
  5.     WHERE
  6.     (
  7.             `TABLE_SCHEMA` = 'database name'
  8.         AND
  9.             `TABLE_NAME` = 'table name'
  10.     )
  11.     LIMIT 1;
  12.  
Member
 
Join Date: May 2007
Posts: 101
#3: Oct 5 '07

re: Finding table size (physical disk space)


Ops, I'm retarded.

I meant to post in the MS SQL forum. Thanks for the reply though. MySQL is my DB of choice. Just wish all my clients could think that too :)

Thanks again and sorry.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Oct 5 '07

re: Finding table size (physical disk space)


Heya, zensunni.

No problem.

I'm going to go ahead and move this thread to the MS SQL forum, where our resident Experts will be better able to help you out.
Member
 
Join Date: May 2007
Posts: 101
#5: Oct 11 '07

re: Finding table size (physical disk space)


I'm pretty sure it's one of the system tables I have to access, which holds the information, but I'm unsure of which one.
iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#6: Oct 12 '07

re: Finding table size (physical disk space)


Let's assume you want to find out space that is taken by sysobjects.
Usually to see space used by the table you execute following statement:


Expand|Select|Wrap|Line Numbers
  1. sp_spaceused sysobjects

Because you need to query the same data I assume you are interested in reserved space by this table in a database. Use query below to select the same data directly from system tables. If you use SQL 2005 add schema name before system table name.

Expand|Select|Wrap|Line Numbers
  1. Declare @table varchar(50)
  2. select @table = 'sysobjects'
  3.  
  4. select ltrim(str((select sum(reserved)  
  5.                   from sysindexes
  6.                   where indid in (0, 1, 255)  
  7.                   and id = object_id(@table) ) 
  8.                 * d.low / 1024.,15,0) +  
  9.        ' KB')
  10.  from master.dbo.spt_values d  
  11.   where d.number = object_id(@table) 
  12.    and d.type = 'E'
Good Luck.
Member
 
Join Date: May 2007
Posts: 101
#7: Oct 30 '07

re: Finding table size (physical disk space)


Thanks all. I used these commands:

To find all the tables:
SELECT * FROM sysobjects WHERE type = 'U'

To find out how much space was used by a table:
exec sp_spaceused '<tablename>'
Reply


Similar Microsoft SQL Server bytes