Go to DotNetFunda.com
 Online : 621 |  Welcome, Guest!   Login
 
Home > Articles > Sql Server > How to Rebuilt all indexes in all Databases in SQL 2000/2005

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

How to Rebuilt all indexes in all Databases in SQL 2000/2005

 Posted on: 6/23/2009 4:34:30 AM by Vuyiswamb | Views: 832 | Category: Sql Server | Level: Advance | Print Article
This Article will Show you , how to rebuild all the indexes in table in all the Databases in SQL 2005/200

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

How to Rebuilt all indexes in all Databases in SQL 2000/2005

 

Introduction

There was a time where I inherited a project that uses a SQL Database that used indexes. The Database will work fine and the queries will look healthy. But after a certain long period the queries will become slow. Someone might ask what has changed. Well nothing has change. I used SQL Compare to check the difference between the Two Databases and there was none, the other one was working and the other was slowJ.  I tried so much to optimize the queries, and I came I across the rebuilt option in the on the indexes when I browse the table in SQL Management Studio. I rebuild the indexes and something change, the speed was great again, but not in all part, only in table that got index rebuild. Now this means the indexes need to be maintained.

Solution

I goggled and I came across a great post by Edgewood Solutions Engineers. This will allow you to Rebuilt all the Indexes in all the Databases in a SQL Server. The nice thing here is that you can put this in a SSIS package that can run on certain days and certain time.

Using the code

I have prepared a stored procedure that will help us do everything on one place

 


/*THIS SP WILL REBUILT ALL THE INDEXES IN ALL TABLES IN ALL DATABASES

THIS WILL BE CALLED FROM AN SSIS PACKAGE. THIS WILL USED IN CLIENTS TO REBUILT INDEXES AND IT WILL BE

RAN EVERY FRIDAY NIGHT

*/

 

ALTER PROC DATABASE_MAINTANANCE_PLAN

AS

 

DECLARE @Database VARCHAR(255) 

DECLARE @Table VARCHAR(255)

DECLARE @cmd NVARCHAR(500)

DECLARE @fillfactor INT

 

SET @fillfactor = 90

 

DECLARE DatabaseCursor CURSOR FOR

SELECT name FROM master.dbo.sysdatabases 

WHERE name NOT IN ('master','model','msdb','tempdb','distrbution') 

ORDER BY 1

 

OPEN DatabaseCursor

 

FETCH NEXT FROM DatabaseCursor INTO @Database

WHILE @@FETCH_STATUS = 0

BEGIN

 

   SET @cmd = 'DECLARE TableCursor CURSOR FOR SELECT table_catalog + ''.'' + table_schema + ''.'' + table_name as tableName 

                    FROM ' + @Database + '.INFORMATION_SCHEMA.TABLES WHERE table_type = ''BASE TABLE''' 

 

   -- create table cursor

   EXEC (@cmd)

   OPEN TableCursor 

 

   FETCH NEXT FROM TableCursor INTO @Table 

   WHILE @@FETCH_STATUS = 0 

   BEGIN 

 

       -- SQL 2000 command

       --DBCC DBREINDEX(@Table,' ',@fillfactor) 

       

       -- SQL 2005 command

       SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'

       EXEC (@cmd)

 

       FETCH NEXT FROM TableCursor INTO @Table 

   END 

 

   CLOSE TableCursor 

   DEALLOCATE TableCursor

 

   FETCH NEXT FROM DatabaseCursor INTO @Database

END

CLOSE DatabaseCursor 

DEALLOCATE DatabaseCursor

GO

sp_updatestats


 

Conclusion

You can use SSIS to do this, If you need more in for SSIS you can just Google “Introduction to SSIS in SQL”

Thank you

 

Vuyiswa Maseko

 

 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:8 year(s)
Home page:http://www.VuyiswaMaseko.com
Member since:Sunday, July 06, 2008
Level:HonoraryPlatinum
Status: [Member] [Administrator]
Biography:Vuyiswa Junius Maseko is a programmer for ITS abacus and a moderator in ".NetFunda. Vuyiswa has been developing for 8 years now. his major strength are C# 1.1,2.0,3.0,3.5 and vb.net and sql and his interest are in asp.net, c#, smart clients, Robot Programming(embedded programming).He has been doing a lot of windows development and web development, but lately his projects are web based projects. He has been using .net since the beta version of it. Currently he works on 2.0 but has some project running on 3.5. Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda).They have made vuyiswa what he is today.
 Latest post(s) from Vuyiswamb

   ◘ Introduction to DeepZoom Silverlight 3 posted on 3/15/2010 4:15:18 PM
   ◘ How to make your Control Always Available While Scrolling in ASP.NET AJAX posted on 3/11/2010 3:39:02 AM
   ◘ How to Write a Simple login page in Asp.net posted on 3/5/2010 5:53:22 PM
   ◘ How to Execute SSIS Packages in C# ASP.NET Part II posted on 2/24/2010 1:35:03 AM
   ◘ How to Execute SSIS Packages in C# ASP.NET - Part I posted on 2/12/2010 3:05:27 AM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)