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

結論

在創建索引以後,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上面的鎖纔會被釋放。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章