How to List Table Sizes in MSSQL

Using SQL Server Management Studio (SSMS):

You can use SQL Server Management Studio (SSMS) to list the size of tables. Follow the steps below:

  • Open SSMS and connect to the relevant database.
  • Expand the database node and open the “Tables” folder.
  • Right-click on the desired table and select “Properties.”
  • Right-click on the desired table and select “Properties.”
  • In the opened window, navigate to the “Storage” section.
  • You can find the “Data space used” value in KB or MB.

Using this method, you can manually check the size of each table.

Using T-SQL Query:

You can list the size of tables in KB and MB using the following T-SQL query:

USE YourDatabaseName;
GO

SELECT 
    t.NAME AS TableName,
    s.NAME AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB,
    SUM(a.total_pages) * 8 / 1024 AS TotalSpaceMB
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
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.is_ms_shipped = 0
GROUP BY 
    t.NAME, s.NAME, p.Rows
ORDER BY 
    TotalSpaceKB DESC;

Make sure to replace “YourDatabaseName” with the actual name of your database.

When you execute the above query, it will list the table names, schema names, row counts, total space in KB, and total space in MB for tables in the specified database.

I hope you find this article helpful. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *