What is the difference between OFFLINE and ONLINE index rebuild in SQL Server?

前端 未结 3 765
遥遥无期
遥遥无期 2021-02-01 12:30

When rebuilding an index, there is an option for ONLINE=OFF and ONLINE=ON. I know that when ONLINE mode is on, it makes a copy of the index, switches new queries to utilizing it

相关标签:
3条回答
  • 2021-02-01 12:58

    Online index rebuilds are less intrusive when it comes to locking tables. Offline rebuilds cause heavy locking of tables which can cause significant blocking issues for things that are trying to access the database while the rebuild takes place.

    "Table locks are applied for the duration of the index operation [during an offline rebuild]. An offline index operation that creates, rebuilds, or drops a clustered, spatial, or XML index, or rebuilds or drops a nonclustered index, acquires a Schema modification (Sch-M) lock on the table. This prevents all user access to the underlying table for the duration of the operation. An offline index operation that creates a nonclustered index acquires a Shared (S) lock on the table. This prevents updates to the underlying table but allows read operations, such as SELECT statements."

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

    Additionally online index rebuilds are a enterprise (or developer) version only feature.

    0 讨论(0)
  • 2021-02-01 13:01

    In ONLINE mode the new index is built while the old index is accessible to reads and writes. any update on the old index will also get applied to the new index. An antimatter column is used to track possible conflicts between the updates and the rebuild (ie. delete of a row which was not yet copied). See Online Index Operations. When the process is completed the table is locked for a brief period and the new index replaces the old index. If the index contains LOB columns, ONLINE operations are not supported in SQL Server 2005/2008/R2.

    In OFFLINE mode the table is locked upfront for any read or write, and then the new index gets built from the old index, while holding a lock on the table. No read or write operation is permitted on the table while the index is being rebuilt. Only when the operation is done is the lock on the table released and reads and writes are allowed again.

    Note that in SQL Server 2012 the restriction on LOBs was lifted, see Online Index Operations for indexes containing LOB columns.

    0 讨论(0)
  • 2021-02-01 13:01

    The main differences are:

    1) OFFLINE index rebuild is faster than ONLINE rebuild.

    2) Extra disk space required during SQL Server online index rebuilds.

    3) SQL Server locks acquired with SQL Server online index rebuilds.

    • This schema modification lock blocks all other concurrent access to the table, but it is only held for a very short period of time while the old index is dropped and the statistics updated.
    0 讨论(0)
提交回复
热议问题