索引 Reorganize 和 Rebuild 的区别

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

BTree结构决定 Index 的叶子节点,从左到右使依次增大,如图是Index的叶子节点,左边的Index Key最小,右边的Index Key最大:

如果更新数据导致index key变化,例如,将index key 由4变更为9,那么必须将9放置在8之后,10之前,如果8所在的Page有空间容纳9,那么SQL Server只需要将9移动到8之后,原来的4被删除,这会降低原page中数据存储的密度,造成一个碎片(fragment),即:3和5之间存在空闲空间,但是物理顺序和逻辑顺序还是一致的。

如果8和10所在的page不能容纳9,那么 SQL Server 选择最节省,最有效的方式:拆分Page。试想,如果不拆分page,那么,5,6,7,8 这几个数据行都要向前移动,为9腾挪空间。在SQL Server中,数据移动是十分浪费IO,内存和CPU资源的,IO必须在CPU的调控下进行。

拆分Page是指分配一个新的Page,将8所在的Page上的数据的一半(后一半,或前一半)移动在新的Page上,如图,将page中的后一半移动在新的page上,通过指针连接在一起,保持逻辑顺序的一致性,但是物理顺序已经不连续了。

对于Index Key移动之后,其物理顺序和逻辑顺序仍然保持一致,这会导致索引出现碎片,数据存储的密度降低;而拆分page,不仅将page存储数据的密度降低一半,而且数据的物理顺序和逻辑顺序,导致SQL Server的预读取操作效果下降。针对Index的这两种情况,根据Index的碎片率,对Index 进行重组(Reorganize)或重建(Rebuild)。

1,Reorganize 和 Rebuild 的区别

Rebuild 是重新创建,将Index之前占用的空间释放,重新申请空间来创建index。Reorganize 是重新组织,将index的叶子节点进行重新组织。

Rebuilding an index means that a whole new set of pages is allocated for it。

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.

ALTER INDEX { index_name | ALL }
ON <object>
REBUILD | REORGANIZE 

2, 重建索引

在重建index时,将使用Index的定义(index key,index type,唯一属性和排序方向),重建索引是能够将一个被disabled的索引启用,重建一个聚集索引时,不会将与之关联的nonclustered index一起重建,除非指定all关键字,all关键字是指将object上的所有index重建。

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 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.

使用Rebuild 重新创建index时,如果没有指定Index Option,Rebuild使用默认的索引选项来重建index。

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

查看 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

3,重组索引

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.

Appendix

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.

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.

参考文档:

CREATE INDEX (Transact-SQL)

Reorganize and Rebuild Indexes

ALTER INDEX (Transact-SQL)

时间: 2024-10-25 01:54:30

索引 Reorganize 和 Rebuild 的区别的相关文章

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

蛋疼的郁闷——聚集索引扫描、非聚集索引扫描、表扫描区别

聚集索引扫描,首先我们知道数据它是以索引键为叶节点排列起来的树形数据结构,表中每行的数据都附属在索引键中,对这样的表进行数据查找时,最快的方式当然是“聚集索引查找”.什么情况下才是“聚集索引扫描”呢?是当你要查找的数据的条件字段上没有索引时,此时查询执行器将对整个表中的数据挨个的进行读取确认符合查询条件的数据,但当该表上有字段设有聚集索引时,该扫描过程称之为“聚集索引扫描",相反的情况是当该表上没有一个字段设有”聚集索引“时,该扫描过程称之为”表扫描“.其实他们本质上的过程都是一样的,就是挨个的

蛋疼的郁闷-聚集索引扫描、非聚集索引扫描、表扫描区别

本文适用于对数据库索引有一定深入的攻城师阅读参考. 我们对于聚集索引扫描和表扫描比较容易理解的,但是对于非聚集索引扫描不太容易理解,这一点也往往容易使初学者感到很是困惑,原因是总认为没必要存在非聚集索引扫描,因为如果查询结果不具有高选择性的话,在聚集索引表中可以使用聚集索引扫描,在对表中会使用表扫描的,那么为什么要会存在非聚集索引扫描呢? 之所以有这样的问题,是因为我们没有考虑到一种情况,那就是查询结果如果被建有非聚集索引的字段覆盖或包含了,而此时where条件字段上的非聚集索引对于本次查询结果

[转载]Elasticsearch索引重建(Rebuild)

From:http://blog.csdn.net/changong28/article/details/38491185 索引重建(Rebuild) 索引创建后,你可以在索引当中添加新的类型,在类型中添加新的字段.但是如果想修改已存在字段的属性(修改分词器.类型等),目前ES是做不到的.如果确实存在类似这样的需求,只能通过重建索引的方式来实现.但想要重建索引,请保证索引_source属性值为true,即存储原始数据.索引重建的过程就是将原来索引数据查询回来入到新建的索引当中去,为了重建过程不影

Sql Server之旅——第八站 复合索引和include索引到底有多大区别?

周末终于搬进出租房了,装了宽带....才发现没网的日子...那是一个怎样的与世隔绝呀...再也受不了那样的日子了....好了,既然网 安上去了,还得继续我的这个系列. 索引和锁,这两个主题对我们开发工程师来说,非常的重要...只有理解了这两个主题,我们才能写出高质量的sql语句,在之前的博客中,我所说的 索引都是单列索引...当然数据库不可能只认单列索引,还有我这篇的复合索引,说到复合索引,可能熟悉的人又会说到include索引,那这两个索引到底 有什么区别呢,当然我也是菜鸟一枚...所以下面的

MYSQL中的普通索引,主健,唯一,全文索引区别

MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记录数量越多,这个操作的代价就越高.如果作为搜索条件的列上已经创建了索引,MySQL无需扫描任何记录即可迅速得到目标记录所在的位置.如果表有1000个记录,通过索引查找记录至少要比顺序扫描记录快100倍. 总体分析 PRIMARY, INDEX, UNIQUE 这3种是一类 PRIMARY 主键. 就

mysql索引优化 mysiam和innodb区别?

Mysql中有哪几种锁? 1.表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低. 2.行级锁:开销大,加锁慢:会出现死锁:锁定粒度最小,发生锁冲突的概率最低,并发度也最高. 3.页面锁:开销和加锁时尚界于表锁和行锁之间,并发度一般. mysql中有哪些不同的表格? 5种 myisam.Heap.merge.innodb.isam MYISAM和Innodb区别 myisam表引擎: 1.  5.1版本前,myisam是默认的存储引擎. 2. 支持全文索引,压缩空

SQL server 聚集索引与主键的区别

主键是一个约束(constraint),他依附在一个索引上,这个索引可以是聚集索引,也可以是非聚集索引. 所以在一个(或一组)字段上有主键,只能说明他上面有个索引,但不一定就是聚集索引. 例如下面: 1 USE [pratice] 2 GO 3 CREATE TABLE #tempPKCL 4 ( 5 ID INT PRIMARY KEY CLUSTERED --聚集索引 6 ) 7 8 9 --------------------------------- 10 USE [pratice] 1

索引优化之:创建、填充和查找

在做性能优化时,经常需要创建索引,维护索引,或重建,或重组:在创建索引时,索引的数据页有时需要填充满,有时需要预留一定比例的空闲空间:在分析查询的执行计划之后,推荐创建覆盖索引(covering index),优化查询语句,使用执行计划通过Index Seek来获取少量数据等,这些都是索引优化不得不知的要点. 一,索引的重组(Reorganize)和重建(Rebuild) 在SQL Server中,索引(Index)是B-Tree(balance tree)结构,每个Page之间都有双向指针链接