Important alert: (current site time 7/15/2013 7:57:16 PM EDT)
 

VB icon

SQL Database Defrag

Email
Submitted on: 2/6/2001 8:36:31 AM
By: philip hannent 
Level: Advanced
User Rating: By 10 Users
Compatibility: SQL Server 7.0
Views: 19600
author picture
(About the author)
 
     This code will organise the indexes on your database to improve speed. (Simple)

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: SQL Database Defrag
-- Description:This code will organise the indexes on your database to improve speed. (Simple)
-- By: philip hannent
--
-- Assumes:When you run this code it will create a stored proc. Once you have done that just run 
EXEC usp_DefragDatabase 'Test' 
This will perform the defrag.
--
-- Side Effects:While the defrag is in process you performace will take a dive.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=275&lngWId=5--for details.--**************************************

--Re-indexes the specified database
CREATE PROCEDURE usp_DefragDatabase
-- We don't use sysname because it might not be long enough. 
-- sysname is 128 chars, so we use double that. 
@dbname nvarchar(256)
AS 
BEGIN
-- Quote the database name with brackets
DECLARE @quoteddbname nvarchar(256)
set @quoteddbname = quotename( @dbname )
-- The outer EXEC is so we can do USE, not allowed in stored procs
-- The inner EXEC does the actual reindex on each table in the
-- specified database
EXEC('USE'+ @quoteddbname +'
DECLARE @sTableName sysname
DECLARE PKMS_Tables CURSOR LOCAL FOR 
select table_name from information_schema.tables 
where table_type = ''base table'' order by 1 
OPEN PKMS_Tables 
FETCH NEXT FROM PKMS_Tables INTO @sTableName 
WHILE @@FETCH_STATUS = 0 
BEGIN 
select @sTablename = quotename(@sTablename, ''[]'')
EXEC('' DBCC DBREINDEX ( ''+@sTableName+'') WITH NO_INFOMSGS'')
FETCH NEXT FROM PKMS_Tables INTO @sTableName 
END 
CLOSE PKMS_Tables')
END
GO


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
2/7/2001 8:08:52 AMPhilip Hannent

What 80 people viewing it and no comments? :-(
(If this comment was disrespectful, please report it.)

 
2/26/2001 8:03:38 AMBalasubramanian.R

The following can also be achieved by using this command

exec sp_MSforeachtable "DBCC DBREINDEX ('?') WITH NO_INFOMSGS"
(If this comment was disrespectful, please report it.)

 
8/6/2001 11:05:42 AMCharles Kincaid

Very good. This could be expanded in many ways. Logging could be added. You could also create a table and store the date and time of each reindex. You could then create a stored proceedure that would loop through each database on a server and get them all. Your stored procedure could look at how long it had been running and quit after some predetermined amount of time.

When put altogether such a system could automate a bunch of routine tasks and only bog down the system when it is being used the least.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.