码迷,mamicode.com
首页 > 其他好文 > 详细

Index Fragmentation Detect

时间:2016-01-16 19:24:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

1,Fragmentation分为Internal Fragmentation and External Fragmentation。

Internal fragmentation 是指(index or data)page内部的fragmentation,在Page内部存在没有使用的space。

Internal fragmentation (often called physical fragmentation or page desnsity) means that index pages have wasted space, both at the leaf and non-leaf levels. Internal fragmentation means the index is taking more space than necessary, leading to increased disk space usage, more pages to read to process the data, and more memory used to hold the pages in the buffer pool.

 

External fragmentation 是指 pages 或 extents 的logical order 和 physical order 是不一致的,Logical order 是由 index key定义的,physical order 是page或extent 在disk space实际存储的order。如果在逻辑上连续的page或extent在物理上也是contiguous的,那么就不存在External fragmentation。

External fragmentation (often called logical fragmentation or extent fragmentation) means the pages or extents comprising the leaf level of a clustered or nonclustered index aren‘t in the most efficient order. The most efficient order is where the logical order of the pages and extents(as defined by the index keys, following the next-page pointers from the page headers) is the same as the physical order of the pages and extents with the data files. In other words, the index leaf-lelvel page that has the row with the next index key is also the next physical contiguous page int the data file.

 

SQL Server 提供DMF: sys.dm_db_index_physical_stats,用于查看External fragmentation,External Fragmentation 分为 Logical 和 Extent Fragmentation。如果扫描的table 有Clustered Index(BTree Structure),那么 DMF返回的Fragmentation是Pages 数量;如果扫描的table没有clustered index(Heap Structure),那么DMF返回的Fragmentation是Extents 数量,因为Heap的 Pages 是没有顺序的。在Heap的 Page header中,字段 next_page 和 Pre_page pointer是null。

Logical Fragmentation              

This is the percentage of out-of-order pages in the leaf pages of an index. An out-of-order page is a page for which the next physical page allocated to the index is not the page pointed to by the next-page pointer in the current leaf page.

Extent Fragmentation              

This is the percentage of out-of-order extents in the leaf pages of a heap. An out-of-order extent is one for which the extent that contains the current page for a heap is not physically the next extent after the extent that contains the previous page.

 

Fragmentation是由于对数据的更新(insert,update和update),导致数据的存储的物理顺序和index key定义的逻辑顺序不一致而产生的,少量的Fragmentation是有利的,能够减少page split,提高更新性能;大量的fragmentation是有害的,增加IO的次数,降低查询性能。

Fragmentation occurs through the process of data modifications (INSERT, UPDATE, and DELETE statements) that are made against the table and, therefore, to the indexes defined on the table. Because these modifications are not ordinarily distributed equally among the rows of the table and indexes, the fullness of each page can vary over time. For queries that scan part or all of the indexes of a table, this kind of fragmentation can cause additional page reads. This hinders parallel scanning of data.

 

2,DMF:sys.dm_db_index_physical_stats 用法

2.1 Syntax

Returns size and fragmentation information for the data and indexes of the specified table or view.

For an index, one row is returned for each level of the B-tree in each partition.

For a heap, one row is returned for the IN_ROW_DATA allocation unit of each partition.

For large object (LOB) data, one row is returned for the LOB_DATA allocation unit of each partition.

If row-overflow data exists in the table, one row is returned for the ROW_OVERFLOW_DATA allocation unit in each partition.

Does not return information about xVelocity memory optimized columnstore indexes.

Syntax

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 }
)

mode | NULL | DEFAULT              

Is the name of the mode. mode specifies the scan level that is used to obtain statistics. mode is sysname. Valid inputs are DEFAULT, NULL, LIMITED, SAMPLED, or DETAILED. The default (NULL) is LIMITED.

 

2.2 Scanning Modes

Mode 参数 indicate scan mode和返回信息的详细程度。在扫描的过程中,需要在扫描对象上申请IS table lock。

Limited Mode 不会扫描data pages,对于Index,扫描Leaf Level的Parent-level pages;对于Heap,扫描Object对应的IAM 和 PFS pages。

Detailed Mode 扫描all papges,耗时最久,返回的信息最详细。

 

The mode in which the function is executed determines the level of scanning performed to obtain the statistical data that is used by the function. mode is specified as LIMITED, SAMPLED, or DETAILED. The function traverses the page chains for the allocation units that make up the specified partitions of the table or index. sys.dm_db_index_physical_stats requires only an Intent-Shared (IS) table lock, regardless of the mode that it runs in.

The LIMITED mode is the fastest mode and scans the smallest number of pages. For an index, only the parent-level pages of the B-tree (that is, the pages above the leaf level) are scanned. For a heap, the associated PFS and IAM pages are examined and the data pages of a heap aren‘t scanned in LIMITED mode.

With LIMITED mode, compressed_page_count is NULL because the Database Engine only scans non-leaf pages of the B-tree and the IAM and PFS pages of the heap.

Use SAMPLED mode to get an estimated value for compressed_page_count, and use DETAILED mode to get the actual value for compressed_page_count. The SAMPLED mode returns statistics based on a 1 percent sample of all the pages in the index or heap. Results in SAMPLED mode should be regarded as approximate. If the index or heap has fewer than 10,000 pages, DETAILED mode is used instead of SAMPLED.

The DETAILED mode scans all pages and returns all statistics.The modes are progressively slower from LIMITED to DETAILED, because more work is performed in each mode. To quickly gauge the size or fragmentation level of a table or index, use the LIMITED mode. It is the fastest and will not return a row for each nonleaf level in the IN_ROW_DATA allocation unit of the index.

 

2.3 Critical Columns

avg_fragmentation_in_percent

Logical fragmentation for indexes, or extent fragmentation for heaps in the IN_ROW_DATA allocation unit.

标识Fragmentation的百分比,MS推荐的值是0-10.

fragment_count

Number of fragments in the leaf level of an IN_ROW_DATA allocation unit.

avg_fragment_size_in_pages

Average number of pages in one fragment in the leaf level of an IN_ROW_DATA allocation unit.

avg_page_space_used_in_percent

Average percentage of available data storage space used in all pages.

For an index, average applies to the current level of the b-tree in the IN_ROW_DATA allocation unit.

For a heap, the average of all data pages in the IN_ROW_DATA allocation unit.

 

3,Detect Fragmentation

Script1 Used to detect fragmentations

select ps.database_id,ps.object_id,ps.index_id,ps.partition_number,ps.index_type_desc,
    ps.alloc_unit_type_desc,
    ps.index_depth,
    ps.index_level,
    ps.avg_fragmentation_in_percent,
    ps.fragment_count,
    ps.avg_fragment_size_in_pages,
    ps.page_count,
    ps.avg_page_space_used_in_percent,
    ps.record_count,
    ps.ghost_record_count,
    ps.version_ghost_record_count,
    ps.min_record_size_in_bytes,
    ps.max_record_size_in_bytes,
    ps.avg_record_size_in_bytes,
    ps.forwarded_record_count,
    ps.compressed_page_count
from sys.dm_db_index_physical_stats(db_id(),object_id(dbo.FactProduct‘),1,1,detailed‘) as ps
order by ps.index_level

The fragmentation level of an index or heap is shown in the avg_fragmentation_in_percent column.

For heaps, the value represents the extent fragmentation of the heap. For indexes, the value represents the logical fragmentation of the index. Unlike DBCC SHOWCONTIG, the fragmentation calculation algorithms in both cases consider storage that spans multiple files and, therefore, are accurate.

The value for avg_fragmentation_in_percent should be as close to zero as possible for maximum performance. However, values from 0 percent through 10 percent may be acceptable. All methods of reducing fragmentation, such as rebuilding, reorganizing, or re-creating, can be used to reduce these values.

 

参考文档:

https://technet.microsoft.com/en-us/library/ms188917(v=sql.110).aspx

Index Fragmentation Detect

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5129860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!