• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Index Tuning

Index Tuning






Total Views
Views on SlideShare
Embed Views



2 Embeds 40

http://www.sqlserver.co.il 39
http://newmail.walla.co.il 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Index Tuning Index Tuning Presentation Transcript

    • Index Tuning
      By Roi Assa
    • Topics:
      Fragmentation types
      How to identify?
      How to analyze it?
      Resolving fragmentation issues
    • Extents
    • Fragmentation Types
    • 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?
    • How to identify?
      select * from sys.dm_db_index_physical_stats
      { database_id | NULL | 0 | DEFAULT }
      , { object_id | NULL | 0 | DEFAULT }
      , { index_id | NULL | 0 | -1 | DEFAULT }
      , { partition_number | NULL | 0 | DEFAULT }
      , { mode | NULL | DEFAULT }
    • 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
      Scans all pages and returns all statistics.
    • How To Analyze It?
    • Results
      • alloc_unit_type_desc
      • IN_ROW_DATA
      • LOB_DATA
      • 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)
    • Results
      • 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
      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
      • Rebuilding:
      • removes fragmentation
      • 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
      ON HumanResources.Employee(EmployeeID )
    • Reorganizing Indexes (BOL)
      • Physically reordering the leaf-level pages to match the logical, left to right, order of the leaf nodes
      • Reorganizing:
      • Compacts the index pages (based on the existing fill factor value)
      • Uses minimal system resources
      • Two phases:
      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)
    • Reorganizing Index
    • Questions?