sql server中index的REBUILD和REORGANIZE
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了sql server中index的REBUILD和REORGANIZE,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3744字,纯文字阅读大概需要6分钟。
内容图文
![sql server中index的REBUILD和REORGANIZE](/upload/InfoBanner/zyjiaocheng/918/8df5ba0c81dd40cc8f4453ec9840a524.jpg)
参考文献:
http://technet.microsoft.com/en-us/library/ms188388.aspx
使用alter index来rebuild和reorganize索引来清除碎片,rebuild能够完全清除碎片,但是reorganize却不能。
--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
结论
- 在创建索引以后,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报错。
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上面的锁才会被释放。
内容总结
以上是互联网集市为您收集整理的sql server中index的REBUILD和REORGANIZE全部内容,希望文章能够帮你解决sql server中index的REBUILD和REORGANIZE所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。