This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing different scripts on how to find row count, occupied space, free space, page and extent information of the SQL Server Tables.
The first step for performance optimization is to measure the size of tables with different information like a number of pages and extents, row size, free space.
Below are different scripts for finding the table related information.
Using DBCC SHOWCONTG, find average row size, page and extent information:
1 2 |
DBCC SHOWCONTIG ('Table_Name') WITH tableresults GO |
Using sp_spaceused, find row count, index size, data size, unused space:
1 2 |
sp_spaceused 'Table_Name' GO |
T-SQL Script for all tables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SELECT O.name AS TableName ,P.rows AS TotalRows ,SUM(AU.used_pages)*8 AS SizeInKB FROM sys.objects AS O JOIN sys.indexes AS I ON O.object_id = I.object_id JOIN sys.partitions AS P ON O.object_id = P.object_id JOIN sys.allocation_units AS AU ON AU.container_id = P.partition_id WHERE O.type = 'U' AND I.index_id IN (0,1) GROUP BY O.name, P.rows ORDER BY 3 DESC |