Index Reorganize 和 Rebuild 的区别

对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 OFF
STATISTICS_NORECOMPUTE :    Default OFF
DROP_EXISTING:              Default OFF
ONLINE:                     Default OFF
DATA_COMPRESSION :          Default NONE
MAXDOP:                     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_locks
from 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

时间: 2024-08-24 21:29:44

Index Reorganize 和 Rebuild 的区别的相关文章

索引 Reorganize 和 Rebuild 的区别

在SQL Server中,Index是BTree(balance tree)结构,每个Page之间都有双向指针链接在一起.Index是在table结构之外,独立存在的存储结构.Index能使查询性能带来飞跃的主要原因是:Index 结构更小,能够更快加载到内存:Index ey物理顺序和逻辑一致,数据的预读取能够提高数据的加载速度,SQL Server 每次读取操作都会将物理物理相邻的多个Page一起加载到内存. BTree结构决定 Index 的叶子节点,从左到右使依次增大,如图是Index的

Oracle index unusable和invisible的区别

摘录自11g的官方文档:      UNUSABLE Specify UNUSABLE to mark the index or index partition(s) or index subpartition(s) UNUSABLE. The space allocated for an index or index partition or subpartition is freed immediately when the object is marked UNUSABLE. An unu

sql server中index的REBUILD和REORGANIZE

参考文献: http://technet.microsoft.com/en-us/library/ms188388.aspx 使用alter index来rebuild和reorganize索引来清除碎片,rebuild能够完全清除碎片,但是reorganize却不能. --1.准备实验数据 select * into Employee from AdventureWorks2008R2.HumanResources.Employee; --2.查看使用空间:Employee 290 72 KB

SQL Server Rebuild Index

USE [msdb]GO /****** Object:  StoredProcedure [dbo].[IndexMaintain]    Script Date: 6/14/2013 1:46:17 PM ******/SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO CREATE  procedure  [dbo].[IndexMaintain]  as   SET NOCOUNT on  BEGIN TRY  declare @EXCEPTIO

ORACLE中index的rebuild(转)

Oracle里大量删除记录后,表和索引里占用的数据块空间并没有释放. table move可以释放已删除记录表占用的数据块空间,整理碎片.如果将表格用move方式整理碎片后,索引将失效,这时需要将索引重建. 重建索引可以释放已删除记录索引占用的数据块空间.重建索引不仅能增加索引表空间空闲空间大小,还能够提高查询性能. Sql代码   --table move alter table tbl move; --rebuild索引 alter index idx_tbl_col rebuild; al

Index的填充属性:FillFactor 和 PAD_INDEX

在Create Index时,必须慎重考虑属性FillFactor 和 PAD_INDEX的设置,这两个属性只在create index 或 rebuild index时起作用,表示Index page(Leaf-Level 或 intermediate-Level)的填充程度.如果设置FillFactor=90,那么SQL Server 在create index 或 rebuild index时,不是将Index Page的空间全部用完,而是只使用接近90%的Page空间,预留10%的空闲空

Maintenance Plan Usage3:Task Usage (Maintain Index)

Maintenance Plan用于维护Index Fragmentation 和 Statistics的Task 共有三个,后台是使用相应的TSQL来进行data维护. Reorganize Index Task Move index pages into a more efficient search order. This task uses the ALTER INDEX REORGANIZE statement with SQL Server 2012 databases. Rebui

Index Defragmentation

如果使用sys.dm_db_index_physical_stats 检查Index的 avg_fragmentation_in_percent 是一个非常大的数值,那么就要检查fragmentation是否影响到性能,如果导致性能下降,那么就需要 remove fragmentation. Fragmentation alone is not a sufficient reason to reorganize or rebuild an index. The main effect of fr

查找索引碎片Find Index Fragmentation Details – Slow Index Performance

SQL SERVER – 2005 – Find Index Fragmentation Details – Slow Index Performance Just a day ago, while using one index I was not able to get the desired performance from the table where it was applied. I just looked for its fragmentation and found it wa