sql server中index的REBUILD和REORGANIZE
参考文献:
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

结论
- 在创建索引以后,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的区别及工作方式
sql server中index的REBUILD和REORGANIZE 转自:https://www.cnblogs.com/flysun0311/archive/2013/12/05/3459451 ...
- Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...
- 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...
- SQL Server中是否可以准确获取最后一次索引重建的时间?
在SQL Server中,我们能否找到索引的创建时间?最后一次索引重建(Index Rebuild)的时间? 最后一次索引重组(INDEX REORGANIZE)的时间呢? 答案是我们无法准确的找到 ...
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- SQL Server中的索引
1 SQL Server中的索引 索引是与表或视图关联的磁盘上结构,可以加快从表或视图中检索行的速度.索引包含由表或视图中的一列或多列生成的键.这些键存储在一个结构(B 树)中,使 SQL Serve ...
- 转载: SQL Server中的索引
http://www.blogjava.net/wangdetian168/archive/2011/03/07/347192.html 1 SQL Server中的索引 索引是与表或视图关联的磁盘上 ...
- 理解SQL Server中索引的概念
T-SQL查询进阶--理解SQL Server中索引的概念,原理以及其他 简介 在SQL Server中,索引是一种增强式的存在,这意味着,即使没有索引,SQL Server仍然可以实现应有的功能 ...
- SQL Server中事务日志管理的步骤,第5级:完全恢复模式管理日志(译)
SQL Server中事务日志管理的步骤,第5级:完全恢复模式管理日志 作者:Tony Davis,2012/01/27 系列 本文是进阶系列的一部分:SQL Server中事务日志管理的步骤 当事情 ...
随机推荐
- groovy
1.加载和卸载(每次都新建一个GroovyClassLoader 实例,然后使用新建的classloader去加载) try { GroovyClassLoader groovyClassLoader ...
- github上所有大于800 star OC框架
https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID ...
- search搜索功能
1.html <div class="search"> <form name="formsearch" action=" ...
- Failed to load PDF in chrome/Firefox/IE
笔者的公司搭建了一个Nexus服务器,用来管理我们自己的项目Release构件和Site文档. 今天的问题是当用户访问一个Site里的PDF文件的时候,报错说“detected that the ne ...
- 30天,O2O速成攻略【8.22北京站】
活动概况 时间:2015年8月22日13:30-16:30 地点:车库咖啡(北京市海淀西大街48号鑫鼎宾馆二层) 主办:APICloud.融云.品读者 网址:www.apicloud.com 费用:免 ...
- 10月26日 奥威Power-BI基于微软示例库(MSOLAP)快速制作管理驾驶舱 腾讯课堂开课啦
本次课是基于olap数据源的案例实操课,以微软olap示例库Adventure Works为数据基础. AdventureWorks示例数据库为一家虚拟公司的数据,公司背景为大型跨国生产 ...
- javascript设计模式学习之二——this
一.this指向问题 1)默认绑定,即作为独立的普通函数调用 此时this指向全局对象window,如果是严格模式下,则指向undefined; 2)隐式绑定,即具有调用上下文(一种场景就是作为对象的 ...
- 前端实战——前端效果accordition的实现
一.bootstrap实现 1)水平折叠组件 使用panel和collaspe组件 <!doctype html> <html lang="zh-hans"> ...
- MFC项目中包含atlimage.h导致fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
因为要用到CImage所以包含了atlimage.h 报这个错误的话你只需要把atlimage.h放在afxwin.h的下方即可,不能让它在afxwin.h的上方
- subprocess使用
1. Popen使用 test = subprocess.Popen('ls /tmpa', shell=True, stdout = subprocess.PIPE, stderr=subproce ...