Index Fragmentation Detect

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

时间: 2024-08-25 12:42:52

Index Fragmentation Detect的相关文章

查找索引碎片Find Index Fragmentation Details – Slow Index Performance

SQL SERVER – 2005 – Find Index Fragmentation Details – Slow Index Performance Just a day ago, while using one index I was not able to get the desired performance from the table where it was applied. I just looked for its fragmentation and found it wa

Index Fragmentation Report in SQL Server 2005 and 2008

ProblemWhile indexes can speed up execution of queries several fold as they can make the querying process faster, there is overhead associated with them. They consume additional disk space and require additional time to update themselves whenever dat

Index Defragmentation

如果使用sys.dm_db_index_physical_stats 检查Index的 avg_fragmentation_in_percent 是一个非常大的数值,那么就要检查fragmentation是否影响到性能,如果导致性能下降,那么就需要 remove fragmentation. Fragmentation alone is not a sufficient reason to reorganize or rebuild an index. The main effect of fr

Maintenance Plan Usage3:Task Usage (Maintain Index)

Maintenance Plan用于维护Index Fragmentation 和 Statistics的Task 共有三个,后台是使用相应的TSQL来进行data维护. Reorganize Index Task Move index pages into a more efficient search order. This task uses the ALTER INDEX REORGANIZE statement with SQL Server 2012 databases. Rebui

QUOTED_IDENTIFIER 选项对 index的影响

SET QUOTED_IDENTIFIER 选项对Index的影响 当创建或修改的index包含computed columns ,必须 SET QUOTED_IDENTIFIER=ON: 当创建或修改Indexed View上的Index时,必须 SET QUOTED_IDENTIFIER=ON: 当创建或修改filtered index时,必须 SET QUOTED_IDENTIFIER=ON: 一,Case 早上,发现用于处理Index fragmentation的Job跑失败了,查看jo

非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛

-- SQL Server 2012 Diagnostic Information Queries -- Glenn Berry -- April 2015 -- Last Modified: April 27, 2015 -- http://sqlserverperformance.wordpress.com/ -- http://sqlskills.com/blogs/glenn/ -- Twitter: GlennAlanBerry -- Please listen to my Plura

SQL Server FullCheck

-- ----------------------------------------------------------------------------------- -- --1. 数据库 ----------------------------------------------------------------------------------- -- --1.查看数据库版本信息 select @@version --2.查看所有数据库名称及大小 exec sp_helpdb -

《Pro SQL Server Internals》部分翻译(P36-P45)

本文选自<Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress 出版年: 2016-12-29 作者简介:Dmitri Korotkevitchis是微软SQL Server MVP和微软认证大师.作为应用程序和数据库开发人员.数据库管理员和数据库架构师,他具有多年使用SQL Server的经验.他专门从事OLTP系统在高负载下的设计.开发和性能调优.Dmitri经常在各种Microsoft和SQL PASS活动上发言,他为

《Pro SQL Server Internals》翻译之索引

本文选自<Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress 出版年: 2016-12-29 页数: 804 作者简介:Dmitri Korotkevitchis是微软SQL Server MVP和微软认证大师.作为应用程序和数据库开发人员.数据库管理员和数据库架构师,他具有多年使用SQL Server的经验.他专门从事OLTP系统在高负载下的设计.开发和性能调优.Dmitri经常在各种Microsoft和SQL PASS