sql server中index的REBUILD和REORGANIZE

https://www.cnblogs.com/flysun0311/archive/2013/12/05/3459451.htmlhtml

參考文獻:
http://technet.microsoft.com/en-us/library/ms188388.aspxnode

正文
本文主要講解如何使用alter index來rebuild和reorganize索引來清除碎片,rebuild可以徹底清除碎片,可是reorganize卻不能。sql

Rebuild indexide

--1.準備實驗數據
select * into Employee from AdventureWorks2008R2.HumanResources.Employee;測試

--2.查看使用空間:Employee 290 72 KB 56 KB 8 KB 8 KB
sp_spaceused Employeeui

--3.建立彙集索引
create clustered index IX_BusinessEntityID on Employee(BusinessEntityID);spa

--4.查看使用空間:Employee 290 80 KB 56 KB 16 KB 8 KB
sp_spaceused Employeeserver

--5.索引重建,清除fragment,並設定fillfactor爲60
ALTER INDEX ALL ON Employee
REBUILD WITH (FILLFACTOR = 60, SORT_IN_TEMPDB = ON,
STATISTICS_NORECOMPUTE = ON);htm

--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上面的鎖纔會被釋放。

相關文章
相關標籤/搜索