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


INDEX DEFRAGMENTATION SCRIPT


INDEX DEFRAGMENTATION SCRIPT

Author
Message
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: 1128 Visits: 1048
I know this comment is rather late in the day but I'm just revisiting your script - I've used it as the basis of something that suits what I want so thought I'd mention a simple performance improvement & that is to make the cursor a fast_forward cursor. Makes access to a "step through 1 row after another" type of cursor significantly faster.
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: 1128 Visits: 1048
Having used this as the basis for my own version though I best update again - there is an error in the code when you have multiple schemas in a database where tablenames are repeated (so schemaA.Table1 and schemaB.Table1).

Solution is to make the code schema aware, so changing this code section :
INSERT INTO #TEMPFRAG
SELECT OBJECT_NAME(F.OBJECT_ID) OBJ,I.NAME IND,
F.AVG_FRAGMENTATION_IN_PERCENT,
F.PAGE_COUNT,TABLE_SCHEMA
FROM SYS.DM_DB_INDEX_PHYSICAL_STATS (DB_ID(),NULL,NULL,NULL,NULL) F
JOIN SYS.INDEXES I
ON(F.OBJECT_ID=I.OBJECT_ID)AND I.INDEX_ID=F.INDEX_ID
JOIN INFORMATION_SCHEMA.TABLES S
ON (S.TABLE_NAME=OBJECT_NAME(F.OBJECT_ID))
--WHERE INDEX_ID<> 0
AND F.DATABASE_ID=DB_ID()
AND OBJECTPROPERTY(I.OBJECT_ID,''ISSYSTEMTABLE'')=0'



with this


INSERT INTO #TEMPFRAG
SELECT OBJECT_NAME(F.OBJECT_ID)
, I.NAME IND
, F.AVG_FRAGMENTATION_IN_PERCENT
, F.PAGE_COUNT
, s.name -- NOTE CHANGED FROM TABLE_SCHEMA
--, i.allow_page_locks -- I use this to better handle allow_page_locks true / false
FROM SYS.DM_DB_INDEX_PHYSICAL_STATS ('+cast(@DB_ID as varchar)+',NULL,NULL,NULL,NULL) F
JOIN SYS.INDEXES I ON(F.OBJECT_ID=I.OBJECT_ID) AND I.INDEX_ID=F.INDEX_ID and i.is_disabled = 0 -- Note I'm skipping disabled indexes
join sys.tables t on f.object_id = t.object_id -- NOTE NEW LINE TO REPLACE USE OF INFORMATION_SCHEMA
join sys.schemas s on s.schema_id = t.schema_id -- NOTE NEW LINE TO COMPLETE JOIN
--WHERE INDEX_ID<> 0
AND F.DATABASE_ID=DB_ID()
AND OBJECTPROPERTY(I.OBJECT_ID,''ISSYSTEMTABLE'')=0



The only other change you want to make for the above to work is to add at the start of your code :

declare @DB_ID int
set @DB_ID= DB_ID('Your DB Name Here')


Because that gets around a failure in the original source if you want to run this for any database from any database.
jshailendra
jshailendra
SSC Eights!
SSC Eights! (924 reputation)SSC Eights! (924 reputation)SSC Eights! (924 reputation)SSC Eights! (924 reputation)SSC Eights! (924 reputation)SSC Eights! (924 reputation)SSC Eights! (924 reputation)SSC Eights! (924 reputation)

Group: General Forum Members
Points: 924 Visits: 455
Thanks Mutthu...this goes to my briefcase.Satisfied
macherla.harish
macherla.harish
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 65
HI Muthu ,

Good script given.


HI muthu, i have a Dought in Sqlserver2000 , IN sqlserver 2005 Copy_only option is there But In sql server2000 not there if hava log shiping or Replication iam using iam taken backup lsn chain will breakup.

In my production sqlserver2000 is there how to take a backup without lSN Breakup


Thank You
Harishkumar.M
macherla.harish
macherla.harish
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 65
HI Muthu ,

Good script given.


HI muthu, i have a Dought in Sqlserver2000 , IN sqlserver 2005 Copy_only option is there But In sql server2000 not there if hava log shiping or Replication iam using iam taken backup lsn chain will breakup.

In my production sqlserver2000 is there how to take a backup without lSN Breakup


Thank You
Harishkumar.M
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: 7237 Visits: 4990
macherla.harish (2/12/2010)
HI Muthu ,

Good script given.


HI muthu, i have a Dought in Sqlserver2000 , IN sqlserver 2005 Copy_only option is there But In sql server2000 not there if hava log shiping or Replication iam using iam taken backup lsn chain will breakup.

In my production sqlserver2000 is there how to take a backup without lSN Breakup


Thank You
Harishkumar.M


Please start a new thread :-)

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: 7237 Visits: 4990
Balance (1/22/2010)
Can I ask how you came to the decision to only do if @Page>1000?

Thanks


Its MS rule of thumb.

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: 7237 Visits: 4990
Click here:INDEX DE-FRAGMENTATION SCRIPT for SQL server 2000

Edit: to Fix the link

Muthukkumaran Kaliyamoorthy

Helping SQL DBAs and Developers >>>SqlserverBlogForum

bobw1776
bobw1776
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 289
I haven't read through the above replies to check, but has anyone received the below error ONLY when the SP is executed against a CS collated DB? e.g. "SQL_Latin1_General_CP1_CS_AS"

"
Msg 208, Level 16, State 1, Line 2
Invalid object name 'SYS.DM_DB_INDEX_PHYSICAL_STATS'. "

It runs fine on any other DB. It might be the solution to my failed indexing scripts, won't know until I get it running for a little bit.

Thanks,
Bobw
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: 7237 Visits: 4990
bobw1776 (11/30/2011)
I haven't read through the above replies to check, but has anyone received the below error ONLY when the SP is executed against a CS collated DB? e.g. "SQL_Latin1_General_CP1_CS_AS"

"
Msg 208, Level 16, State 1, Line 2
Invalid object name 'SYS.DM_DB_INDEX_PHYSICAL_STATS'. "

It runs fine on any other DB. It might be the solution to my failed indexing scripts, won't know until I get it running for a little bit.

Thanks,
Bobw


Bobw,

Hi,

Ask a question in general forum ---> http://www.sqlservercentral.com/Forums/

you got 'Invalid object name' error the DMV available on 2005 onwards.

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