Tuesday, 29 September 2026

Table size and rows of tables in sql server

Below query  will help us, to know tables size and now  of rows in a table. 
SELECT
    SCHEMA_NAME(t.schema_id) AS SchemaName,
    t.name AS TableName,
    SUM(p.rows) AS RowCont,

    CAST(
        SUM(a.total_pages) * 8.0 / 1024 / 1024
        AS DECIMAL(18,2)
    ) AS TotalSizeGB,

    CAST(
        SUM(a.used_pages) * 8.0 / 1024 / 1024
        AS DECIMAL(18,2)
    ) AS UsedSizeGB,

    CAST(
        (SUM(a.total_pages) - SUM(a.used_pages)) * 8.0 / 1024 / 1024
        AS DECIMAL(18,2)
    ) AS UnusedSizeGB

FROM sys.tables t
INNER JOIN sys.indexes i
    ON t.object_id = i.object_id
INNER JOIN sys.partitions p
    ON i.object_id = p.object_id
    AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a
    ON p.partition_id = a.container_id

WHERE t.is_ms_shipped = 0

GROUP BY
    t.schema_id,
    t.name

ORDER BY
    TotalSizeGB DESC;

No comments:

Post a Comment

Table size and rows of tables in sql server

Below query  will help us, to know tables size and now  of rows in a table.  SELECT     SCHEMA_NAME(t.schema_id) AS SchemaName,     t.name A...