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
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2585 Visits: 4952
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 Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 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-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 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
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2585 Visits: 4952
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
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 247
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
Say Hey Kid
Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)

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

Thanks
Alex Gay
Alex Gay
SSChasing Mays
SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)

Group: General Forum Members
Points: 641 Visits: 474
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
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2585 Visits: 4952
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
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2585 Visits: 4952
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