Administration Guide


Creating a User-Defined Extended Index Type

To support user-defined index types, DB2 Universal Database allows you to create and apply your own logic for the primary components that make up how an index works. Those components that can be substituted are:

Note:The user-defined function definition must be deterministic and must not allow external actions in order to be exploitable by the optimizer.

An optional data filter function can also be specified. The optimizer uses the filter against the fetched tuple before the user-defined function is evaluated.

Only a structured type or distinct type column can use the index extension to create a user-defined extended index type on these objects. The user-defined extended index type must not:

Details on Index Maintenance

You define two of the components that make up the operations of an index through the CREATE INDEX EXTENSION statement.

Index maintenance is the process of transforming the index column content (or source key) to a target index key. The transformation process is defined using a table function that has previously been defined in the database.

The FROM SOURCE KEY clause specifies a structured data type or distinct type for the source key column supported by this index extension. A single parameter name and data type are given and associated with the source key column.

The GENERATE KEY USING clause specifies the user-defined table function used to generate the index key. The output from this function must be specified in the TARGET KEY clause specification (which is presented below). The output from this function can also be used as input for the index filtering function specified on the FILTER USING clause.

Details on Index Searching

Index searching maps search arguments to search ranges.

The WITH TARGET KEY clause specifies the target key parameters that are the output of the key generation function specified on the GENERATE KEY USING clause. A single parameter name and data type are given and associated with the target key column. This parameter corresponds to the columns of the RETURNS table of the user-defined table function of the GENERATE KEY USING clause.

The SEARCH METHODS clause introduces one or more search methods defined for the index. Each search method consists of a method name, search arguments, a range producing function, and an optional index filter function. Each search method defines how index search ranges for the underlying user-defined index are produced by a user-defined table function. Further, each search method defines how the index entries in a particular search range can be further qualified by a user-defined scalar function.

Details on Index Exploitation

Index exploitation occurs in the evaluation of the search method.

The CREATE FUNCTION (External Scalar) statement creates a user-defined predicate used with the search methods defined for the index extension.

The PREDICATES clause identifies those predicates using this function that can possibly exploit the index extensions (and that can possibly use the optional SELECTIVITY clause for the predicate's search condition). If the PREDICATES clause is specified, the function must be defined as DETERMINISTIC with NO EXTERNAL ACTION.

A Scenario for Defining an Index Extension

A scenario for defining an index extension follows:

  1. Define the structured types (for shapes). Use the CREATE TYPE statement to define a type hierarchy where shape is a supertype and nullshape, point, line, and polygon are subtypes. These structured types model spatial entities. For example, the location of a store is a point; the path of a river is a line; and, the boundary of a business zone is a polygon. A minimum bounded rectangle (mbr) is an attribute. The gtype attribute identifies whether the associated entity is a point, a line, or a polygon. Geographical boundaries are modeled by numpart, numpoint, and geometry attributes. All other attributes are ignored because they are of no interest to this scenario.
  2. Create the index extension.
  3. Create the key transformation which corresponds to the index maintenance component of an index.
       CREATE INDEX EXTENSION iename (parm_name datatype, ...)
          FROM SOURCE KEY (parm_name datatype)
          GENERATE KEY USING table_function_invocation
          ...
    

    The FROM SOURCE KEY clause identifies the parameter and data type of the key transformation. The GENERATE KEY USING clause identifies the function used to map the source key with the value generated from the function.

  4. Define the range-producing and index-filter functions which correspond to the index search component of an index.
       CREATE INDEX EXTENSION iename (parm_name datatype, ...)
          ...
          WITH TARGET KEY
             WHEN method_name (parm_name datatype, ...)
             RANGE THROUGH range_producing_function_invocation
             FILTER USING index_filtering_function_invocation
    

    The WITH TARGET KEY clause identifies the search method definition. The WHEN clause identifies the method name. The RANGE THROUGH clause identifies the function used to limit the scope of the index to be used. The FILTER USING clause identifies the function used to eliminate unnecessary items from the resulting index values.
    Note:The FILTER USING clause could identify a case expression instead of an index filtering function.

  5. Define the predicates to exploit the index extension.
       CREATE FUNCTION within (x shape, y shape)
          RETURNS INTEGER
          ...
          PREDICATES
             WHEN = 1
                FILTER USING mbrWithin (x..mbr..xmin, ...)
                SEARCH BY INDEX EXTENSION grid_extension
                WHEN KEY (parm_name) USE method_name(parm_name)
    

    The PREDICATES clause introduces one or more predicates that are started with each WHEN clause. The WHEN clause begins the specification for the predicate with a comparison operator followed by either a constant or an EXPRESSION AS clause. The FILTER USING clause identifies a filter function that can be used to perform additional filtering of the result table. This is a cheaper version of the defined function (used in the predicate) that reduces the number of rows on which the user-defined predicate must be executed to determine the rows that qualify. The SEARCH BY INDEX EXTENSION clause specifies where the index exploitation takes place. Index exploitation defines the set of rules using the search method of an index extension that can be used to exploit the index. The WHEN KEY clause specifies the exploitation rule. The exploitation rule describes the search targets and search arguments as well as how they can be used to perform the index search through a search method.

  6. Define a filter function.
       CREATE FUNCTION mbrWithin (...)
    
    The function defined here is created for use in the predicate of the index extension.

In order for the query optimizer to successfully exploit indexes created to improve query performance, a SELECTIVITY option is available on function invocation. In cases where you have some idea of the percentage of rows that the predicate may return, you can use the SELECTIVITY option on function invocation to help the DB2 optimizer choose a more efficient access path.

In the following example, the within user-defined function computes the center and radius (based on the first and second parameters, respectively), and builds a statement string with an appropriate selectivity:

   SELECT * FROM customer 
      WHERE within(loc, circle(100, 100, 10) = 1 SELECTIVITY .05

In this example, the indicated predicate (SELECTIVITY .05) filters out 95 percent of the rows in the customer table.


[ Top of Page | Previous Page | Next Page ]