Reorganizing/rebuilding an index
This recipe demonstrates how to reorganize or rebuild an index.
Getting ready
We will iterate through all the indexes in the Person.Person table in the AdventureWorks2008R2 database, for this exercise.
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPSmodule and create a new SMO Server object, as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run:
$VerbosePreference = "Continue" $databasename = "AdventureWorks2008R2" $database = $server.Databases[$databasename] $tableName = "Person" $schemaName = "Person" $table = $database.Tables | Where Schema -Like $schemaName | Where Name -Like $tableName #From MSDN: #EnumFragmentation enumerates...