For workloads involving many ad-hoc queries, it is often difficult to optimize a database for high performance. Queries against tables with composite (multi-column) indexes present a particular challenge. Ideally, a query's predicates are consistent with a table's composite index. This would mean that each predicate could be used as a start-stop key, which would, in turn, reduce the scope of the index needing to be searched. When a query contains predicates that are inconsistent with a composite index, this is known as an index gap. As such, index gaps are a characteristic of a query, not of a table's indexes.
SELECT * FROM t WHERE a=5 AND c=10
This
query contains an index gap on column B in the composite index (this
is assuming that the access plan contains an index scan on the composite
index).In the case of an index gap, the index scan will likely have to process many unnecessary keys. The predicates on the non-leading columns of the index would likely need to be applied individually against each key in the index that satisfies the start-stop keys. This slows down the index scan as more rows need to be processed, and additional predicates need to be evaluated for each key. Also, DB2 must sequentially examine all keys in what could be a large range.
To avoid index gaps you can define additional indexes to cover the permutations of query predicates likely to appear in your workloads. This is not an ideal solution, because defining additional indexes requires additional database administration and consumes storage capacity. Also, for workloads with many ad-hoc queries, it can be difficult to anticipate which indexes would be needed.
In DB2 V10.1, the query optimizer can build an access plan that uses the jump scan operation when queries contain index gaps. In a jump scan operation, the index manager identifies qualifying keys for small sections of a composite index where there are gaps, and fills these gaps with these qualifying keys. The result is that the index manager skips over parts of the index that will not yield any results.