|
For most indexing, the operators in the default operator class of a secondary access method provide adequate support. However, when you want to order the data in a different sequence than the default operator class provides, you can define a new operator class for the secondary access method.
The CREATE OPCLASS statement creates an operator class. It provides the following information about the operator class to the database server:
The database server stores this information in the sysopclasses system catalog table. You must have the Resource privilege for the database or be the DBA to create an operator class.
The database server provides the default operator class, btree_ops, for the generic B-tree access method. The following CREATE OPCLASS statement creates a new operator class for the generic B-tree access method:
For more information, see Generic B-Tree Index.
You might want to create a new operator class for:
To traverse the index structure, the generic B-tree index uses the sequence that the relational operators define. By default, a B-tree uses the lexicographical sequence of data because the default operator class, btree_ops, contains the relational-operator functions. (For more information on this sequence, see Changing the Sort Order.) For a generic B-tree to use a different sequence for its index values, you can create a new operator class for the btree secondary access method. You can then specify the new operator class when you define an index on that data type.
When you create a new operator class for the generic B-tree index, you provide an additional sequence for organizing data in a B-tree. When you create the B-tree index, you can specify the sequence that you want a column (or user-defined function) in the index to have.
These steps create the new operator class of the generic B-tree index. You can also extend the default operator class to provide support for new data types. For more information, see Extensions of the btree_ops Operator Class.
To use the new operator class, specify the name of the operator class after the column or function name in the CREATE INDEX statement.
As an example, suppose you want to define a new ordering for integers. The lexicographical sequence of the default B-tree operator class orders integers numerically: -4 < -3 < -2 < -1 < 0 < 1 < 2 < 3. Instead, you might want the numbers -4, 2, -1, -3 to appear in order of absolute value.
To obtain the absolute-value order, you must define external functions that treat negative integers as positive integers. The following steps create a new operator class called abs_btree_ops with strategy and support functions that provide the absolute-value order:
You can now create a B-tree index on an INTEGER column and associate the new operator class with this column.
The c_num1_ix index uses the new operator class, abs_btree_ops, for the cust_num column. An end user can now use the absolute value functions in SQL statements, as in the following example:
In addition, because the abs_lt() function is part of an operator class, the query optimizer can use the c_num1_ix index when it looks for all cust_tab rows with cust_num values between -7 and 7. A cust_num value of -8 does not satisfy this query.
The default operator class is still available for indexes. The following CREATE INDEX statement defines a second index on the cust_num column:
The c_num2_ix index uses the default operator class, btree_ops, for the cust_num column. The following query uses the operator function for the default less than (<) operator:
The query optimizer can use the c_num2_ix index when it looks for all cust_tab rows with cust_num values less than 7. A cust_num value of -8 does satisfy this query.
You can also define operator classes for user-defined secondary access methods. A user-defined secondary access method is one that a database developer has defined to implement a particular type of index. These access methods might have been defined in the database by a DataBlade module.
Tip: You can examine the sysams system catalog table to determine which secondary access methods your database defines. For information on the columns of the sysams system catalog table, see the "Informix Guide to SQL: Reference."
You perform the same steps to define an operator class on a user-defined secondary access method as you use to define an operator class on the generic B-tree index. (See Creating a New B-Tree Operator Class.) The only difference is that to create the index, you must specify the name of the user-defined secondary access method in the USING clause of the CREATE INDEX statement.