Home | Previous Page | Next Page   Purpose-Function Reference > Purpose-Function Syntax >

am_scancost

The query optimizer calls am_scancost during a SELECT statement, before it calls am_open.

Syntax

mi_real * am_scancost(MI_AM_TABLE_DESC *tableDesc,
                 MI_AM_QUAL_DESC *qualDesc)
tableDesc
points to the index descriptor.
qualDesc
points to the qualification descriptor, which specifies the criteria that a table rowindex key must satisfy to qualify for retrieval.

Usage

The am_scancost purpose function estimates the cost to fetch and qualify data for the current query. The optimizer relies on the am_scancost return value to evaluate a query path for a scan that involves the access method.

Warning:
If the access method does not have an am_scancost purpose function, the database server estimates the cost of a scan or bypasses the virtual index, which can diminish the optimal nature of the query plan.

Calculating Cost

The following types of information influence cost:

To calculate a cost, am_scancost considers the following factors:

Compute the cost of retrieving only those index entries that qualify. If retrieving an index entry does not supply the columns that the SELECT statement projects, the scan cost includes both of the following:

Important:
Because a function cannot return an mi_real data type by value, you must allocate memory to store the scan cost value and return a pointer to that memory from the am_scancost purpose function.

Factoring Cost

To adjust the result of am_scancost, set the am_costfactor purpose value. The database server multiplies the cost that am_scancost returns by the value of am_costfactor, which defaults to 1 if you do not set it. To find out how to set purpose values, refer to SQL Statements for Access Methods.

Forcing Reoptimization

The optimizer might need a new scan cost for subsequent scans of the same index, for example because of a join. To execute am_scancost before each rescan, call the mi_qual_setreopt() function.

Returning a Negative Cost

If the query specifies a feature that the access method does not support, return a value from am_scancost that forces the optimizer to pursue another path. In Figure 31, an access method that does not process Boolean operators checks the qualification descriptor for Boolean operators and returns a negative value if it finds one.

Figure 31. Forcing a Table Scan
mi_real * my_scan_cost(td, qd)
   MI_AM_QUAL_DESC *qd;
   MI_AM_TABLE_DESC *td;
{......
   for (i = 0; i < mi_qual_nquals(qd); i++)
      if (mi_qual_issimple(qd, i) == MI_FALSE) /* Boolean Operator found. */
         return -1;
   } 

The database server might respond to a negative scan-cost value in one of the following ways:

Warning:
The database server has no means to detect if a secondary access method does not set values for complex expressions. If an access method has no code to evaluate AND or OR, call accessor function mi_qual_boolop() or mi_qual_issimple() to determine if the qualification descriptor contains a Boolean operator.

Return Values

This function returns a pointer to an mi_real data type that contains the cost value.

Related Topics

See the descriptions of:

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]