Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

INDEX DEFRAGMENTATION SCRIPT Expand / Collapse
Author
Message
Posted Thursday, February 4, 2010 2:03 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 7:38 AM
Points: 485, Visits: 840
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.
Post #859415
Posted Thursday, February 11, 2010 5:23 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 7:38 AM
Points: 485, Visits: 840
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.
Post #864013
Posted Thursday, February 11, 2010 5:38 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 7:12 AM
Points: 366, Visits: 455
Thanks Mutthu...this goes to my briefcase.
Post #864017
Posted Friday, February 12, 2010 3:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 7:16 AM
Points: 6, Visits: 63
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
Post #864517
Posted Friday, February 12, 2010 3:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 7:16 AM
Points: 6, Visits: 63
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
Post #864519
Posted Saturday, February 13, 2010 3:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 7:21 AM
Points: 1,159, Visits: 4,658
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
Post #865062
Posted Monday, May 3, 2010 1:26 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 7:21 AM
Points: 1,159, Visits: 4,658
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
Post #914505
Posted Sunday, March 13, 2011 12:36 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 7:21 AM
Points: 1,159, Visits: 4,658
Click here:INDEX DE-FRAGMENTATION SCRIPT for SQL server 2000

Edit: to Fix the link


Muthukkumaran Kaliyamoorthy

Helping SQL DBAs and Developers >>>SqlserverBlogForum
Post #1077420
Posted Wednesday, November 30, 2011 9:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 13, 2014 9:09 AM
Points: 3, Visits: 253
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
Post #1214054
Posted Wednesday, November 30, 2011 11:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 7:21 AM
Points: 1,159, Visits: 4,658
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
Post #1214164
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse