Topics: Fragmentation types How to identify? How to analyze it? Resolving fragmentation issues Examples…
External Fragmentation Occurs when an index leaf page is not in logical order Known as logical fragmentation as well For example: On Insert - it is possible that new keys will be inserted in between existing keys
This may cause new index pages to be created to accommodate any existing keys that were moved so that the new keys can be inserted in correct order
These new index pages usually will not be physically adjacent to the pages the moved keys were originally stored in
External Fragmentation Index structure before any additional data inserts: An INSERT statement adds new data to the index (value=5). This insert will cause the index pages to be out of logical order. SELECT * FROM Table WHERE Data BETWEEN 4 AND 10
Internal Fragmentation Internal fragmentation occurs when the index pages are not being used to their maximum volume.
Severe internal fragmentation can lead to increased index size and cause additional reads to be performed to return needed data.
These extra reads can lead to degradation in query performance.
Internal Fragmentation Index structure before any additional data inserts: An INSERT statement adds new data to the index (value=5). This insert will cause the index page not to use the maximum volume.
How to identify? Scanning Modes : LIMITED – The fastest It scans all pages for a heap, but only the parent-level pages for an index, which are the pages above the leaf-level SAMPLED – Returns statistics based on a 1 percent sample of all the pages in the index or heap DETAILED Scans all pages and returns all statistics.
How To Analyze It?
ROW_OVERFLOW_DATA (varchar, nvarchar, varbinary, or sql_variant)
index_depth - Number of index levels
index_level - Current level of the index
(The nonleaf levels of indexes are only processed when mode = DETAILED)
avg_fragmentation_in_percent – Percentage of external fragmentation (out-of-order pages in the index)
The lower the number the better > 5% and < = 30% ALTER INDEX REORGANIZE > 30% ALTER INDEX REBUILD WITH (ONLINE = ON) fragment_count - The number of fragments in the index (physically consecutive leaf pages) avg_fragment_size_in_pages - Average number of pages in one fragment in an index (page_count/ fragment_count) Should be as high as possible page_count - Total number of index or data pages
Results (for Sampled and Detailed) avg_page_space_used_in_percent - Average percentage of available data storage space used in all pages (internal) Should be as high as possible ghost_record_count - Number of ghost records ready for removal by the ghost cleanup task (records are in the pages physically but not there logically due to deletion or update) version_ghost_record_count - Counts those records which are logically not in the table but cannot be deleted yet due to an open transaction using row versioning (running on snapshot isolation level)
Results (for Sampled and Detailed)
forwarded_record_count - Number of records in a heap that have forward pointers to another data location
(Occurs during an update, when there is not enough room to store the new row in the original location) SQL Server 2008
compressed_page_count - The number of compressed pages
Resolving Fragmentation Issues
Rebuilding Indexes (BOL)
Rebuilding an index drops and re-creates the index
reclaims disk space by compacting the pages based on the specified or existing fill factor setting
reorders the index rows in contiguous pages
ALTER INDEX PK_Employee_EmployeeID ON HumanResources.Employee REBUILD CREATE UNIQUE CLUSTERED INDEX PK_Employee_EmployeeID ON HumanResources.Employee(EmployeeID ) WITH (DROP_EXISTING = ON)
Reorganizing Indexes (BOL)
Physically reordering the leaf-level pages to match the logical, left to right, order of the leaf nodes
Compacts the index pages (based on the existing fill factor value)
Uses minimal system resources
Compaction Defrag ALTER INDEX PK_Employee_EmployeeID ON HumanResources.Employee REORGANIZE
Rebuilding vs. Reorganizing Rebuild - Re-create the index internally again and when that has been achieved, it drops the existing index Statistics are re-computed Require free space Reorganize – Physically re-organizing the leaf nodes of the index Does not update statistics Swaps one page with another and therefore does not require free space Can free up some pages (compaction and defrag) Can remove logical fragmentation Not necessarily fix extent fragmentation (in which the previous and the next extents are physically contiguous)