DB reindex script to keep the DB in autopilot mode

by Kevin Bosch 10. August 2010 20:43

Part of SQL db maintenance is rebuilding indexes in the databases. SQL Server automatically maintains indexes whenever insert, update, or delete operations are made to the underlying data however over time these modifications can cause the information in the index to become fragmented. When fragmentation exists when indexes have pages in which the logical ordering, based on the key value, does not match the physical ordering inside the data file. Fragmented indexes can affect query performance. The script below is a sledge hammer approach to re indexing all indexes in all tables in all databases on a given SQL instance. With smaller type databases <10 GB this approach is effective in keeping the database in auto pilot mode. Good DBA’s will raise their eye brows at this however there are lots of smaller sites which don’t have the luxury of full time DBA’s. This script will work on sql 2005+

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   

       SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'  
       print  @cmd  -- print cmd to see what it is doing. 
       EXEC (@cmd) 
       
       FETCH NEXT FROM TableCursor INTO @Table   
   END   

   CLOSE TableCursor   
   DEALLOCATE TableCursor  

   FETCH NEXT FROM DatabaseCursor INTO @Database  
END  
CLOSE DatabaseCursor   
DEALLOCATE DatabaseCursor  

Tags: ,

SQL development

Comments


August 24. 2010 14:26
Patsy Baker
Right after looking through this article, I pondered exactly the same thing that i often wonder when checking out fresh blogs. What do I think about it? Exactly how does it influence me personally?


August 27. 2010 12:12
Universities
I have to say I am impressed with your blog and I will save its RSS to be alerted whenever you make a new post. Keep up the good work.


August 28. 2010 03:09
digital radio
Thank you for the post. I just about passed your website up in Ask but now I'm glad I clicked on through and got to browse through it. I'm definitely a little better informed now. I'll be telling my buddies about your site. They'll for sure enjoy the heck out of what I just read too. LOL. --Billy


September 9. 2010 06:37
air jordan shoes
This is the first time that I've seen this blog and I'm really impressed. I might have to make myself less of a stranger around here.

Comments are closed

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Code Associate