defrag all tables in all databases

any one has a script to derfrag all tables in all databases?? Inned to run DBCC showcontig and not DMV as it needs to tun on both sql 2k and sql 2k5
chetan1981Asked:
Who is Participating?
 
chapmandewCommented:
here is a link, you'll just need to swap out the DBCC DBREINDEX FOR INDEXDEFRAG

http://blog.sqlauthority.com/2007/01/31/sql-server-reindexing-database-tables-and-update-statistics-on-tables/
0
 
MikeWalshCommented:
I use this script in all of my production SQL Server 2005 and greater instances. Rather than take the cost of rebuilding all indexes and rather than use one of the older methods (DBREINDEX, DBCC SHOWCONTIG, etc. are all supported for backwards compatibility but not the approved manner to do this in 2005 and beyond). Michelle Ufford over at SQLFool uses this in her production environment (She works with some large databases with high up time requirements). This script is priceless and does just what I need:

http://sqlfool.com/2009/06/index-defrag-script-v30/

While the above two answers are definitely helpful answers they also show the older style from SQL Server 2000 and below. And the second example takes word for word and uncredited the example given by Microsoft for SQL Server 2000 at their MSDN site: http://msdn.microsoft.com/en-us/library/ms177571.aspx
0
 
MikeWalshCommented:
I apologize, I saw the 2005 but missed the comment about SQL Server 2000. I would definitely use the script that is at the Microsoft site linked above for both. This will work in both 2000 and 2005 and you can decide which tables to or not to defragment with the maxfrag percentage to avoid doing work you don't need to do.

This is the premise that Michelle based her script off of. I would even suggest using the Microsoft script for SQL Server 2000 and her script for SQL Server 2005, especially if you are on enterprise edition and want to take advantage of online index rebuilds. Her script also addresses a statistics rebuild which should be done during maintenance as well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.