Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
imarchenko
Yak Posting Veteran
57 Posts |
Posted - 2006-10-03 : 17:44:31
|
Hello! I have noticed that when executing DBCC INDEXDEFRAG on big table (SQL Server 2000 SP4), transaction log starts growing significantly. I was hoping that INDEXDEFRAG is implemented as a serious of short transactions and shouldn't cause significant transaction log growth. Our database is in SIMPLE recovery mode. We have the same database/defragmentation process running on SQL Server 2005 and never experienced this problem. Any thoughts on this matter are greatly appreciated.Thanks,Igor |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-03 : 18:54:20
|
Just imagine what your tlog would look like if you ran DBCC DBREINDEX! Both require tlog space, but DBCC INDEXDEFRAG uses significantly less space.Tara Kizer |
 |
|
Luis Martin
Yak Posting Veteran
54 Posts |
Posted - 2006-10-03 : 20:26:29
|
One alternative is to defrag only those indexes fragmented.Try this one script.-- SP code starts here/* uso: sp_defragment_indexes %defrag en decimal */CREATE PROCEDURE sp_defragment_indexes @maxfrag DECIMALAS/* This stored procedure checks index fragmentation in a database and defragmentsindexes whose scan densities fall below a specified threshold, @magfrag, whichis passed to the SP.Must be run in the database to be defragmented.*/-- Declare variablesSET NOCOUNT ONDECLARE @tablename VARCHAR (128)DECLARE @execstr VARCHAR (255)DECLARE @objectid INTDECLARE @indexid INTDECLARE @frag DECIMALDECLARE @indexname CHAR(255)DECLARE @dbname sysnameSELECT @dbname = db_name()IF @dbname IN ('master', 'msdb', 'model', 'tempdb')BEGINPRINT 'This procedure should not be run in system databases.'RETURNEND-- Declare cursorDECLARE tables CURSOR FORSELECT so.nameFROM sysobjects soJOIN sysindexes siON so.id = si.idWHERE so.type ='U'AND si.indid < 2AND si.rows > 0-- Create the tableCREATE TABLE #fraglist (ObjectName CHAR (255),ObjectId INT,IndexName CHAR (255),IndexId INT,Lvl INT,CountPages INT,CountRows INT,MinRecSize INT,MaxRecSize INT,AvgRecSize INT,ForRecCount INT,Extents INT,ExtentSwitches INT,AvgFreeBytes INT,AvgPageDensity INT,ScanDensity DECIMAL,BestCount INT,ActualCount INT,LogicalFrag DECIMAL,ExtentFrag DECIMAL)-- Open the cursorOPEN tables-- Loop through all the tables in the databaseFETCH NEXTFROM tablesINTO @tablenameWHILE @@FETCH_STATUS = 0BEGIN-- Do the showcontig of all indexes of the tableINSERT INTO #fraglist EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')FETCH NEXTFROM tablesINTO @tablenameEND-- Close and deallocate the cursorCLOSE tablesDEALLOCATE tablesSELECT * FROM #fraglist-- Declare cursor for list of indexes to be defraggedDECLARE indexes CURSOR FORSELECT ObjectName, ObjectId, IndexName, ScanDensityFROM #fraglistWHERE ScanDensity <= @maxfragAND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0SELECT 'Started defragmenting indexes at ' + CONVERT(VARCHAR,GETDATE())-- Open the cursorOPEN indexes-- loop through the indexesFETCH NEXTFROM indexesINTO @tablename, @objectid, @indexname, @fragWHILE @@FETCH_STATUS = 0BEGINSELECT @execstr = 'DBCC DBREINDEX (' + RTRIM(@tablename) + ',' + RTRIM(@indexname) + ') WITH NO_INFOMSGS'SELECT (@execstr)EXEC (@execstr)FETCH NEXTFROM indexesINTO @tablename, @objectid, @indexname, @fragEND-- Close and deallocate the cursorCLOSE indexesDEALLOCATE indexesSELECT 'Finished defragmenting indexes at ' + CONVERT(VARCHAR,GETDATE())-- Delete the temporary tableDROP TABLE #fraglistGO |
 |
|
Kristen
Test
22859 Posts |
|
Luis Martin
Yak Posting Veteran
54 Posts |
Posted - 2006-10-04 : 13:07:37
|
I get that exactly from the same source. But I used years ago so I copy and paste from my laptop.Orginal is from Tom Pullen.Also I modify original to spanish version.All in Love is Fair Stevie Wonder |
 |
|
imarchenko
Yak Posting Veteran
57 Posts |
Posted - 2006-10-04 : 16:37:29
|
Thanks for your feedback, guys! My expectations were that since DBCC INDEXDEFRAG is implemented as series of short transactions and database is in SIMPLE recovery mode, transaction log shouldn't be growing significantly. I have a similar automated process that checks fragmentation level and decided whether to run DBCC DBREINDEX or DBCC INDEXDEFRAG.Thanks,Igor |
 |
|
|
|
|
|
|