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.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s