You are on page 1of 2

Advantages and disadvantages of indexes Although the optimizer decides whether to use an index to access table data, except

in the following case, you must decide which indexes might improve performance and create these indexes. Exceptions are the dimension block indexes and the composite block index that are created automatically for each dimension that you specify when you create a multi-dimensional clustering (MDC) table. You must also execute the RUNSTATS utility to collect new statistics about the indexes in the following circumstances:

After you create an index After you change the prefetch size

You should also execute the RUNSTATS utility at regular intervals to keep the statistics current. Without up-to-date statistics about indexes, the optimizer cannot determine the best data-access plan for queries. Note: To determine whether an index is used in a specific package, use the SQL Explain facility. To plan indexes, use the Design Advisor from the Control Center or the db2advis tool to get advice about indexes that might be used by one or more SQL statements. Advantages of an index over no index If no index exists on a table, a table scan must be performed for each table referenced in a database query. The larger the table, the longer a table scan takes because a table scan requires each table row to be accessed sequentially. Although a table scan might be more efficient for a complex query that requires most of the rows in a table, for a query that returns only some table rows an index scan can access table rows more efficiently. The optimizer chooses an index scan if the index columns are referenced in the SELECT statement and if the optimizer estimates that an index scan will be faster than a table scan. Index files generally are smaller and require less time to read than an entire table, particularly as tables grow larger. In addition, the entire index may not need to be scanned. The predicates that are applied to the index reduce the number of rows to be read from the data pages. 7If an ordering requirement on the output can be 7matched with an index column, then scanning the index in column order will 7allow the rows to be retrieved in the correct order without a sort. Each index entry contains a search-key value and a pointer to the row containing that value. If you specify the ALLOW REVERSE SCANS parameter in the CREATE INDEX statement, the values can be searched in both ascending and descending order. It is therefore possible to bracket the search, given the right predicate. An index can also be used to obtain rows in an ordered sequence, eliminating the need for the database manager to sort the rows after they are read from the table.

In addition to the search-key value and row pointer, an index can contain include columns, which are non-indexed columns in the indexed row. Such columns might make it possible for the optimizer to get required information only from the index, without accessing the table itself. Note: The existence of an index on the table being queried does not guarantee an ordered result set. Only an ORDER BY clause ensures the order of a result set. Although indexes can reduce access time significantly, they can also have adverse effects on performance. Before you create indexes, consider the effects of multiple indexes on disk space and processing time:

Each index requires storage or disk space. The exact amount depends on the size of the table and the size and number of columns in the index. Each INSERT or DELETE operation performed on a table requires additional updating of each index on that table. This is also true for each UPDATE operation that changes the value of an index key. The LOAD utility rebuilds or appends to any existing indexes. The indexfreespace MODIFIED BY parameter can be specified on the LOAD command to override the index PCTFREE used when the index was created.

Each index potentially adds an alternative access path for a query for the optimizer to consider, which increases the compilation time.

Choose indexes carefully to address the needs of the application program. Related concepts Space requirements for indexes Index planning tips Index performance tips The Design Advisor Table reorganization Index reorganization Table and index management for standard tables Table and index management for MDC tables Index cleanup and maintenance Related tasks Creating an index Collecting catalog statistics Collecting index statistics This topic can be found in: Administration Guide: Performance.