SQL Server: What is the difference between Index Rebuilding and Index Reorganizing?

前端 未结 7 1254
暗喜
暗喜 2021-02-05 05:37

What is the difference between Index Rebuilding and Index Reorganizing?

相关标签:
7条回答
  • 2021-02-05 05:47

    Rebuild index - rebuilds one or more indexes for a table in the specified database.

    Reorganise index - Defragments clustered and secondary indexes of the specified table

    0 讨论(0)
  • 2021-02-05 05:48

    REBUILD locks the table for the whole operation period (which may be hours and days if the table is large).

    REORGANIZE doesn't lock the table.

    Well. actually, it places some temporary locks on the pages it works with right now, but they are removed as soon as the operation is complete (which is fractions of second for any given lock).

    As @Andomar noted, there is an option to REBUILD an index online, which creates the new index, and when the operation is complete, just replaces the old index with the new one.

    This of course means you should have enough space to keep both the old and the new copy of the index.

    REBUILD is also a DML operation which changes the system tables, affects statistics, enables disabled indexes etc.

    REORGANIZE is a pure cleanup operation which leaves all system state as is.

    0 讨论(0)
  • 2021-02-05 05:48

    Rebuild it droping the current indexes and recreating new ones.

    Reorganizing is like putting the house in order with what u already have.

    it is a good practice to use 30% fragmentation to determine rebuild vs reorganize.

    <30% reorganize vs. >30% rebuild

    0 讨论(0)
  • 2021-02-05 05:48

    In addition to the differences above (basically rebuild will create the index anew, and then "swap it in" for the existing one, rather than trying to fix the existing one), an important consideration is that a rebuild - even an Enterprise ONLINE rebuild - will interfere with snapshot isolation transactions.

    TX1 starts snapshot transaction
    TX1 reads from T

    TX2 rebuilds index on T
    TX2 rebuild complete

    TX1 read from T again:

    Error 3961, Snapshot isolation transaction failed in database because the object accessed by the statement has been modified by a DDL statement in another concurrent transaction since the start of this transaction. It is disallowed because the metadata is not versioned. A concurrent update to metadata can lead to inconsistency if mixed with snapshot isolation.

    0 讨论(0)
  • 2021-02-05 06:01

    There are a number of differences. Basically, rebuilding is a total rebuild of an index - it will build a new index, then drop the existing one, whereas reorganising it will simply, well... it will reorganise it.

    This blog entry I came across a while back will explain it much better than I can. :)

    0 讨论(0)
  • 2021-02-05 06:05

    Think about how the index is implemented. It's generally some kind of tree, like a B+ Tree or B- Tree. The index itself is created by looking at the keys in the data, and building the tree so the table can be searched efficiently.

    When you reorganize the index, you go through the existing index, cleaning up blocks for deleted records etc. This could be done (and is in some databases) when you make a deletion, but that imposes some performance penalty. instead, you do it separately in order to do it more or less batch mode.

    When you rebuild the index, you delete the existing tree and read all the records, building a new tree directly from the data. That gives you a new, and hopefully optimized, tree that may be better than the results of reorganizing the table; it also lets you regenerate the tree if it somehow has been corrupted.

    0 讨论(0)
提交回复
热议问题