sql server中index的REBUILD和REORGANIZE

转自:https://www.cnblogs.com/flysun0311/archive/2013/12/05/3459451.html

 

参考文献:

http://technet.microsoft.com/en-us/library/ms188388.aspx

正文

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

Rebuild index

--1.准备实验数据
select * into Employee from AdventureWorks2008R2.HumanResources.Employee; --2.查看使用空间:Employee 290 72 KB 56 KB 8 KB 8 KB
sp_spaceused Employee --3.创建聚集索引
create clustered index IX_BusinessEntityID on Employee(BusinessEntityID); --4.查看使用空间:Employee 290 80 KB 56 KB 16 KB 8 KB
sp_spaceused Employee --5.索引重建,清除fragment,并设定fillfactor为60
ALTER INDEX ALL ON Employee
REBUILD WITH (FILLFACTOR = 60, SORT_IN_TEMPDB = ON,
STATISTICS_NORECOMPUTE = ON); --6.查看使用空间:Employee 290 144 KB 88 KB 16 KB 40 KB
sp_spaceused Employee

结论

  1. 在创建索引以后,index从8变到16,说明索引占用物理磁盘,因此索引是一个physical object。
  2. 在重建索引并设定fillfactor为60以后,我们发现data空间变大,这是因为填充因子重新使得原来装满的data page现在只装60%
  3. fillfactor只在对已有数据create index和alter index rebuild的时候有用。对于普通的insert操作无效。
  4. fillfactor的取值是1-100,msnd上说取0跟取100一样,但是实际测试发现使用0报错。

reorganize index

ALTER INDEX ALL ON Employee
REORGANIZE
GO

两者的区别

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下执行的。

Difference between rebuild index online and offline(PS:2012-9-11)

既然rebuild index既可以是online模式,也可以是offline模式,那么两者有什么区别呢。这个我们可以参考stackoverflow上面的一篇文章:What is the difference between OFFLINE and ONLINE index rebuild in SQL Server? 在这里我还是简要总结一下:

online模式下

rebuild index会复制旧索引来新建索引,此时旧的索引依然可以被读取和修改,但是所以在旧索引上的修改都会同步更新到新索引下。中间会有一些冲突解决机制,具体参考Online Index Operations 里面的Build Phase这一章节。然后在rebuild这个过程完整的时候,会对table上锁一段时间,在这段时间里会用新索引来替换旧索引,当这个过程完成以后再释放table上面的锁。如果索引列包含 LOB对象的话,在SQL Server 2005/2008/R2中rebuild index online会失败。在sql server 2012中,即使索引列包含LOB对象,也可以rebuild index online了,可以参考 Online Index Operations for indexes containing LOB columns.

offline模式下

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

最新文章

  1. 解决Unity3D导出apk失败:Failed to re-package resources
  2. GridView和DATAGRID前后台查询用法的比较
  3. jQuery.validationEngine.js学习
  4. webpack入坑之旅(五)加载vue单文件组件
  5. 什么是商业智能BI和实施BI的解决方案【转】
  6. Win10系统下安装Tensorflow
  7. document.write() 和 document.writeln() 区别
  8. vs2012 arcgis engine 10 丢失arcgis模板
  9. FPGA的JTAG下载问题
  10. inner join
  11. scp的使用
  12. Android 切横竖屏时走的生命周期方法?222
  13. BZOJ1115[POI2009]石子游戏——阶梯Nim游戏
  14. ALGO-139_蓝桥杯_算法训练_s01串(递归)
  15. c语言:辗转相除法求最大公约数、最小公倍数
  16. 浅析Linux操作系统是如何工作的(思维导图)
  17. python基础===对字符串进行左右中对齐
  18. Java基础(三)选择和循环结构
  19. ZeroMQ作者于昨天下午宣布选择安乐死
  20. centos7 下 的lamp 的安装原创详细教程

热门文章

  1. 无需序列号安装Photoshop CS6
  2. hive export import命令
  3. 剖析top命令显示的VIRT RES SHR值
  4. Android开发人员必须掌握的10 个开发工具+应该深入学习的10个开源应用项目
  5. 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)
  6. Flash AS 2.0右键菜单问题
  7. Effective JavaScript Item 55 接受配置对象作为函数參数
  8. flask-第三方组件
  9. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.1——单元测试
  10. Castle 整合.NET Remoting