SQL Server 2008 - Worth the Wait
Online Indexing is a new feature available in SQL Server 2005. In SQL Server 2005, DBAs can create, rebuild, or drop indexes online. The index operations on the underlying table can be performed concurrently with update or query operations. This was not possible in previous versions of SQL Server. In the past, indexing operations (reorganizing or rebuilding) were usually performed as a part of other maintenance tasks running during off-peak hours in SQL Server 2000 or 7.0. During these offline operations, the indexing operations hold exclusive locks on the underlying table and associated indexes. During online index operations, SQL Server 2005 eliminates the need of exclusive locks (This is explained in the "How Online Index Operations work" section, found later in this article.)
The Online indexing feature is very helpful for environments that run 24 hours a day, seven days a week. The Online Indexing feature is available only in the Enterprise Edition of SQL Server 2005.
Now let's have a look at how indexing operations work in SQL Server 2005. During online index operations, several different structures are used. They are the source, preexisting indexes, target, and temporary mapping indexes.
The source refers to the underlying table or clustered index data. Preexisting indexes are nonclustered indexes associated with the table or the clustered index. Preexisting indexes are available for users for concurrent DML operations. The target structure is the new index that is created or rebuilt. The temporary mapping index comes into the picture during the creation of the clustered index. This nonclustered index is created in the same step as the new clustered index (or heap) and does not require a separate sort operation. Concurrent transactions also maintain the temporary mapping index in all INSERT, UPDATE, and DELETE operations.
The online index operation can be divided into three phases:
The Build phase is a longest phase of all. It is in this phase where the creation, dropping, or rebuilding of indexes take place. The duration of the Build phase depends on the size of the data and the speed of the hardware. Exclusive locks are not held in this phase, so concurrent DML operations can be performed during this phase. The Preparation and Final phases are for shorter durations. They are independent of the size factor of the data. During these two short phases, the table or the indexed data is not available for concurrent DML operations. Now let's discuss these three phases in detail.
During the preparation phase, the following activities take place:
During the build phase:
When a clustered index is created or dropped and non-clustered indexes exist for that table, the preparation and build phases are repeated twice; once for the new clustered index and again for the nonclustered indexes, which are all rebuilt in a single operation to incorporate the change in the definition of the base structure of the clustered index or heap.
The Final phase is used to inform the system that the index is ready. These are the following activities that take place during this phase: