Index Reorganize 和 Rebuild 的區別

來源:互聯網
上載者:User

標籤:

對Index 進行 Reorganize 和 Rebuild 是有區別的。

1,語義區別

Rebuild 是重新建立,將Index之前佔用的空間釋放,重新申請空間來建立index。Rebuilding an index means that a whole new set of pages is allocated for it。

Reorganize 是重新組織,作用於 index leaf level pages。Reorganizing an index compacts the leaf-level pages back to their original specified fillfactor ant then rearrages the pages at the leaf level pages to correct the logical fragmentation, using the same pages that the index originally occupied.No new pages are allocated.

2, Different Syntax

ALTER INDEX index_name    ON <object>{     REBUILD         [ [PARTITION = ALL] [ WITH ( <rebuild_index_option> [ ,...n ] ) ]           | [ PARTITION = partition_number [ WITH ( <single_partition_rebuild_index_option> [ ,...n ] )] ]        ]    | REORGANIZE         [ PARTITION = partition_number ]        [ WITH ( LOB_COMPACTION = { ON | OFF } ) ]}[ ; ]<rebuild_index_option > ::={    PAD_INDEX = { ON | OFF }  | FILLFACTOR = fillfactor   | SORT_IN_TEMPDB = { ON | OFF }  | IGNORE_DUP_KEY = { ON | OFF }  | STATISTICS_NORECOMPUTE = { ON | OFF }  | ONLINE = { ON | OFF }   | ALLOW_ROW_LOCKS = { ON | OFF }  | ALLOW_PAGE_LOCKS = { ON | OFF }  | MAXDOP = max_degree_of_parallelism  | DATA_COMPRESSION = { NONE | ROW | PAGE }      [ ON PARTITIONS ( { <partition_number_expression> | <range> }      [ , ...n ] ) ]}<range> ::= <partition_number_expression> TO <partition_number_expression><single_partition_rebuild_index_option> ::={    SORT_IN_TEMPDB = { ON | OFF }  | MAXDOP = max_degree_of_parallelism  | DATA_COMPRESSION = { NONE | ROW | PAGE } }}

REBUILD [ WITH (<rebuild_index_option> [ ,... n]) ]               

Specifies the index will be rebuilt using the same columns, index type, uniqueness attribute, and sort order. This clause is equivalent to DBCC DBREINDEX. REBUILD enables a disabled index. Rebuilding a clustered index does not rebuild associated nonclustered indexes unless the keyword ALL is specified.

If index options are not specified, the existing index option values stored in sys.indexes are applied. For any index option whose value is not stored in sys.indexes, the default indicated in the argument definition of the option applies.

在sys.indexes 視圖中,共儲存5個index option,分別是 ignore_dup_key,fill_factor,is_padded,allow_row_locks,allow_page_locks,其他5個Index Option的Default value都是“否定的”,分別是

SORT_IN_TEMPDB :            Default OFFSTATISTICS_NORECOMPUTE :    Default OFFDROP_EXISTING:              Default OFFONLINE:                     Default OFFDATA_COMPRESSION :          Default NONEMAXDOP:                     0

詳細資料,查看CREATE INDEX (Transact-SQL)

If ALL is specified and the underlying table is a heap, the rebuild operation has no effect on the table. Any nonclustered indexes associated with the table are rebuilt.

The rebuild operation can be minimally logged if the database recovery model is set to either bulk-logged or simple.

REORGANIZE               

Specifies the index leaf level will be reorganized. ALTER INDEX REORGANIZE statement is always performed online. This means long-term blocking table locks are not held and queries or updates to the underlying table can continue during the ALTER INDEX REORGANIZE transaction. REORGANIZE cannot be specified for a disabled index or an index with ALLOW_PAGE_LOCKS set to OFF.

 

3, sys.indexes 儲存的Index option

select i.object_id,i.name as IndexName,i.index_id,i.type,i.type_desc,    i.data_space_id,i.is_disabled,    --Unique Property    i.is_unique,        --Constraint    i.is_primary_key,    i.is_unique_constraint,    --Filter Index    i.has_filter,    i.filter_definition,    --Index Options    i.ignore_dup_key,    i.fill_factor,    i.is_padded,    i.allow_row_locks,    i.allow_page_locksfrom sys.indexes i

 

4,Alter Index Remarks

ALTER INDEX cannot be used to repartition an index or move it to a different filegroup. This statement cannot be used to modify the index definition, such as adding or deleting columns or changing the column order. Use CREATE INDEX with the DROP_EXISTING clause to perform these operations.

When an option is not explicitly specified, the current setting is applied. For example, if a FILLFACTOR setting is not specified in the REBUILD clause, the fill factor value stored in the system catalog will be used during the rebuild process. To view the current index option settings, use sys.indexes.

                     Note                  

The values for ONLINE, MAXDOP, and SORT_IN_TEMPDB are not stored in the system catalog. Unless specified in the index statement, the default value for the option is used.

On multiprocessor computers, just like other queries do, ALTER INDEX REBUILD automatically uses more processors to perform the scan and sort operations that are associated with modifying the index. When you run ALTER INDEX REORGANIZE, with or without LOB_COMPACTION, the max degree of parallelism value is a single threaded operation. For more information, see Configure Parallel Index Operations.

An index cannot be reorganized or rebuilt if the filegroup in which it is located is offline or set to read-only. When the keyword ALL is specified and one or more indexes are in an offline or read-only filegroup, the statement fails.

Rebuilding Indexes

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. FOREIGN KEY constraints do not have to be dropped in advance. When indexes with 128 extents or more are rebuilt, the Database Engine defers the actual page deallocations, and their associated locks, until after the transaction commits.

Rebuilding or reorganizing small indexes often does not reduce fragmentation. The pages of small indexes are stored on mixed extents. Mixed extents are shared by up to eight objects, so the fragmentation in a small index might not be reduced after reorganizing or rebuilding it.

In SQL Server 2012, statistics are not created by scanning all the rows in the table when a partitioned index is created or rebuilt. Instead, the query optimizer uses the default sampling algorithm to generate statistics. To obtain statistics on partitioned indexes by scanning all the rows in the table, use CREATE STATISTICS or UPDATE STATISTICS with the FULLSCAN clause.

In earlier versions of SQL Server, you could sometimes rebuild a nonclustered index to correct inconsistencies caused by hardware failures. In SQL Server 2008 and later, you may still be able to repair such inconsistencies between the index and the clustered index by rebuilding a nonclustered index offline. However, you cannot repair nonclustered index inconsistencies by rebuilding the index online, because the online rebuild mechanism will use the existing nonclustered index as the basis for the rebuild and thus persist the inconsistency. Rebuilding the index offline, by contrast, will force a scan of the clustered index (or heap) and so remove the inconsistency. As with earlier versions, we recommend recovering from inconsistencies by restoring the affected data from a backup; however, you may be able to repair the index inconsistencies by rebuilding the nonclustered index offline. For more information, see DBCC CHECKDB (Transact-SQL).

Reorganizing Indexes

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. To view the fill factor setting, use sys.indexes.

When ALL is specified, relational indexes, both clustered and nonclustered, and XML indexes on the table are reorganized. Some restrictions apply when specifying ALL, see the definition for ALL in the Arguments section.

 

 

 

參考文檔:

https://msdn.microsoft.com/en-us/library/ms188783(v=sql.110).aspx

https://msdn.microsoft.com/en-us/library/ms189858(v=sql.110).aspx

https://msdn.microsoft.com/en-us/library/ms188388(v=sql.110).aspx

 

Index Reorganize 和 Rebuild 的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.