Groups | Blog | Home
all groups > sql server new users > february 2006 >

sql server new users : How to defrag an SQL 7 AND 2000 server?


Clayton Sutton
2/24/2006 9:26:03 AM
Can someone tell me how to defrag. an MSSQL 7 AND MSSQL 2000 server. We
have both ver. 7 and 2000 (running on two different systems). I am not a
DBA but I really need to get this done tonight (2/24/2006) while we take our
systems down. I will also want to defrag the Windows 2000 and Windows 2003
NTSF file system volumes. Thanks for any and all input.

I will need the commands and where to run the commands (i.e. command prompt,
inside Enterprise Manager or inside Query Analyzer). I don't know anything
about SQL so go easy on me.


Clayton

Andrew J. Kelly
2/24/2006 1:10:31 PM
Here is a quick but effective way to reindex all the tables in a specific db
which will defrag the tables and indexes. If you want to defrag the OS
files you would simply use one of the tools designed for that such as
DiskKeeper, Norton etc. You should make sure you have a good backup before
you start and turn off sql server before you do the OS defrag for best
results.

--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

Clayton Sutton
2/24/2006 2:48:08 PM
Hey Andrew,

Thanks for the reply. See inline comments.


[quoted text, click to view]

What? How? Did you forget to give me the info?

[quoted text, click to view]

I will just be using Windows built-in defrag utility

[quoted text, click to view]

I will stop all SQL services first.

But what are the commands to defrag ALL of the DBs on the server? Are you
saying that if a defrag the NTFS volume that that will defrag ALL the DBs
too? Are there not SQL commands that I need to run to defrag ALL of the
DBs?


Clayton


[quoted text, click to view]

Andrew J. Kelly
2/24/2006 6:02:40 PM
Sorry I forgot to do the paste, see below for the code:

[quoted text, click to view]

Defragging at the OS level does nothing towards defragging the tables and
indexes. You would have to run the above script on each database.

-------

SET NOCOUNT ON

DECLARE @TableName VARCHAR(100)


DECLARE curTables CURSOR STATIC LOCAL
FOR
SELECT Table_Name
FROM Information_Schema.Tables
WHERE Table_Type = 'BASE TABLE'

OPEN curTables
FETCH NEXT FROM curTables INTO @TableName
SET @TableName = RTRIM(@TableName)

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT 'Reindexing ' + @TableName

DBCC DBREINDEX (@TableName)

FETCH NEXT FROM curTables INTO @TableName
END

CLOSE curTables
DEALLOCATE curTables


--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

Clayton Sutton
2/24/2006 9:33:07 PM
Hey Andrew,

Getting ready to go into work to get started (9:30pm CST). Just one more
question. How do I run the script? In a DOS Command Prompt? In a "Text"
file and call it "SomeFile.vbs"? Do I copy ad paste it into an MSSQL GUI
interface somewhere? Thanks for your help.


Clayton



[quoted text, click to view]

Andrew J. Kelly
2/25/2006 12:00:00 AM
The easiest way is to use Query Analyzer and paste the code into the window
and hit the F5 key. Query Analyzer should be found under SQL Servers folder
of the programs menu.


--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

AddThis Social Bookmark Button