In this post, I am providing different scripts 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, number of pages and extents, row size, free space.
Below are different scripts to find table related information.
Using DBCC SHOWCONTG, find average row size, page and extent information:
DBCC SHOWCONTIG ('Table_Name') WITH tableresults
Using sp_spaceused, find row count, index size, data size, unused space:
T-SQL Script for all tables:
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
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.