informix
Informix Guide to SQL: Syntax
Segments

Optimizer Directives

The Optimizer Directives segment specifies keywords that you can use to partially or fully specify the query plan of the optimizer. Use this segment whenever you see a reference to Optimizer Directives in a syntax diagram.

Syntax

Usage

Use one or more optimizer directives to partially or fully specify the query plan of the optimizer.

When you use an optimizer directive, the scope of the optimizer directive is for the current query only.

By default, optimizer directives are enabled. To obtain information about how specified directives are processed, view the output of the SET EXPLAIN statement. To disable optimizer directives, you must set either the IFX_DIRECTIVES environment variable to 0 or OFF or the DIRECTIVES parameter in the onconfig file to 0.

Optimizer Directives as Comments

An optimizer directive or a string of optimizer directives immediately follows the DELETE, SELECT, or UPDATE keyword in the form of a comment.

After the comment symbol, the first character in a directive is always a plus (+) sign. No space is allowed between the comment symbol and the plus sign.

You can use any of the following comment styles:

For more information on SQL comment symbols, see How to Enter SQL Comments.

If you use multiple directives in one query, you must separate them. You can separate directives with a space, a comma, or any character that you choose. However, Informix recommends that you separate directives with a comma.

Syntax errors that appear in an optimizer directive do not cause a working query to break. The output of the SET EXPLAIN statement contains information related to such errors.

Restrictions on Optimizer Directives

In general, you can specify optimizer directives for any query block in a DELETE, SELECT, or UPDATE statement. However, you cannot use optimizer directives when your statement includes one of the following items:

Using the Join-Order Directive

Use the ORDERED join-order directive to force the optimizer to join tables in the order in which they appear in the FROM clause.

Element Purpose Restrictions Syntax
comments Any text that explains the purpose of the directive or other significant information Text must appear inside the comment symbols. Character string

For example, the following query forces the database server to join the dept and job tables, and then join the result with the emp table.

Because no predicates occur between the dept table and the job table, this query forces a Cartesian product.

Using the Ordered Directive with Views

When your query involves a view, the placement of the ORDERED join-order directive determines whether you are specifying a partial- or total-join order.

For examples that use the ORDERED join-order directive with views, refer to your Performance Guide.

Access-Method Directives

Use the access-method directive to specify the manner in which the optimizer should search the tables.

.

Element Purpose Restrictions Syntax
alias Temporary alternative name assigned to the table or view in the FROM clause When an alias is declared in the FROM clause, the alias also must be used in the optimizer directive. Identifier, p. 4-205
comments Any text that explains the purpose of the directive or other significant information Text must appear outside the parenthesis, but inside the comment symbols. Character string
index Name of the index for which you want to specify a query plan directive The index must be defined on the specified table. With the AVOID _INDEX directive, at least one index must be specified. Database Object Name, p. 4-50
synonym Name of the synonym for which you want to specify a query plan directive The synonym and the table to which the synonym points must exist. Database Object Name, p. 4-50
table Name of the table for which you want to specify a query plan directive The table must exist. Database Object Name, p. 4-50

You can separate the elements that appear within the parentheses with either one or more spaces or by commas.

The following table lists the purpose of each of the access-method directives and how it affects the query plan of the optimizer.

Keywords Purpose Optimizer Action
AVOID_FULL Do not perform a full-table scan on the listed table. The optimizer considers the various indexes it can scan. If no index exists, the optimizer performs a full table scan.
AVOID_INDEX Do not use any of the indexes listed. The optimizer considers the remaining indexes and a full table scan. If all indexes for a particular table are specified, the optimizer uses a full table scan to access the table.
FULL Perform a full-table scan. Even if an index exists on a column, the optimizer uses a full table scan to access the table.
INDEX Use the index specified to access the table. If more than one index is specified, the optimizer chooses the index that yields the least cost. If no indexes are specified, then all the available indexes are considered.

Prohibiting a Full Scan of a Table

Both the AVOID_FULL and INDEX keywords specify that the optimizer should avoid a full scan of a table. However, Informix recommends that you use the AVOID_FULL keyword to specify the intent to avoid a full scan on the table. In addition to specifying that the optimizer not use a full-table scan, the negative directive allows the optimizer to use indexes that are created after the access-method directive is specified.

Using Multiple Access-Method Directives on the Same Table

In general, you can specify only one access-method directive per table. However, you can specify both AVOID_FULL and AVOID_INDEX for the same table. When you specify both of these access-method directives, the optimizer avoids performing a full scan of the table and it avoids using the specified index or indexes.

This combination of negative directives allows the optimizer to use indexes that are created after the access-method directives are specified.

Examples that Uses an Access-Method Directive

Suppose that you have a table named emp, that contains the following indexes: loc_no, dept_no, and job_no. When you perform a SELECT that uses the table in the FROM clause you might direct the optimizer to access the table in one of the following ways:

Example Using a Positive Directive

In this example the access-method directive forces the optimizer to scan the index on the dept_no column.

Example Using Negative Directives

This example includes multiple access-method directives. These access-method directives also force the optimizer to scan the index on the dept_no column. However, if a new index, emp_no is created for table emp, the optimizer can consider it.

Join-Method Directives

Use join-method directives to influence how the database server joins tables in a query.

Element Purpose Restrictions Syntax
alias Temporary alternative name assigned to the table or view in the FROM clause When an alias is declared in the from clause, the alias also must be used in the optimizer directive. Identifier, p. 4-205
comments Any text that explains the purpose of the directive or other significant information Text must appear outside the parenthesis, but inside the comment symbols. Character string
synonym Name of the synonym for which you want to specify a query plan directive The synonym and the table to which the synonym points must exist. Database Object Name, p. 4-50
table Name of the table for which you want to specify a query plan directive The table must exist. Database Object Name, p. 4-50

You can separate the elements that appear within the parentheses with either one or more spaces or by commas.

The following table lists the purpose of each of the join-method directives

Keyword Purpose
USE_NL Uses the listed tables as the inner table in a nested-loop join. If n tables are specified in the FROM clause, then at most n-1 tables can be specified in the USE_NL join-method directive.
USE_HASH Uses a hash join to access the listed table. You can also choose whether the table will be used to create the hash table, or to probe the hash table.
AVOID_NL Does not use the listed table as the inner table in a nested loop join. A table listed with this directive can still participate in a nested loop join as the outer table.
AVOID_HASH Does not access the listed table using a hash join. Optionally, you can allow a hash join, but restrict the table from being the one that is probed, or the table from which the hash table is built.

A join-method directive takes precedence over the join method forced by the OPTCOMPIND configuration parameter.

Specifying the Role of the Table in a Hash Join

When you specify that you want to avoid or use a hash join, you can also specify the role of each table:

If neither the BUILD nor PROBE keyword is specified, the optimizer uses cost to determine the role of the table.

Example Using Join Method Directives

In the following example, the USE_HASH join-method directive forces the optimizer to construct a hash table on the dept table and consider only the hash table to join the dept table with the other tables. Because no other directives are specified, the optimizer can choose the least expensive join methods for the other joins in the query.

Optimization-Goal Directives

Use optimization-goal directives to specify the measure that is used to determine the performance of a query result.

Element Purpose Restrictions Syntax
comments Any text that explains the purpose of the directive or other significant information Text must appear outside the parenthesis, but inside the comment symbols. Character string

The two optimization-goal directives are:

An optimization-goal directive takes precedence over the OPT_GOAL environment variable and the OPT_GOAL configuration parameter.

Restrictions on Optimization-Goal Directives

You cannot use an optimization-goal directive in the following instances:

Example of an Optimization-Goal Directive

The following query returns the names of the employees who earned the top fifty bonuses. The optimization-goal directive directs the optimizer to return the first screenful of rows as fast as possible.

For information about how to set the optimization goal for an entire session, see the SET OPTIMIZATION statement.

Directive-Mode Directive

Use the EXPLAIN directive-mode directive to turn SET EXPLAIN ON for a particular query. You can use this directive to test and debug query plans. Information about the query plan is printed to the sqexplain.out file. This directive is redundant when SET EXPLAIN ON is already specified.

You cannot use the EXPLAIN directive-mode directive in two situations:

Related Information

For information about the sqexplain.out file, see SET EXPLAIN.

For information about how to set optimization settings for an entire session, see SET OPTIMIZATION.

For a discussion about optimizer directives and performance, see your Performance Guide.

For information on the IFX_DIRECTIVES environment variable, see the Informix Guide to SQL: Reference.

For information on the DIRECTIVES parameter in the onconfig file, see your Administrator's Reference.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved