SQL Server – Get Table Sizes

The script below will retrieve the number of rows, reserved size, data size, index size and unused size. The​​ reserved size​​ is the overall size of the table (it is the total of data size, index size and unused size).

 

 

--DROP TABLE #tmpTableSizes

CREATE​​ TABLE​​ #tmpTableSizes

(

 ​​ ​​ ​​​​ tableName​​ varchar(100),

 ​​ ​​ ​​​​ numberofRows​​ varchar(100),

 ​​ ​​ ​​​​ reservedSize​​ varchar(50),

 ​​ ​​ ​​​​ dataSize​​ varchar(50),

 ​​ ​​ ​​​​ indexSize​​ varchar(50),

 ​​ ​​ ​​​​ unusedSize​​ varchar(50)

)

insert​​ #tmpTableSizes

EXEC​​ sp_MSforeachtable​​ @command1="EXEC sp_spaceused '?'"

 

 

select ​​​​ *​​ from​​ #tmpTableSizes

order​​ by​​ cast(LEFT(dataSize,​​ LEN(dataSize)​​ -​​ 4)​​ as​​ int) ​​​​ desc

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter
Bookmark the permalink.

Leave a Reply

Leave a Reply

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