Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29896 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > How to Rebuilt all indexes in all Databases in SQL 2000/2005

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

Article posted by Vuyiswamb on 6/23/2009 | Views: 2680 | Category: Sql Server | Level: Advance red flag


This Article will Show you , how to rebuild all the indexes in table in all the Databases in SQL 2005/200

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.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:8 year(s)
Home page:http://www.Dotnetfunda.com
Member since:Sunday, July 06, 2008
Level:NotApplicable
Status: [Member] [MVP] [Administrator]
Biography:Vuyiswa Junius Maseko is a programmer 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 sql and his interest are in Silverlight,WPF,C#. He has been doing a lot of Silverlight development. He has been using .net since the beta version of it. He is also an online Trainer at www.Itfunda.com. Thanks to people like Sheo Narayan (.Netfunda) , Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject),.They have made vuyiswa what he is today.
>> Write Response - Respond to this post and get points
Related Posts

This is part 27 of the series of article on SSIS. In this article we are going to learn how to delete local folder using FTP task in SSIS.

According to Microsoft best practice, User defined stored procedures should not be started with 'SP_'. The reasone is...

One of the nightmares of a developer is the deadlock. The main source of a deadlock is the backend code, SQL Server scripts. In this article, we will discuss about how to use the SQL Profiler to capture the deadlocks in SQL Statements.

This is part 23 of the series of article on SSIS. In this article, we are going to see how to send file to FTP using SSIS.

Most of us worry about Database Backup is Time taken, free space required for the backup file etc. etc.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:57:44 AM