How can we see all database tables name and sizes

  • Hi,

    How can we see all tables names and size of tables in (mb ) from database with query .

    thaxxx

    regards

    jagpal singh

  • jagpalsingh4 (3/18/2008)


    Hi,

    How can we see all tables names and size of tables in (mb ) from database with query .

    thaxxx

    regards

    jagpal singh

    Try this - results are in kb:

    select name

    , reserved = reserved * 8

    , data = data * 8

    , index_size = CASE WHEN data > pages THEN (data - pages) ELSE 0 END * 8

    , unused = CASE WHEN reserved > data THEN (reserved - data) ELSE 0 END * 8

    from

    (

    select

    name

    , sum(reserved_page_count) as reserved

    , sum(used_page_count) as data

    , sum(

    case

    when (index_id < 2) then (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)

    else lob_used_page_count + row_overflow_used_page_count

    end

    ) as pages

    , sum(

    case

    when (index_id < 2) then row_count

    else 0

    end

    ) as row_count

    from sys.dm_db_partition_stats s

    join sys.tables t on t.object_id = s.object_id

    group by name

    ) as table_sizes

  • Something like this should help.

    CREATE TABLE #temp (

    table_name sysname ,

    row_count int,

    reserved_size varchar(50),

    data_size varchar(50),

    index_size varchar(50),

    unused_size varchar(50)

    )

    SET NOCOUNT ON

    INSERT #temp

    EXEC sp_msforeachtable 'sp_spaceused ''?'''

    SELECT a.table_name,

    a.row_count,

    count(*) as col_count,

    a.data_size [Data_Size_KB],

    1.0 * CAST(Replace(a.data_size, ' KB', '') as decimal(9,3)) / 1024 as [Data_Size_MB]

    FROM #temp a

    INNER JOIN information_schema.columns b

    ON a.table_name = b.table_name

    GROUP BY a.table_name, a.row_count, a.data_size

    ORDER BY CAST(Replace(a.data_size, ' KB', '') as int) desc

    DROP TABLE #temp

  • I should point out that my method will not return correct results for tables with XML indexes or full text indexes, whereas Adam's method will return correct results. But then with that method you're effectively using a cursor so pick your poison 🙂

  • You can also right click the database in SSMS, choose Reports, and look for one that matches your needs. There are a number of standard reports that might help.

  • This gives you the information you want, along with a more detailed analysis.

    Script to analyze table space usage

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61762

  • You can also right click the database in SSMS, choose Reports, and look for one that matches your needs. There are a number of standard reports that might help.

    These reports are nice and can help you identify large tables.

    This gives you the information you want, along with a more detailed analysis.

    Script to analyze table space usage

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61762

    Good, link Michael. I saved this one 🙂

  • hi,

    will u plz clear me this ssms, and that query is lovely will u plz tell me how to see reports from ssms .

    thaxx

  • Right-click the database --> reports --> standard reports --> disk usage by table

  • hi

    I have done this before but when i right click the database thn there is no option of reports .Thats y i am confused that what is ssms .and plz tell me what can i do now

    thaxx for ur reply and sorry fro inconvenience

    regards

    jagpal singh

  • SSMS is SQL Server Management Studio

    What service pack is your instance on? I think you need to be on service pack 2.

  • If you're not on the latest service pack then the reports are viewable from the summary tab in ssms. Select the summary tab, select a database and then the Report button should become visible in the summary tab toolbar.

  • hi,

    Thaxxx now i have done this .

    regards

    jagpal singh

  • If you're not on the latest service pack then the reports are viewable from the summary tab in ssms. Select the summary tab, select a database and then the Report button should become visible in the summary tab toolbar.

    Thanks Karl. I havent been on SP1 in a while and could not remember if reporting was available in SP1, without reporting services being installed.

  • I have done this before but when i right click the database thn there is no option of reports .Thats y i am confused that what is ssms .and plz tell me what can i do now

    SSMS = SQL Server Management Studio

    To see the Reports you must have SQL Server 2005 SP2 installed.


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

Viewing 15 posts - 1 through 15 (of 18 total)

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