sp_spaceused reports the row count, data size, and index size for a SQL Server table or database.
sp_spaceused gives an instant view of how much space a table or index consumes. It helps capacity planning, performance tuning, and identifying bloated tables.
EXEC sp_spaceused 'TableName'; returns rows, reserved, data, index_size, and unused in KB. Use @updateusage = 'N' | 'Y' to refresh internal metadata first.
Run EXEC sp_spaceused 'Orders'; to see the current size of the Orders table, including all its indexes.
Run sp_MSforeachtable 'EXEC sp_spaceused ''?'''; This undocumented helper loops through every table and prints its size. Store the output in a temp table for sorting.
INSERT the results into #sizes, then SELECT * FROM #sizes ORDER BY data_MB DESC; This quickly highlights space hogs.
Use @updateusage = 'TRUE' after massive INSERT or DELETE operations. It walks system catalogs to correct row and page counts for accurate sizing.
sys.dm_db_partition_stats joined to sys.tables yields precise page counts. Multiply page_count * 8 / 1024 for MB. DMV queries are scriptable and avoid the PRINT output of sp_spaceused.
SELECT t.name, SUM(ps.used_page_count)*8/1024 AS data_MB FROM sys.dm_db_partition_stats ps JOIN sys.tables t ON t.object_id = ps.object_id GROUP BY t.name ORDER BY data_MB DESC;
Automate weekly reports, focus on rapidly growing tables, and archive or partition data before storage limits affect performance.
Skipping @updateusage causes stale size numbers. Ignoring index_size hides growth from non-clustered indexes that can outpace data.
Use sp_spaceused for quick checks, sys.dm_db_partition_stats for detailed scripts, and always refresh metadata after bulk operations.
No. It reads DMVs and catalog views; locking impact is negligible.
Yes, grant VIEW DATABASE STATE to allow DMV access or EXECUTE on sp_spaceused for the specific database.
Weekly for OLTP systems and daily for rapidly growing logging tables.
No. It reads catalog data and completes in milliseconds on most systems.
Cast the numeric part and divide by 1024, e.g., CAST(REPLACE(data,' KB','') AS int)/1024.0.
Yes for ad-hoc reports, but it is undocumented. Consider writing a cursor or set-based DMV query for production scripts.