Monday, May 3, 2010

SQL SERVER – ReIndexing Database Tables and Update Statistics.


When any data modification operations (INSERT, UPDATE, or DELETE statements) table fragmentation can occur.

In SQL Server it is very much important to reorganization your database.
Periodically (daily, weekly, or monthly) perform a database reorganization on all the indexes on all the tables in your database. This will rebuild the indexes so that the data is no longer fragmented. Fragmented data can cause SQL Server to perform unnecessary data reads, slowing down SQL Server's performance. It will also update column statistics.

If you do reorganization on a table with a clustered index, any non-clustered indexes on that same table will automatically be rebuilt.

--Script to automatically reindex all tables in a database
USE YourDatabaseName
GO
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
EXEC sp_updatestats
GO

No comments:

Post a Comment