Subscribe to How-To Geek

Determine Size of a Table in SQL Server

Ever wonder how big a table really is in your database? You know there are a million rows in the table, but how much space is that really taking?

SQL Server provides a built-in stored procedure that you can run to easily show the size of a table, including the size of the indexes… which might surprise you.

Syntax:

sp_spaceused ‘Tablename’

Here’s an example of it in action:

Highly useful stuff. It’s always interesting to see that the indexes are bigger than the actual data.

| More
This article was originally written on 03/27/07 Tagged with: Database, SQL Server

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (9)

  1. sMiGuMp

    While this is a useful command for determining the space allocated to a table, keep in mind that sp_spaceused references the sysindexes system table to get the space attributes and sometimes what’s stored there can get be out of synch with reality. If you see space numbers that are seemingly out of whack (especially for the ‘unused’ value), run a “DBCC UPDATEUSAGE” command against the table in question before running sp_spacesued (or against the entire database if you have time and aren’t worried about stepping on anyone’s toes).

    This can automatically be done as an optional parameter within sp_spaceused…

    e.g. sp_spaceused ‘Orders’,true

    …or as separate commands as shown below.

    To update usage stats for the entire current database, the syntax is:
    DBCC UPDATEUSAGE (0)

    For a specific, named database:
    DBCC UPDATEUSAGE (’InsertdDbNameHere’)

    For a specific table:
    DBCC UPDATEUSAGE (’InsertdDbNameHere’,'InsertdTableNameHere’)

    You can always check BOL for all the details on DBCC UPDATEUSAGE

  2. Charles tucker

    Thank you, using query analyer and running procedure sp_spaceused is helpful, however, what if your database has close too 100 tables. Anyway to return the entire database size?

  3. Rick

    I just found this, it returns spaceused for each table:

    EXEC sp_MSforeachtable @command1=”EXEC sp_spaceused ‘?’”

  4. Sanjiv

    Charles sp_helpdb

  5. Brian

    Rick – that is one of the most useful sql server commands of all time. You are the greatest.

  6. LNu76

    I tried to use Ricks Tipp, but having 181 Tables in the Database didnt do it much good. Also, his Mechanic with sp_spaceused treated each Table as a standalone query, wich made it impossible to use in an automated Growth-of-Database Statistic.

    I stole/borrowed the logic of sp_spaceused and applied it to the following VIEW (wich I now add to a Statistics Table, wich can then be queried in several ways):

    SELECT
    TableName,
    NumRows,
    reservedpages *8192/1024 as TotalSpace,
    pages * 8192/1024 as DataSpace,
    (usedpages-pages)*8192/1024 as IndexSpace,
    (reservedpages-usedpages)*8192/1024 as UnusedSpace

    FROM (SELECT
    t.[name] as tablename,
    avg([rows]) as NumRows,
    sum(total_pages) as reservedpages,
    sum(used_pages) as usedpages,
    sum(
    CASE
    When it.internal_type IN (202,204) Then 0
    When a.type 1 Then a.used_pages
    When p.index_id

  7. LNu76

    Bah, it got cut off, here is the rest:

    When p.index_id

  8. LNu76

    When p.index_id

  9. LNu76

    (smaller than sign – why doesnt it take it here?) 2 Then a.data_pages
    Else 0
    END) as pages
    from sys.allocation_units as a Join sys.partitions as p on p.partition_id = a.container_id
    left join sys.internal_tables it on p.object_id = it.object_id
    JOIN sys.tables as t on p.object_id=t.object_id
    –WHERE t.name=’mittra’
    group by t.[name]) as subselect


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.