I need get the size of all tables in the database

  • Hi,

    Can anyone help me? I need get the size of all tables in the database.

    Is any query or sp is there which can give the result.

    I can get the size of each table individually by using "sp_spaceused 'table name'" but as the DB is huge and containg the 500+ table its a tedious task to carry.

    Please help as soon as possible.

    Thanks

    Vishwajeet 😛

  • Hi,

    CREATE TABLE #temp(

    rec_idint IDENTITY (1, 1),

    table_namevarchar(128),

    nbr_of_rowsint,

    data_spacedecimal(15,2),

    index_spacedecimal(15,2),

    total_sizedecimal(15,2),

    percent_of_dbdecimal(15,12),

    db_sizedecimal(15,2))

    -- Get all tables, names, and sizes

    EXEC sp_msforeachtable @command1="insert into #temp(nbr_of_rows, data_space, index_space) exec sp_mstablespace '?'",

    @command2="update #temp set table_name = '?' where rec_id = (select max(rec_id) from #temp)"

    -- Set the total_size and total database size fields

    UPDATE #temp

    SET total_size = (data_space + index_space), db_size = (SELECT SUM(data_space + index_space) FROM #temp)

    -- Set the percent of the total database size

    UPDATE #temp

    SET percent_of_db = (total_size/db_size) * 100

    -- Get the data

    SELECT *

    FROM #temp

    ORDER BY total_size DESC

    -- Comment out the following line if you want to do further querying

    DROP TABLE #temp

    PSA

  • If you are using SQL 2005, u can get this via Performance Dashboard reports ( note u need SP2), its called DiskUsage by table report

    🙂

  • Thanks a lot for your help....:D

    But for the last response i dint understood clearly can you please tell in details what do you meam:w00t:

  • In Management Studio, there are reports for various objects. Right click, select "Reports" and you'll see them.

  • I knew there was a reason why I love SSC so much. Thanks PSA for the code, and thanks Steve for the advice.

  • original code by rbarry young..................

    SELECT SCHEMA_NAME(tbl.schema_id) as [Schema]

    , tbl.Name

    , Coalesce((Select pr.name

    From sys.database_principals pr

    Where pr.principal_id = tbl.principal_id)

    , SCHEMA_NAME(tbl.schema_id)) as [Owner]

    , tbl.max_column_id_used as [Columns]

    , CAST(CASE idx.index_id WHEN 1 THEN 1 ELSE 0 END AS bit) AS [HasClusIdx]

    , Coalesce( (Select sum (spart.rows) from sys.partitions spart

    Where spart.object_id = tbl.object_id and spart.index_id < 2), 0) AS [RowCount]

    , Coalesce( (Select Cast(v.low/1024.0 as float)

    * SUM(a.used_pages - CASE WHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END)

    FROM sys.indexes as i

    JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id

    JOIN sys.allocation_units as a ON a.container_id = p.partition_id

    Where i.object_id = tbl.object_id )

    , 0.0) AS [IndexKB]

    , Coalesce( (Select Cast(v.low/1024.0 as float)

    * SUM(CASE WHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END)

    FROM sys.indexes as i

    JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id

    JOIN sys.allocation_units as a ON a.container_id = p.partition_id

    Where i.object_id = tbl.object_id)

    , 0.0) AS [DataKB]

    , tbl.create_date, tbl.modify_date

    FROM sys.tables AS tbl

    INNER JOIN sys.indexes AS idx ON (idx.object_id = tbl.object_id and idx.index_id < 2)

    INNER JOIN master.dbo.spt_values v ON (v.number=1 and v.type='E')

    order by datakb desc

    ---------------------------------------------------------------------

  • oops.just seen this thread is 2 years old.................

    ---------------------------------------------------------------------

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply