天天看点

sql server中index的REBUILD和REORGANIZE

<a href="http://technet.microsoft.com/en-us/library/ms188388.aspx">http://technet.microsoft.com/en-us/library/ms188388.aspx</a>

本文主要讲解如何使用alter index来rebuild和reorganize索引来清除碎片,rebuild能够完全清除碎片,但是reorganize却不能。

sql server中index的REBUILD和REORGANIZE
sql server中index的REBUILD和REORGANIZE

结论

在创建索引以后,index从8变到16,说明索引占用物理磁盘,因此索引是一个physical object。

在重建索引并设定fillfactor为60以后,我们发现data空间变大,这是因为填充因子重新使得原来装满的data page现在只装60%

fillfactor只在对已有数据create index和alter index rebuild的时候有用。对于普通的insert操作无效。

fillfactor的取值是1-100,msnd上说取0跟取100一样,但是实际测试发现使用0报错。

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. 

重新生成索引将会删除并重新创建索引。 这将根据指定的或现有的填充因子设置压缩页来删除碎片、回收磁盘空间,然后对连续页中的索引行重新排序。 如果指定 ALL,将删除表中的所有索引,然后在单个事务中重新生成。

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.

重新组织索引使用最少系统资源重新组织索引。 通过对叶级页以物理方式重新排序,使之与叶节点的从左到右的逻辑顺序相匹配,进而对表和视图中的聚集索引和非聚集索引的叶级进行碎片整理。 重新组织还会压缩索引页。 压缩基于现有的填充因子值。

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.

rebulid index既可以在online又可以在offline下执行,而reorganize index只能在online下执行的。

rebuilde index会对table上锁,所有对这个table的读写操作都会被阻塞,在这期间新索引根据旧索引来创建,其实就是一个复制的过程,但是新索引没有碎片,最后使用新索引替换旧索引。当rebuild整个过程完成以后,table上面的锁才会被释放。

 本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2012/07/31/2617390.html,如需转载请自行联系原作者