Sunday, 27 January 2013

Check SQL Server Table/Index Space

select top 50
    -- one page is 8K
    sum(s.page_count)/125.00 as [Size(MB)]
    -- record_count is at index level, can not do total sum here
    -- also using limited level with dm_db_index_physical_stats will give us null value
    --,sum(isnull(s.record_count, 0)) as total_record_Count
    ,d.name as DataBaseName
    ,sc.name as SchemaName
    ,o.name as TableName
    ,i.type_desc as IndexType
    ,i.name as IndexName
from    --sys.dm_db_index_physical_stats(db_id(), null, null, null, 'detailed') s
        sys.dm_db_index_physical_stats(db_id(), null, null, null, 'limited') s
        inner join sys.databases d on d.database_id=s.database_id
        inner join sys.objects o on o.object_id=s.object_id
        left join sys.indexes i on i.object_id=s.object_id and i.index_id=s.index_id
        inner join sys.schemas sc on sc.schema_id=o.schema_id
where    -- specfic index type (0 : heap, 1: clustered, 2: non-clusterd)
        i.type in (0,1,2)
        --and i.type in (0,1)
group by d.name, sc.name, o.name, i.name, i.type_desc
order by 1 desc
--go
--sp_spaceused 'MyTableName', @updateusage = N'TRUE'

No comments:

Post a Comment