Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Chapter 23 Creating and Using Segments [Table of Contents] Chapter 25 Checking Database Consistency

System Administration Guide (Online Only)

[-] Chapter 24 Using the reorg Command

Chapter 24

Using the reorg Command

Update activity against a table can eventually lead to inefficient utilization of space and reduced performance. The reorg command reorganizes the use of table space and improves performance.

reorg subcommands

The reorg command provides four subcommands for carrying out different types and levels of reorganization:

The reclaim_space, forwarded_rows, and compact subcommands:

The following considerations apply to the rebuild subcommand:

The following restrictions hold:

When to run a reorg command

reorg is useful when:

Using the optdiag utility to assess the need for a reorg

To assess the need for running a reorg, you can use statistics from the systabstats table and the optdiag utility. systabstats contains statistics on the utilization of table space, while optdiag generates reports based on statistics in both systabstats and the sysstatistics table.

For information on the systabstats table, see the Performance and Tuning Guide. For information about optdiag, see Utility Programs for UNIX Platforms.

Space reclamation without the reorg command

Several types of activities reclaim or reorganize the use of space in a table on a page-by-page basis:

Each of these has limitations and may be insufficient for use on a large number of pages. For example, inserts may execute more slowly when they need to reclaim space, and may not affect many pages with space that can be reorganized. Space reclamation under the housekeeper task compacts unused space, but it runs only when no other tasks are requesting CPU time, so it may not reach every page that needs it.

Moving forwarded rows to home pages

If an update makes a row too long to fit on its current page, the row is forwarded to another page. A reference to the row is maintained on its original page, the row's home page, and all access to the forwarded row goes through this reference. Thus, it always takes two page accesses to get to a forwarded row. If a scan needs to read a large number of forwarded pages, the I/Os caused by extra page accesses slow performance.

reorg forwarded_rows undoes row forwarding by either moving a forwarded row back to its home page, if there is enough space, or by deleting the row and reinserting it in a new home page.

You can get statistics on the number of forwarded rows in a table by querying systabstats and using optdiag.

reorg forwarded_rows syntax

The syntax for reorg forwarded_rows is:

reorg forwarded_rows tablename 
     [with {resume, time = no_of_minutes}]

For information about the resume and time options, see "resume and time options for reorganizing large tables".

reorg forwarded_rows does not apply to indexes, because indexes do not have forwarded rows.

Using reorg compact to remove row forwarding

reorg forwarded_rows uses allocation page hints to find forwarded rows. Because it does not have to search an entire table, this command executes quickly, but it may miss some forwarded rows. After running reorg forwarded_rows, you can evaluate its effectiveness by using optdiag and checking "Forwarded row count." If "Forwarded row count" is high, you can then run reorg compact, which goes through a table page by page and undoes all row forwarding.

Reclaiming unused space from deletes and updates

When a task performs a delete operation or an update that shortens row length, the empty space is preserved in case the transaction is rolled back. If a table is subject to frequent deletes and row-shortening updates, unreclaimed space may accumulate to the point that it impairs performance.

reorg reclaim_space reclaims unused space left by deletes and updates. On each page that has space resulting from committed deletes or row-shortening updates, reorg reclaim_space rewrites the remaining rows contiguously, leaving all the unused space at the end of the page. If all rows have been deleted and there are no remaining rows, reorg reclaim_space deallocates the page.

You can get statistics on the number of unreclaimed row deletions in a table from the systabstats table and by using the optdiag utility. There is no direct measure of how much unused space there is as a result of row-shortening updates.

reorg reclaim_space syntax

The syntax for reorg reclaim_space is:

reorg reclaim_space tablename [indexname] 
     [with {resume, time = no_of_minutes}]

If you specify only a table name, only the table's data pages are reorganized to reclaim unused space; in other words, indexes are not affected. If you specify an index name, only the pages of the index are reorganized.

For information about the resume and time options, see "resume and time options for reorganizing large tables".

Reclaiming unused space and undoing row forwarding

reorg compact combines the functions of reorg reclaim_space and reorg forwarded_rows. Use reorg compact when:

reorg compact syntax

The syntax for reorg compact is:

reorg compact tablename 
     [with {resume, time = no_of_minutes}]

For information about the resume and time options, see "resume and time options for reorganizing large tables".

Rebuilding a table

Use reorg rebuild when:

If a table needs to be rebuilt because of a low cluster ratio, it may also need to have its space management settings changed (see "Changing space management settings before using reorg rebuild").

reorg rebuild uses a table's current space management settings to rewrite the rows in the table according to the table's clustered index, if it has one. All indexes on the table are dropped and re-created using the current space management values for reservepagegap and fillfactor. After a rebuild, a table has no forwarded rows and no unused space from deletions or updates.

reorg rebuild syntax

The syntax for reorg rebuild is:

reorg rebuild tablename [index_name]

Prerequisites for running reorg rebuild

Before you run reorg rebuild on a table:

To set select into/bulkcopy/pllsort to true and checkpoint the database, use the following isql commands:

1> use master
2> go
1> sp_dboption pubs2, 
    "select into/bulkcopy/pllsort", true
2> go
1> use pubs2
2> go
1> checkpoint
2> go

Following a rebuild on a table:

Changing space management settings before using reorg rebuild

When reorg rebuild rebuilds a table, it rewrites all table and index rows according to the table's current settings for reservepagegap, fillfactor, and exp_row_size. These properties all affect how quickly inserts cause a table to become fragmented, as measured by a low cluster ratio.

If it appears that a table quickly becomes fragmented and needs to be rebuilt too frequently, it may be a sign that you need to change the table's space management settings before you run reorg rebuild.

To change the space management settings, use sp_chgattribute (see the Reference Manual). For information on space management settings, see the Performance and Tuning Guide.

Using the reorg rebuild command on indexes

The reorg rebuild command allows you to rebuild individual indexes while the table itself is accessible for read and update activities.

Syntax

The syntax for rebuilding an index is:

reorg rebuild table_name index_name 

Comments

To use reorg rebuild, you must be the table owner or the Database Owner, or have System Administrator privileges.

If you omit the index name, the entire table is rebuilt.

If you specify an index, only that index is rebuilt.

Requirements for using reorg rebuild on an index are less stringent than for tables. The following rules apply:

Limitations

The reorg command applies only to tables using datarows or datapages locking. You cannot run reorg on a table that uses allpages locking.

You cannot run reorg on a text index, the name from sysindexes associated with a text chain.

You cannot run reorg within a transaction.

You can do a dump tran on a table after rebuilding its index. However, you cannot do a dump tran if the entire table has been rebuilt.

You can rebuild the index for systabstats, but you cannot run reorg rebuild on the table itself.

Although online index rebuilding is allowed on a placement index, it rebuilds only the index pages. The data pages remain untouched, which means datarows are neither sorted nor rewritten to fresh pages. You can rebuild data pages by dropping a placement index, and then re-creating it.

How indexes are rebuilt with reorg rebuild indexname

Rebuilding a single index rewrites all index rows to new pages. This improves performance by:

To reduce contention with users whose queries need to use the index, reorg rebuild locks a small number of pages at a time. Rebuilding an index is a series of independent transactions, with some independent, nested transactions. Approximately 32 pages are rebuilt in each nested transaction and approximately 256 pages are rebuilt in each outer transaction. Address locks are acquired on the pages being modified and are released at the end of the topaction. The pages deallocated in a transaction are not available for reuse until the next transaction begins.

If the reorg rebuild command stops running, the transactions that are already committed are not rolled back. Therefore, the part that has been reorganized is well clustered with desired space utilization, and the part that has not been reorganized is the same as it was before you ran the command. The index remains logically consistent.

Rebuilding the clustered index does not affect the data pages of the table. It only affects the leaf pages and higher index levels. Non-leaf pages above level 1 are not rebuilt.

Space requirements for rebuilding an index

If you do not specify fill_factor or reservepagegap, rebuilding an index requires additional space of approximately 256 pages or less in the data segment. The amount of log space required is larger than that required to drop the index and re-create it using create index, but it should be only a small fraction of the actual index size. The more additional free space is available, the better the index clustering will be.

reorg rebuild may not rebuild those parts of the index that are already well clustered and have the desired space utilization.

Performance characteristics

Index scans are faster after you run reorg.

Running reorg against a table can have a negative effect on performance of concurrent queries.

Status messages

Running reorg rebuild indexname on a large table may take a long time. Periodic status messages are printed to give the user an idea of how reorg has progressed. Starting and ending messages are written to the error log and to the client process executing reorg. In-progress messages go only to the client.

A status reporting interval is calculated as either 10 percent of the pages to be processed or 10,000 pages, whichever is larger. When this number of pages is processed, a status message is printed. Therefore, no more than 10 messages are printed, regardless of the size of the index. Status messages for existing reorg commands are printed more frequently.

resume and time options for reorganizing large tables

Use the resume and time options of the reorg command when reorganizing an entire table would take too long and interfere with other database activities. time allows you to run a reorg for a specified length of time. resume allows you to start a reorg at the point in a table where the previous reorg left off. In combination, the two options allow you to reorganize a large table by running a series of partial reorganizations (for example, during off-hours).

resume and time not available with reorg rebuild.

Syntax for using resume and time in reorg commands

The syntax for resume and time is:

reorg reclaim_space tablename [indexname] 
     [with {resume, time = no_of_minutes}]

reorg forwarded_rows tablename 
     [with {resume, time = no_of_minutes}]

reorg compact tablename 
     [with {resume, time = no_of_minutes}]

The following considerations apply:

Specifying no_of_minutes in the time option

The no_of_minutes argument in the time option refers to elapsed time, not CPU time. For example, to run reorg compact for 30 minutes, beginning where a previous reorg compact finished, enter:

reorg compact tablename with resume, time=30

If the reorg process goes to sleep during any part of the 30 minutes, it still counts as part of the elapsed time and does not add to the duration of the reorg.

When the amount of time specified has passed, reorg saves statistics about the portion of the table or index that was processed in the systabstats table. This information is used as the restart point for a reorg with the resume option. The restart points for each of the three subcommands that take resume and time options are maintained separately. You cannot, for example, start a reorg with reorg reclaim_space and then resume it with reorg compact.

If you specify no_of_minutes, and reorg arrives at the end of a table or an index before the time is up, it returns to the beginning of the object and continues until it reaches its time limit.

resume and time allow you to reorganize an entire table or index over multiple runs. However, if there are updates between reorg runs, some pages may be processed twice and some pages may not be processed at all.


A segment tutorial [Table of Contents] Chapter 25 Checking Database Consistency