Friday, February 24, 2012

"ALTER INDEX <tablename> REBUILD"

I have read through BOL but am still confused by the above sql. I think it rebuilds all indexes on a table. Am I correct? (If so, if would seem to be a good thing to run it nightly on all tables in all databases. Or maybe that is too extreme)

Barkingdog

This will rebuild all indexes on a table


Code Snippet

ALTER INDEX ALL ON YourTable REBUILD.

The following will rebuild an individual index:

Code Snippet

ALTER INDEX YourIndexName ON YourTable REBUILD

This is often a resource intensive process as locks are taken out on the table and should ideally be scheduled for a period of low usage. As for how often you need to do this, i would guess it depends on how fragmented your indexes get. This can be checked by looking in sys.dm_db_index_physical_stats.

HTH!

No comments:

Post a Comment