Index Tuning


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Index Tuning

  1. 1. Index Tuning<br />By Roi Assa<br /><br />
  2. 2. Topics:<br />Fragmentation types<br />How to identify?<br />How to analyze it?<br />Resolving fragmentation issues<br />Examples…<br />
  3. 3. Extents<br />
  4. 4. Fragmentation Types<br />
  5. 5. External Fragmentation<br />Occurs when an index leaf page is not in logical order<br />Known as logical fragmentation as well<br />For example:<br />On Insert - it is possible that new keys will be inserted in between existing keys<br /><ul><li>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
  6. 6. These new index pages usually will not be physically adjacent to the pages the moved keys were originally stored in</li></li></ul><li>External Fragmentation<br />Index structure before any additional data inserts:<br />An INSERT statement adds new data to the index (value=5). <br />This insert will cause the index pages to be out of logical order. <br />SELECT * FROM Table<br /> WHERE Data BETWEEN 4 AND 10<br />
  7. 7. Internal Fragmentation<br />Internal fragmentation occurs when the index pages are not being used to their maximum volume. <br /><ul><li>Severe internal fragmentation can lead to increased index size and cause additional reads to be performed to return needed data.
  8. 8. These extra reads can lead to degradation in query performance. </li></li></ul><li>Internal Fragmentation<br />Index structure before any additional data inserts:<br />An INSERT statement adds new data to the index (value=5). <br />This insert will cause the index page not to use the maximum volume. <br />
  9. 9. How to identify?<br />
  10. 10. How to identify?<br />select * from sys.dm_db_index_physical_stats<br />( <br /> { database_id | NULL | 0 | DEFAULT }<br /> , { object_id | NULL | 0 | DEFAULT }<br /> , { index_id | NULL | 0 | -1 | DEFAULT }<br /> , { partition_number | NULL | 0 | DEFAULT }<br /> , { mode | NULL | DEFAULT }<br />)<br />
  11. 11. How to identify?<br />Scanning Modes :<br />LIMITED – <br />The fastest<br />It scans all pages for a heap, but only the parent-level pages for an index, which are the pages above the leaf-level<br />SAMPLED – <br />Returns statistics based on a 1 percent sample of all the pages in the index or heap<br />DETAILED<br />Scans all pages and returns all statistics.<br />
  12. 12. How To Analyze It?<br />
  13. 13. Results<br /><ul><li>alloc_unit_type_desc
  14. 14. IN_ROW_DATA
  15. 15. LOB_DATA
  16. 16. ROW_OVERFLOW_DATA (varchar, nvarchar, varbinary, or sql_variant)
  17. 17. index_depth - Number of index levels
  18. 18. index_level - Current level of the index</li></ul> (The nonleaf levels of indexes are only processed when mode = DETAILED)<br />
  19. 19. Results<br /><ul><li>avg_fragmentation_in_percent – Percentage of external fragmentation (out-of-order pages in the index)</li></ul>The lower the number the better <br />&gt; 5% and &lt; = 30%  ALTER INDEX REORGANIZE<br /> &gt; 30%  ALTER INDEX REBUILD WITH (ONLINE = ON)<br />fragment_count - The number of fragments in the index (physically consecutive leaf pages)<br />avg_fragment_size_in_pages - Average number of pages in one fragment in an index (page_count/ fragment_count)<br /> Should be as high as possible<br />page_count - Total number of index or data pages<br />
  20. 20. Results (for Sampled and Detailed)<br />avg_page_space_used_in_percent - Average percentage of available data storage space used in all pages (internal)<br /> Should be as high as possible<br />ghost_record_count - Number of ghost records ready for removal by the ghost cleanup task <br /> (records are in the pages physically but not there logically due to deletion or update)<br />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)<br />
  21. 21. Results (for Sampled and Detailed)<br /><ul><li>forwarded_record_count - Number of records in a heap that have forward pointers to another data location</li></ul> (Occurs during an update, when there is not enough room to store the new row in the original location)<br />SQL Server 2008<br /><ul><li>compressed_page_count - The number of compressed pages</li></li></ul><li>Resolving Fragmentation Issues<br />
  22. 22. Rebuilding Indexes (BOL)<br /><ul><li>Rebuilding an index drops and re-creates the index
  23. 23. Rebuilding:
  24. 24. removes fragmentation
  25. 25. reclaims disk space by compacting the pages based on the specified or existing fill factor setting
  26. 26. reorders the index rows in contiguous pages</li></ul>ALTER INDEX PK_Employee_EmployeeID<br /> ON HumanResources.Employee REBUILD<br />CREATE UNIQUE CLUSTERED INDEX PK_Employee_EmployeeID<br /> ON HumanResources.Employee(EmployeeID )<br /> WITH (DROP_EXISTING = ON)<br />
  27. 27. Reorganizing Indexes (BOL)<br /><ul><li>Physically reordering the leaf-level pages to match the logical, left to right, order of the leaf nodes
  28. 28. Reorganizing:
  29. 29. Compacts the index pages (based on the existing fill factor value)
  30. 30. Uses minimal system resources
  31. 31. Two phases: </li></ul>Compaction <br />Defrag<br />ALTER INDEX PK_Employee_EmployeeID<br /> ON HumanResources.Employee REORGANIZE<br />
  32. 32. Rebuilding vs. Reorganizing<br />Rebuild - <br />Re-create the index internally again and when that has been achieved, it drops the existing index <br />Statistics are re-computed <br />Require free space<br />Reorganize – <br />Physically re-organizing the leaf nodes of the index<br />Does not update statistics<br />Swaps one page with another and therefore does not require free space<br />Can free up some pages (compaction and defrag)<br />Can remove logical fragmentation <br />Not necessarily fix extent fragmentation (in which the previous and the next extents are physically contiguous)<br />
  33. 33. Reorganizing Index<br />
  34. 34. Questions?<br /><br />