defrag all tables in all databases

Posted on 2009-12-21
Medium Priority
Last Modified: 2012-05-08
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
Question by:chetan1981
  • 2
LVL 60

Accepted Solution

chapmandew earned 1000 total points
ID: 26099999
here is a link, you'll just need to swap out the DBCC DBREINDEX FOR INDEXDEFRAG

LVL 13

Assisted Solution

MikeWalsh earned 1000 total points
ID: 26125595
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:


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
LVL 13

Expert Comment

ID: 26125612
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.

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question