Sethu's blog

February 14, 2012

Index rebuild on large database – SQL Agent Powershell job

Filed under: DB Management,Powershell,SQL Agent,SQL Power shell,SQL Server — sethusrinivasan @ 6:00 pm

Index rebuild on a large database could take longer time. One of our customer was looking for a solution to rebuild index one by one  when index  fragmentation is greater than 30%.  Related  newsgroup thread: http://social.msdn.microsoft.com/Forums/en-US/sqlkjmanageability/thread/4faae7c8-3e58-4117-8538-f397e342743f

In this blog post, I have written a SQL Powershell script that identifies indexes on tables that have fragmentation greater than 30% and performs a Index Rebuild one by one. You can also control the number of indexes to process in single script run. By this approach, you can run this SQL powershell script as a recurring scheduled SQL Agent job in non-peak hours.

PowerShell code:

You can download powershell script from rebuildIndexes.ps1

SQL agent job from Indexrebuild_agentjob.sql

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.