|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, February 28, 2009 6:51 AM
Points: 1,489,
Visits: 7
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, November 01, 2006 12:25 AM
Points: 19,
Visits: 1
|
|
One of the best things about books online is the free code. If you look up dbcc showcontig you will see a routine on the help page that retrieves the table output of the dbcc and then runs index defrag against any index fragged at or above 30% this is easily modified to be a rebuild or a reindex.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, March 08, 2010 2:50 PM
Points: 54,
Visits: 77
|
|
I have a server where there is heavy file fragmentation but it is quite busy and cannot be down for long periods of time. Is there any way other than shutting down SQL Server that would allow me to run a defrag on the datafiles while the the database files are in use?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, February 28, 2009 6:51 AM
Points: 1,489,
Visits: 7
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, December 10, 2008 8:47 AM
Points: 20,
Visits: 5
|
|
| One thing that you forgot to mention, when rebuilding the indexes you can specify a fill factor for the index. This is helpful if you have a clustered index where the new record will be inserted between existing records. Not as helpful, when all new records are inserted at the end of the table.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, March 02, 2010 4:10 PM
Points: 39,
Visits: 45
|
|
Hello!!! I have been in trouble with my SQL Query that was causing time out on the UI...Investigation with DBCC showcontig reveiled that the Scandensity for these tables are 50% or less. But to my surprise the Scandensity was not improving with REINDEXing and DEFRAG....(Number of rows in these tables are a few hundred only) . I am not sure why is this happening.... to some how escape I tried moving these tables to new ones and saw some improvement in the Scandensity (Query to a improved slightly). But what puzzles me is why REINDEXing and DEFRAG did't work???? Also one more question.... If I make a backup of this database and restore into a new server will that create new data/index pages?? does that have the same fragmentation as when it was backed up??? or does the RESTORE just uncompress the old pages??? Thanks Cheriyan.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, October 22, 2009 5:53 AM
Points: 894,
Visits: 318
|
|
DisKeeper claims to be able to defrag locked files. I've used it a couple times in production. A very nice feature it has is that it monitors the IO queue. If the queue rises, it backs off and allows the system more time on the disks. The effect is zero (Near zero) impact while doing a defrag. The con is your defrag could take forever. But you have the time...
Cheers, Crispin
I can't die, there are too many people who still have to meet me!
It's not a bug, SQL just misunderstood me!
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, October 22, 2009 5:53 AM
Points: 894,
Visits: 318
|
|
On a whole, a good article. One correction though. When CREATE INDEX, WITH DROP_EXISTING is executed on a clustsered index, all non clustered indexes have to be rebuild. A NC index contains keys to the clustered index, i.e. the data. When you rebuuild this, the data could, and probably will, move to another page. This would cause the key link to become invalid. See BOL: CREATE INDEX, DROP_EXISTING "Because nonclustered indexes contain the clustering keys, the nonclustered indexes must be rebuilt when a clustered index is dropped. If a clustered index is recreated, the nonclustered indexes must be rebuilt to take the new set of keys into account" I learnt this the hard way... 
Cheers, Crispin
I can't die, there are too many people who still have to meet me!
It's not a bug, SQL just misunderstood me!
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, October 22, 2009 5:53 AM
Points: 894,
Visits: 318
|
|
To try and answer your first question: If you only have a few rows (couple could be a few) in a table, SQL creates those pages on an extent which is shared by other pages. An extent is 64k, page 8. If you have a table with narrow rows and only a couple hundred rows, the chance is good you using a mixed extent. With this in mind, SQL may not defrag it because a) it's so small, b) there's other data sitting on the extent. Try adding a couple thousand rows to the table (Assuming you can) and see if that changes things. See BOL, pages and extents for more info. To your second question, yes. When SQL does a restore, it restores all tables. One of them being sysindexes. Sysindexes determines on what pages / extents sit. DUring a restore, SQL acquires a chuck of disk space. It then creates all the extents / pages. Once done, it starts writing out the data page by page. (or extent by extent?). Restoring a backup will only give you contiguous OS space, not within the SQL files.
Cheers, Crispin
I can't die, there are too many people who still have to meet me!
It's not a bug, SQL just misunderstood me!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, February 28, 2009 6:51 AM
Points: 1,489,
Visits: 7
|
|
Hm, now I do not have the experience from learning the hard way about this (not that I can recall at least). However, you did not copy-paste all what BOL says:
"The DROP_EXISTING clause enhances performance when re-creating a clustered index (with either the same or a different set of keys) on a table that also has nonclustered indexes. The DROP_EXISTING clause replaces the execution of a DROP INDEX statement on the old clustered index followed by the execution of a CREATE INDEX statement for the new clustered index. The nonclustered indexes are rebuilt once, and only if the keys are different.
If the keys do not change (the same index name and columns as the original index are provided), the DROP_EXISTING clause does not sort the data again. This can be useful if the index must be compacted."
To me, this seems to say that if you do not change the keys (e.g. run this simply to defrag the index) then the NC indexes do not need to be rebuilt at all. Even if the keys do change, the NCs only need to be rebuilt once instead of twice (one for DROP INDEX and one for CREATE INDEX). This last part is something that my article should have mentioned.
Also, this is not correct:
A NC index contains keys to the clustered index, i.e. the data. When you rebuild this, the data could, and probably will, move to another page. This would cause the key link to become invalid.
If the keys of the clustered index stay the same then the 'links' in the NCs are not invalid even if rows move to different pages. That is the whole idea of the clustered index key, instead of the NCs pointing directly to a physical oage (with a RID), they store the key that can be used to seek the clustered index to find the row.
-- Chris Hedgate http://www.hedgate.net/ Contributor to the Best of SQL Server Central volumes Articles: http://www.sqlservercentral.com/columnists/chedgate/
|
|
|
|