Reorganize vs Rebuild Index

Source: Internet
Author: User

Indexes are special data structures in the database engine that are built for tables (and sometimes for views) to help you find and organize your data. The importance body of an index now enables the database engine to quickly return query results. When modifications are made to the underlying data table where the index resides (including insertions, deletions, and updates), index fragmentation can occur. Index fragmentation occurs when the logical ordering of an index does not match the physical ordering of the underlying table or view. As the number of index fragments increases, query response time slows down and query performance degrades. In SQL Server 2005, to resolve this issue, either reorganize the index or rebuild the index.

  Reorganize vs Regenerate

There are two ways to fix index fragmentation: Reorganize the index or rebuild the index. Reorganize the index to reorder the data in the outermost data page and compress the index pages. No additional data is added during the re-organization process, so the index may still have some degree of fragmentation. The reorganize index operation does not consume a lot of system resources, and the external process is able to query the data table in which the index resides, so it can be said to be on-line (online) execution.

The Rebuild index operation basically removes the target index and creates a new one. Any fragments in the old index are deleted, and the logical ordering of the new index matches the physical ordering of the object. Because the entire process needs to drop the index and recreate it, the external process cannot access the data table and the access performance is greatly affected. In fact, in the process of rebuilding an index, other processes do not completely lock the data table. This is a major barrier to rebuilding indexes.

  Rebuilding indexes Online

SQL Server 2005 introduces the ability to rebuild indexes online so that other processes can access the data tables properly during this process, and there is no need to limit index rebuilds during off-peak hours.

For online operations, the database engine takes several special configurations to allow access to the index while rebuilding the index. First, the original index is preserved for the user to read the data and modify the data. Second, by row versioning (row versioning, the process of storing older versions of rows in the tempdb database) makes transactional reads consistent. During the rebuild process, a new index is created that mimics the old index. Any data modification operation that alters the original index is referenced by SQL Server to the new index during the rebuild of the index. The new index cannot be read and can only be written to. The key point is that there must be enough disk space for two concurrent indexes to accommodate the data during an online rebuild of the index. In the process of rebuilding, if the original index changes, SQL Server uses a mapping index to determine which records in the new index need to be modified accordingly. Once the rebuild process completes successfully, any query or data modification operation will point to the new index and delete the original index.

  Example

The process of rebuilding an index online is not much different from the normal process of rebuilding the index. However, there are several ways to complete the rebirth process. The first method is simply to delete the index, and the CREATE index statement is followed by a drop index. Rebuilding the index in this way keeps the data table in a state that is not indexed until the new index is completely created. Therefore, it is not recommended to use this method to delete an index.

If you use the DROP_EXISTING statement, you can still rebuild the index with CREATE index. This feature allows us to change the definition of the specified index and enable the database administrator to change the storage location of the index to another document group or partition.

The ALTER index statement enables us to regenerate the clustered index and any nonclustered indexes on the data table. The disadvantage of this statement is that you cannot change the definition of the index. These statements contain the option to generate an index online.

The following statement is used to regenerate the clustered index on the flighthistory table (located in the Flightid column). In this process, the existing index is deleted, but because the online option is specified, the index can also be accessed during the operation.

The following is a reference fragment:
CREATE CLUSTERED INDEX Cl_flighthistory_flightid on Flighthistory (Flightid)
With (drop_existing = on, ONLINE = ON)

The following statement is similar to the above, but changes the actual index definition to include an additional column. The index is regenerated in the same way.

The following is a reference fragment:
CREATE CLUSTERED INDEX cl_flighthistory_flightid on Flighthistory (flightid ASC, flightdate ASC)
with (drop_existing = on, ONLINE = ON)

Reorganize vs Rebuild Index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.