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 fragmentation is that it slows down page read-ahead throughput during index scans. This causes slower response times. If the query workload on a fragmented table or index does not involve scans, because the workload is primarily singleton lookups, removing fragmentation may have no effect.

SQL Server Database Engine automatically maintains indexes whenever insert, update, or delete operations are made to the underlying data. Over time these modifications can cause the information in the index to become scattered in the database (fragmented). Fragmentation exists when indexes have pages in which the logical ordering, based on the key value, does not match the physical ordering inside the data file. Heavily fragmented indexes can degrade query performance and cause your application to respond slowly.

You can remedy index fragmentation by reorganizing or rebuilding an index. For partitioned indexes built on a partition scheme, you can use either of these methods on a complete index or a single partition of an index. Rebuilding an index drops and re-creates the index. This removes fragmentation, reclaims disk space by compacting the pages based on the specified or existing fill factor setting, and reorders the index rows in contiguous pages. When ALL is specified, all indexes on the table are dropped and rebuilt in a single transaction. Reorganizing an index uses minimal system resources. It defragments the leaf level of clustered and nonclustered indexes on tables and views by physically reordering the leaf-level pages to match the logical, left to right, order of the leaf nodes. Reorganizing also compacts the index pages. Compaction is based on the existing fill factor value.

1,Detecting Fragmentation

The first step in deciding which defragmentation method to use is to analyze the index to determine the degree of fragmentation. By using the system function sys.dm_db_index_physical_stats, you can detect fragmentation in a specific index, all indexes on a table or indexed view, all indexes in a database, or all indexes in all databases. For partitioned indexes, sys.dm_db_index_physical_stats also provides fragmentation information for each partition.

The result set returned by the sys.dm_db_index_physical_stats function includes the following columns.


Column


Description


avg_fragmentation_in_percent


The percent of logical fragmentation (out-of-order pages in the index).


fragment_count


The number of fragments (physically consecutive leaf pages) in the index.


avg_fragment_size_in_pages


Average number of pages in one fragment in an index.

After the degree of fragmentation is known, use the following table to determine the best method to correct the fragmentation.


avg_fragmentation_in_percent value


Corrective statement


> 5% and < = 30%


ALTER INDEX REORGANIZE


> 30%


ALTER INDEX REBUILD WITH (ONLINE = ON)*

* Rebuilding an index can be executed online or offline. Reorganizing an index is always executed online. To achieve availability similar to the reorganize option, you should rebuild indexes online.

These values provide a rough guideline for determining the point at which you should switch between ALTER INDEX REORGANIZE and ALTER INDEX REBUILD. However, the actual values may vary from case to case. It is important that you experiment to determine the best threshold for your environment. Very low levels of fragmentation (less than 5 percent) should not be addressed by either of these commands because the benefit from removing such a small amount of fragmentation is almost always vastly outweighed by the cost of reorganizing or rebuilding the index.

2,Limitations and Restrictions

Indexes with more than 128 extents are rebuilt in two separate phases: logical and physical. In the logical phase, the existing allocation units used by the index are marked for deallocation, the data rows are copied and sorted, then moved to new allocation units created to store the rebuilt index. In the physical phase, the allocation units previously marked for deallocation are physically dropped in short transactions that happen in the background, and do not require many locks.

3,Reducing Fragmentation in an Index

When an index is fragmented in a way that the fragmentation is affecting query performance, there are three choices for reducing fragmentation:

  • Drop and re-create the clustered index.

    Re-creating a clustered index redistributes the data and results in full data pages. The level of fullness can be configured by using the FILLFACTOR option in CREATE INDEX. The drawbacks in this method are that the index is offline during the drop and re-create cycle, and that the operation is atomic. If the index creation is interrupted, the index is not re-created.

  • Use ALTER INDEX REORGANIZE, the replacement for DBCC INDEXDEFRAG, to reorder the leaf level pages of the index in a logical order. Because this is an online operation, the index is available while the statement is running. The operation can also be interrupted without losing work already completed. The drawback in this method is that it does not do as good a job of reorganizing the data as an index rebuild operation, and it does not update statistics.
  • Use ALTER INDEX REBUILD, the replacement for DBCC DBREINDEX, to rebuild the index online or offline.

4,Reducing Fragmentation in a Heap

To reduce the extent fragmentation of a heap, create a clustered index on the table and then drop the index. This redistributes the data while the clustered index is created. This also makes it as optimal as possible, considering the distribution of free space available in the database. When the clustered index is then dropped to re-create the heap, the data is not moved and remains optimally in position.

                       Caution                    

Creating and dropping a clustered index on a table, rebuilds all non-clustered indexes on that table twice.

5,Compacting Large Object Data

By default, the ALTER INDEX REORGANIZE statement compacts pages that contain large object (LOB) data. Because LOB pages are not deallocated when empty, compacting this data can improve disk space use if lots of LOB data have been deleted, or a LOB column is dropped.

Reorganizing a specified clustered index compacts all LOB columns that are contained in the clustered index. Reorganizing a nonclustered index compacts all LOB columns that are nonkey (included) columns in the index. When ALL is specified in the statement, all indexes that are associated with the specified table or view are reorganized. Additionally, all LOB columns that are associated with the clustered index, underlying table, or nonclustered index with included columns are compacted.

参考文档:

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

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

时间: 2024-08-07 07:52:50

Index Defragmentation的相关文章

T-SQL Recipes之Index Defragmentation

The Problem 索引一直是优化查询性能的不二法门.其中一个最直接的问题便是当审查一个低性能查询语句时,检查索引是否在正确的地方或者加索引没有.运行一个batchjob查看索引碎片,必要时采取步骤优化索引碎片是日常维护程序中不可缺少的. 今天的主题便是如何判定数据库中的索引碎片和优化措施 我们经常会用到sys.dm_db_index_physical_stats表来查看索引信息 示例: USE AdventureWorks2014 GO DECLARE @database_name VAR

Migrating Oracle on UNIX to SQL Server on Windows

Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Appendix B: Getting the Best Out of SQL Server 2000 and Windows Appendix C: Baselining Appendix D: Installing Common Drivers and Applications Installing

nginx配置后只有根目录首页index.php能访问,其他页面404

只有首页面根目录可以访问,其他页面地址都是404 not found.网上找了半天url重定向,url重写都试了无效,要不就是重定向过多,下图为跳坑历程. 1 location / { 2 #if ($http_host !~ "^www\.nginxtest\.com/index\.php$") { 3 4 #if ( !-f $request_filename ) { 5 # rewrite ^/(.+)$ http://www.nginxtest.com/index.php?$1

elasticsearch index 之 put mapping

mapping机制使得elasticsearch索引数据变的更加灵活,近乎于no schema.mapping可以在建立索引时设置,也可以在后期设置.后期设置可以是修改mapping(无法对已有的field属性进行修改,一般来说只是增加新的field)或者对没有mapping的索引设置mapping.put mapping操作必须是master节点来完成,因为它涉及到集群matedata的修改,同时它跟index和type密切相关.修改只是针对特定index的特定type. 在Action su

java.lang.IllegalArgumentException: Illegal character in query at index 261

在BaseFragment中使用了LoadingPage,而LoadingPage的联网加载使用的是AsyncHttpClient.一直报java.lang.IllegalArgumentException: Illegal character in query at index 261解析不成功,改成OkHttp解析即可. 网上有些方法,说先URLEncode再拼接,如果解决不了,换个联网请求方式,试一下.

Fulltext Index Study3:Query

在query 语句中,可以使用 contains predicate来调用Fulltext Index,实现比like速度更快的查询.使用contains能够进行term的extract匹配查询或term的前缀匹配查询,还能够进行基于词根的steming查询,基于自定义同义词文件的synonym查询,基于距离和顺序的相邻term查询.和like 相比,contains不能进行后缀匹配查询.如果Fulltext Index 能够满足业务需求,那么Fulltext Index是一个非常不错的选择,跟

TIOBE Index for January 2016(TIOBE 2016年1月编程语言排行榜 Java拿下2015年度编程语言)

Java has won the TIOBE Index programming language award of the year. This is because Java has the largest increase in popularity in one year time (+5.94%). Java leaves runner ups Visual Basic.NET (+1.51%) and Python (+1.24%) far behind. At first sigh

MySQL的loose index scan

众所周知,InnoDB采用IOT(index organization table)即所谓的索引组织表,而叶子节点也就存放了所有的数据,这就意味着,数据总是按照某种顺序存储的.所以问题来了,如果是这样一个语句,执行起来应该是怎么样的呢?语句如下: select count(distinct a) from table1; 列a上有一个索引,那么按照简单的想法来讲,如何扫描呢?很简单,一条一条的扫描,这样一来,其实做了一次索引全扫描,效率很差.这种扫描方式会扫描到很多很多的重复的索引,这样说的话优

lucene中Field.Index,Field.Store的一些设置

lucene在doc.add(new Field("content",curArt.getContent(),Field.Store.NO,Field.Index.TOKENIZED)); Field有两个属性可选:存储和索引. 通过存储属性你可以控制是否对这个Field进行存储: 通过索引属性你可以控制是否对该Field进行索引. 事实上对这两个属性的正确组合很重要. Field.Index Field.Store 说明 TOKENIZED(分词) YES 被分词索引且存储 TOKE