Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 HOW TO REORGANIZE ALL INDEXES ?

Author  Topic 

sqlvijay
Starting Member

16 Posts

Posted - 2008-11-20 : 11:03:28
Hi Guys,

I want to reorganize all indexes in the database periodically. If there is
any script to reorganize all indexes in the database.
please post it.

Thanks,
Vijay

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2008-11-20 : 11:37:27
what's wrong with searching on this forum for "reorganise indexes"..and following some of the links?

which might lead you to....
http://weblogs.sqlteam.com/tarad/archive/2006/04/11/9585.aspx


sometimes, it's easier if you tried to help yourself first...
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-11-20 : 11:54:17
Here is the code for smart Index reorg:

If avg_fragmentation_in_percent > 5% and < = 30% ALTER INDEX REORGANIZE

IF avg_fragmentation_in_percent > 30% ALTER INDEX REBUILD WITH (ONLINE = ON)


SELECT case when avg_fragmentation_in_percent between 5 and 30 then
'ALTER INDEX ' + i.name + ' ON ' + t.name + ' REORGANIZE;'
when avg_fragmentation_in_percent > 30 then 'ALTER INDEX ' + i.name + ' ON ' + t.name + ' REBUILD with(ONLINE=ON);'
else 'PRINT ''INDEX '+i.name+' ON TABLE '+t.name+' '+cast(avg_fragmentation_in_percent as varchar(5))+'%'''
FROM sys.dm_db_index_physical_stats(NULL,NULL,NULL,NULL,NULL) AS a
INNER JOIN sys.indexes AS i ON a.object_id = i.object_id AND a.index_id = i.index_id
join sys.tables t on t.object_id=i.object_id
where i.name is not null
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-20 : 12:23:47
Remember if you are only reorganizing huge fragmented table ,then it will take long time then rebuilding it.
So go with smart solution as hanbingl posted or Use script from Tara's Blog.
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-11-21 : 00:03:09

Link - Script to rebuild index:

http://www.mssqltips.com/tip.asp?tip=1367

====================================================
you realize you've made a mistake, take immediate steps to correct it.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-21 : 00:13:00
quote:
Originally posted by karthickbabu


Link - Script to rebuild index:

http://www.mssqltips.com/tip.asp?tip=1367

====================================================
you realize you've made a mistake, take immediate steps to correct it.




This script is not smart enough to detect what we are talking about
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-11-21 : 04:14:07

Ok, Using DBCC INDEXDEFRAG, DBCC DBREINDEX is good or not

====================================================
you realize you've made a mistake, take immediate steps to correct it.
Go to Top of Page
   

- Advertisement -