Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Query DB for table sizes/number of rows

Author  Topic 

ds9
Starting Member

38 Posts

Posted - 2007-12-10 : 10:53:23
Hi

I have a database with 150 tables. I want to make a query ranking those tables by table size and/or number of rows.

Is this possible?

Many thanks
ds9

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-10 : 10:55:09
Yes.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

ds9
Starting Member

38 Posts

Posted - 2007-12-10 : 11:01:39
Ok Ok my post was incomplete, fair enough
So let me add to my post:

Can any one (ie, Peso ) please tell me how to do it?

Cheers
JR
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-10 : 11:29:41
[code]SELECT o.NAME AS TableName,
i.ROWS AS Items,
SUM(COALESCE(NULLIF(c.LENGTH, -1), 16)) AS RowSize
FROM SYS.SYSOBJECTS AS o
INNER JOIN SYS.SYSINDEXES AS i ON i.ID = o.ID
INNER JOIN SYS.SYSCOLUMNS AS c ON c.ID = o.ID
WHERE o.XTYPE = 'u'
AND i.INDID < 2
GROUP BY o.NAME,
i.ROWS
ORDER BY o.NAME[/code]The trick here is that columns with size -1 are stored externally (IMAGE, TEXT, NTEXT) and only a pointer of 16 bytes is stored in the table.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

ds9
Starting Member

38 Posts

Posted - 2007-12-11 : 11:50:48
Thanks!

The rowsize is in Bytes?
The row count matches the properties field of the table (the one I've tested), but I can't match the table size showing in that properties window.

Regarding the -1 sized columns it's ok, as i don't have those data types in the tables.

Thanks again
ds9
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-12-11 : 12:04:58
This script does what you want.

Script to analyze table space usage
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61762

CODO ERGO SUM
Go to Top of Page

sqlsquirrel
Starting Member

21 Posts

Posted - 2007-12-11 : 17:25:30
Here is another way to accomplish this. You can find the solution in this blog:

http://www.lockergnome.com/sqlsquirrel/2007/12/04/getting-space-used-for-tables/

I hope it helps. It uses the sp_MSforeachtable stored procedure.

Cheers!

Brett Davis
Senior SQL Server DBA
For more helpful tips checkout my blog at: http://www.lockergnome.com/sqlsquirrel/
Go to Top of Page
   

- Advertisement -