AskHandle

AskHandle Blog

How to Determine MSSQL Table Size Easily

September 4, 2025Annie Hayes3 min read

How to Determine MSSQL Table Size Easily

Do you need to find the size of a table in your MSSQL database? This guide provides a simple way to achieve this.

Introduction to MSSQL Table Size Query

Determining the size of a table in MSSQL relates to the amount of storage space it occupies. This information is crucial for database administrators to make informed decisions about performance and optimization.

The Query You Need

Here is the query to find the size of a specific table in your MSSQL database:

sql
1SELECT 
2    t.NAME AS TableName,
3    s.Name AS SchemaName,
4    p.rows AS RowCounts,
5    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
6    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
7    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
8FROM 
9    sys.tables t
10INNER JOIN      
11    sys.indexes i ON t.OBJECT_ID = i.object_id 
12INNER JOIN 
13    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id 
14INNER JOIN 
15    sys.allocation_units a ON p.partition_id = a.container_id 
16LEFT OUTER JOIN 
17    sys.schemas s ON t.schema_id = s.schema_id
18WHERE 
19    t.NAME = 'YourTableName'
20GROUP BY 
21    t.NAME, s.Name, p.Rows

Replace 'YourTableName' with the actual table name. This query retrieves the table name, schema name, row count, total space, used space, and unused space.

Putting the Query into Practice

To execute the query, open SQL Server Management Studio and connect to your database. Go to the query editor and paste the query with your table name. Click the "Execute" button or press F5 to run the query. The results will appear quickly, showing the table's size metrics.

Interpreting the Results

The result set displays various metrics related to your table's size:

  • TableName: The name of the analyzed table.
  • SchemaName: The schema of the table.
  • RowCounts: The total number of rows in the table.
  • TotalSpaceKB: The total space occupied by the table in kilobytes.
  • UsedSpaceKB: The actively utilized space.
  • UnusedSpaceKB: The unutilized space in the table.

These metrics allow you to assess the storage footprint of your table effectively.

Further Optimization and Insights

Knowing the size of your MSSQL tables is just the start. To optimize your database further, consider additional tools and techniques. Tools like SQL Server Profiler and Database Engine Tuning Advisor can help with performance analysis. Query tuning practices can also enhance database efficiency.

Stay informed about best practices in database management through reputable sources such as Microsoft Docs.

Determining the size of your MSSQL tables is straightforward. Use the provided query to quickly access size metrics and utilize this information for database optimization and maintenance.