Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

ReIndexing Database Tables in SQL Server

$
0
0
If we create index on table and indexed table frequently changes occur than fragmentation increases. This may reduce your query performance. So we need to rebuild the index time to time.

We can use DBCC DBREINDEX to rebuild the all index on all tables in database. DBCC DBREINDEX drops index and creates again.

Rebuild all indexes in database with specific fill factor


USE[BD_NAME]
GO
EXECsp_MSforeachtable@command1="print '?' DBCC DBREINDEX ('?', '', 80)"
GO
EXECsp_updatestats
GO


Rebuild all indexes in database with specific fill factor


USE[BD_NAME]
GO
EXECsp_MSforeachtable@command1="print '?' DBCC DBREINDEX ('?')"
GO
EXECsp_updatestats
GO


sp_updatestatsinsures that “Statistics for all tables have been updated.”

Here question mark is a placeholder for the table name (as it loops through each table in turn).

To rebuild all index in a table


USE[DB_NAME]
GO
ALTERINDEXALLONTBL_NAME
REBUILDWITH (FILLFACTOR= 80,SORT_IN_TEMPDB=ON,
              STATISTICS_NORECOMPUTE=ON);
GO




Viewing all articles
Browse latest Browse all 265

Trending Articles