SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


INDEX DEFRAGMENTATION SCRIPT


INDEX DEFRAGMENTATION SCRIPT

Author
Message
muthukkumaran Kaliyamoorthy
muthukkumaran Kaliyamoorthy
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7191 Visits: 4990
Comments posted to this topic are about the item INDEX DEFRAGMENTATION SCRIPT

Muthukkumaran Kaliyamoorthy

Helping SQL DBAs and Developers >>>SqlserverBlogForum

ryan.mcatee
ryan.mcatee
SSC-Enthusiastic
SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)

Group: General Forum Members
Points: 171 Visits: 134
Very nice work, thank you. I would have 5-starred it, but I don't like the script writing to the master database. I realize others might disagree with me, but I'd rather the script writes to the database being maintained, or optimally, have it's own, dedicated database to write to.
F.L
F.L
SSC Veteran
SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)

Group: General Forum Members
Points: 270 Visits: 402
Very nice script indeed.
Just a question, isn’t there some misinformation in this part?


BEGIN
--IF PAGE LEVEL LOCKING IS DISABLED THEN REBUILD
BEGIN TRY
EXEC ('USE ['+@DB_NAME+'];ALTER INDEX ['+@IND_NAME+'] ON ['+@SCHEMA_NAME+'].['+@TAB_NAME +'] REORGANIZE ')
END TRY
BEGIN CATCH
IF ERROR_NUMBER()=2552
EXEC ('USE ['+@DB_NAME+'];ALTER INDEX ['+@IND_NAME+'] ON ['+@SCHEMA_NAME+'].['+@TAB_NAME +'] REBUILD ')
END CATCH
EXEC ('USE ['+@DB_NAME+'];UPDATE STATISTICS ['+@SCHEMA_NAME+'].['+@TAB_NAME+'] (['+@IND_NAME+']) ' )
INSERT INTO MSDB.DBO.DBA_DEFRAG_MAINTENANCE_HISTORY
VALUES (@DB_NAME,@TAB_NAME,@IND_NAME,@FRAG,@PAGES,'REORGANIZE & UPDATESTATS',GETDATE())
END



If the TRY fail it goes into the CATCH, it does a rebuild.
But it still updates the history table with “REORGANIZE & UPDATESTATS”
muthukkumaran Kaliyamoorthy
muthukkumaran Kaliyamoorthy
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7191 Visits: 4990
If the TRY fail it goes into the CATCH, it does a rebuild.
But it still updates the history table with “REORGANIZE & UPDATESTATS”


Hi F.L
Thanks for your information.I ll change this ASAP.

Muthukkumaran Kaliyamoorthy

Helping SQL DBAs and Developers >>>SqlserverBlogForum

e-ghetto
e-ghetto
Old Hand
Old Hand (323 reputation)Old Hand (323 reputation)Old Hand (323 reputation)Old Hand (323 reputation)Old Hand (323 reputation)Old Hand (323 reputation)Old Hand (323 reputation)Old Hand (323 reputation)

Group: General Forum Members
Points: 323 Visits: 248
OFFTOPIC: MUTHUKKUMARAN, you have the coolest name, I've ever heard!:-P

I guess it's not the first time you hear this, isn't it?
Balance
Balance
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1126 Visits: 1048
Can I ask how you came to the decision to only do if @Page>1000?

Thanks
Alex Gay
Alex Gay
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1027 Visits: 490
Quick bug fix.
The line "EXEC ('USE '+@DB_NAME+';"

Should be "EXEC ('USE ['+@DB_NAME+'];"
otherwise it fails if you have a database name with spaces in.
Not that any of us would do this, but third parties who don't know any better do/did.

Otherwise a very useful script.
D Rose
D Rose
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
I see in the SQL that this is for SQL 2005 +.

Is this possible on SQL 2000?

Thanks!
muthukkumaran Kaliyamoorthy
muthukkumaran Kaliyamoorthy
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7191 Visits: 4990
Alex Gay (1/22/2010)
Quick bug fix.
The line "EXEC ('USE '+@DB_NAME+';"

Should be "EXEC ('USE ['+@DB_NAME+'];"
otherwise it fails if you have a database name with spaces in.
Not that any of us would do this, but third parties who don't know any better do/did.

Otherwise a very useful script.


S i agreed.I missed that one.Thanks

Muthukkumaran Kaliyamoorthy

Helping SQL DBAs and Developers >>>SqlserverBlogForum

muthukkumaran Kaliyamoorthy
muthukkumaran Kaliyamoorthy
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7191 Visits: 4990
D Rose (1/22/2010)
I see in the SQL that this is for SQL 2005 +.

Is this possible on SQL 2000?

Thanks!


Defragmentation script only works for sql server 2005/2008.If u want u can search here for 2000.

Muthukkumaran Kaliyamoorthy

Helping SQL DBAs and Developers >>>SqlserverBlogForum

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search