Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 



Relevant T-SQL Stored Procedures


RSS
Subscribe to Windows IT Pro | See More SQL Server and Database Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
Main Article    Winning the Fragmentation Battle

To set up the environment for the maintenance stored procedures, you start by creating a database to hold the statistics gathered during each execution. The Create Admin Database script creates a database named AdminData containing two tables. The DBCCStatistics table holds the results of the DBCC SHOWCONTIG WITH TABLERESULTS command that the uspBuildFragList stored procedure uses. You can delete the TableRowCounts table; the stored procedures in this article don't use it. Be aware that if you change the database name, you must modify the setup scripts of the stored procedures I discuss here and replace all references to AdminData with the correct database name.

The next step is to load and run the uspBuildFragList script. It creates the uspBuildFragList stored procedure in the AdminData database. If you changed the database name, do a search for AdminData and replace it with the new database name.

Repeat the previous step with the uspDefragTables, uspRecompile, and the DBMaintenance scripts. You must create uspDefragTables and uspRecompile before DBMaintenance because DBMaintenance has references to the first two stored procedures.

DBMaintenance is the stored procedure that does all the work. Note the use of DBCC UPDATEUSAGE at the beginning of the proceudre. This command performs a step that's essential to obtain accurate results for DBCC SHOWCONTIG. Create a job that includes a T-SQL step for each database that you want to monitor and/or defragment by using the following code:

Exec DBMaintenance 10, ‘database name' 

Database name is the name of the database you want to defragment, and 10 is the maximum fragmentation allowed before uspDefragTables runs DBCC DBREINDEX. In my tests, less than 10 percent fragmentation didn't seem to help performance, and it usually caused all my tables to be defragmented each night, which almost doubled the time the maintenance procedure took to finish. Remember that these maintenance tasks create heavy disk activity; therefore, you should schedule the job during a time when you can afford the drag on performance.

All these tasks are well suited to multithreaded, parallel processing. In my tests, run individually in sequence, my five databases took about 90 minutes, on average, to defragment. Run as five separate jobs executing in parallel, it took about 55 minutes on average. Additionally, try to schedule the task so that it finishes before a full backup of the database starts. Defragmentation and backups compete for almost every resource that SQL Server uses; therefore, it's best if they don't run at the same time.

End of Article



Reader Comments
It's too bad that this sidebar article isn't available to SQL Magazine subscribers (where the main article appears) and is instead only available to Windows IT Pro subscribers.

rnickolaus January 29, 2006 (Article Rating: )


I agree with the first review. It should be included with the SQL Server Magazine subscription.

brlarue February 02, 2006 (Article Rating: )


Where is the Create Admin Database script?

brlarue February 02, 2006 (Article Rating: )


Hello: this article is missing the code....
And the code form the connecting article is incomplete.
Winning the Fragmentation Battle

kenpearsonsqlmag February 03, 2006 (Article Rating: )


Great article and great techniques, but where's the beef? I am a Windows IT Pro subscriber and I don't have access to the scripts either.

fsalchli February 17, 2006 (Article Rating: )


Where are the sp scrips? This is a hoax.

Jason0183 March 08, 2006 (Article Rating: )


Looks like a good article, but I dont have access to the source either....I have a current SQL Server Magazine subscription

nathan_main March 21, 2006 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Related Events SQL Server Unleashed EMEA

The Easiest Way to Save Time and Money on E-mail and SharePoint Management

Check out our list of Free Email Newsletters!

SQL Server and Database eBooks Safeguarding Your Windows Servers

SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

Related SQL Server and Database Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement