SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Find the largest tables in a database (II)

I once wrote a post to find the largest tables in a database, and now after studying PowerShell, I feel it can be written in a more concise and efficient way.

The following script is to find the largest 3 tables in row count in AdventureWorks

$srv = Get-SQLServer –sqlserver “<my server name>” –user “sa” –pass “pa$$w0rd”
$db = $srv.databases[‘AdventureWorks’]
$db.tables | sort-object –property RowCount –Desc | select name, RowCount –first 3

You can change RowCount (in 3rd line) to DataSpaceUsed or IndexSpaceUsed to find the largest 3 tables in terms of data space or index space.

Get-SQLServer is cmdlet from SQLPSX, originally initiated by Chad Miller, whom I appreciate a lot for his (and other contributors’) great work in SQLPSX, and I use it daily.


No comments.

Leave a Comment

Please register or log in to leave a comment.