Although indexes can dramatically improve the performance of statements that search for a specific row, or for a specific subset of the rows, two rows appearing sequentially in the index do not necessarily appear on the same table page in the database.
You can further improve an index's retrieval performance by declaring that the index is clustered. Using a clustered index increases the chance that two rows from adjacent index entries will appear on the same page in the database. This can lead to performance benefits by reducing the number of times a table page needs to be read into the buffer pool.
The existence of an index with a clustering property causes the database server to attempt to store table rows in approximately the same order as they appear in the clustered index. However, while the database server attempts to preserve the key order, clustering is approximate and total clustering cannot be guaranteed. Consequently, the database server cannot sequentially scan the table and retrieve all of the rows in clustered index key sequence. Ensuring that the rows of the table are returned in sorted order requires an access plan that either accesses the rows through the index, or performs a physical sort.
The optimizer exploits an index with a clustering property by modifying the expected cost of indexed retrieval to take into account the expected physical adjacency of table rows with matching or adjacent index key values.
The amount of clustering for a given table may degrade over time, as more and more rows are inserted or updated. The database server automatically keeps track of the amount of clustering for each clustered index in the ISYSPHYSIDEX system table. If the database server detects that the rows in a table have become significantly unclustered, the optimizer will adjust its expected index retrieval costs accordingly.
The clustering property of an index can be added or removed at any time. To reorder the rows in a table to match a clustered index, see REORGANIZE TABLE statement.
Any primary key index, foreign key index, UNIQUE constraint index, or secondary index can be declared with the CLUSTERED property. However, you may declare at most one clustered index per table. You can do this using any of the following statements:
Several statements work in conjunction with each other to allow you to maintain and restore the clustering effect:
The UNLOAD TABLE statement allows you to unload a table in the order of the clustered index key. See UNLOAD statement.
The LOAD TABLE statement inserts rows into the table in the order of the clustered index key. See LOAD TABLE statement.
The INSERT statement attempts to put new rows on the same table page as the one containing adjacent rows, as per the clustered index key. See INSERT statement.
The REORGANIZE TABLE statement restores the clustering of a table by rearranging the rows according to the clustered index. If REORGANIZE TABLE is used with tables where clustering is not specified, the tables are reordered using the primary key. See REORGANIZE TABLE statement.