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

結論

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

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM