Pregunta

I found this question here but no answer yet:

If an index is heavily fragmented (over 50 percent) will the alter index reorganize reduce the level of the fragmentation?

¿Fue útil?

Solución

Yes it will - but only to a certain degree. Index reorg only shuffles around leaf-level pages of your index and will try to compact those - but it doesn't completely rebuild the index structure. So it can remove some fragmentation - but only on a limited scale.

That's why as a rule of thumb, for fragmentation greater than about 30% (or you might want to pick a different threshold, like 25%, depending on your situtation), you should rebuild the index - not just reindex. Rebuild also updates the statistics which are vital for good query plans.

Otros consejos

It depends on the nature of the fragmention as to whether it will or not.

Reorganize doesn't allocate new pages and so can't remove fragmentation that occurs due to non contiguous extents. It works by swapping pages. i.e. it determines the first physical page belonging to the leaf level and the first logical page in the leaf level and swaps the contents if they are different.

If I try the following I see reorganization brings the fragmentation down from 99.9% to 0.9%

CREATE TABLE T( [ID] [int] IDENTITY NOT NULL,
                 [Filler] [char](8000) NULL,
                 PRIMARY KEY CLUSTERED ([ID] DESC))

GO

INSERT INTO T DEFAULT VALUES
GO 1000

SELECT object_name(object_id) AS name, 
       page_count, 
       avg_fragmentation_in_percent, 
       fragment_count, 
       avg_fragment_size_in_pages 
FROM 
sys.dm_db_index_physical_stats(db_id(), object_id('T'), 1, NULL, 'DETAILED') 
WHERE  index_level = 0 


SET STATISTICS IO ON;
ALTER INDEX ALL ON T
REORGANIZE ; 

SELECT object_name(object_id) AS name, 
       page_count, 
       avg_fragmentation_in_percent, 
       fragment_count, 
       avg_fragment_size_in_pages 
FROM 
sys.dm_db_index_physical_stats(db_id(), object_id('T'), 1, NULL, 'DETAILED') 
WHERE  index_level = 0 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top