Monday, November 08, 2010

Rebuild and Reorganize Indexes in SQL Server

With the time, when more and more data are inserted into a table, the indexes of it tend to fragment. This might critically effect query performance if not monitored and taken care of. Rebuilding the indexes time to time will prevent things like this happening. Here's how to rebuild indexes in SQL Server 2005 using TSQL,

Rebuilding all the indexes in a table

ALTER INDEX ALL ON dbo.Customers REBUILD

Rebuilding a specific index

Alternatively you can re-organize indexes instead of rebuilding them when the index is fragmented, but not very much. Re-organizing an index would not be as costly as Rebuilding. Also it would not need the table to be locked during the process as rebuilding would. Here's the TSQL for reorganizing an INDEX.

ALTER INDEX ALL ON dbo.Customers REORGANIZE

No comments: