defrag all tables in all databases

Posted on 2009-12-21
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
    LVL 60

    Accepted Solution

    here is a link, you'll just need to swap out the DBCC DBREINDEX FOR INDEXDEFRAG
    LVL 13

    Assisted Solution

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

    Expert Comment

    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

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now