SPEED: Server is not running as fast as it should be


SQL Server indexes are used to make searching and updating information faster in MS SQL. If your server is slower than you think it should be it can be because an index needs to be rebuilt.


If you know the table that might be slowing you down you can run the following SQL statement on that table. For this example the table in question is DATA. Replace that with the table you need to rebuild.


ALTER INDEX ALL ON [Data] REORGANIZE; 



If you want to rebuild ALL indexes in your database here is a script to do that. This could take quite a while so do not run this during working hours!


DECLARE @TableName varchar(255) 

DECLARE TableCursor CURSOR FOR 

 

SELECT table_name FROM information_schema.tables WHERE table_type = 'base table' 

 

OPEN TableCursor 

FETCH NEXT FROM TableCursor INTO @TableName 

WHILE @@FETCH_STATUS =

BEGIN 

       DBCC DBREINDEX(@TableName,' ',90) 

       FETCH NEXT FROM TableCursor INTO @TableName 

END 


CLOSE TableCursor 

DEALLOCATE TableCursor


IMPORTANT: USE THESE SCRIPTS AT YOUR OWN RISK! You must be aware of the implications of these SQL statements to use them correctly. Study up on their uses and use them accordingly.