Printed 2017/08/17 10:03AM

Find the largest tables in a database (II)

By Jeffrey Yao, 2011/09/18

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.

Copyright © 2002-2017 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.