The following hints influence how the optimizer accesses relations to create the result set.
Hint
Description
FULL(table)
Perform a full sequential scan on table.
INDEX(table [ index ] [...])
Use index on table to access the relation.
NO_INDEX(table [ index ] [...])
Do not use index on table to access the relation.
In addition, the ALL_ROWS, FIRST_ROWS, and FIRST_ROWS(n) hints can be used.
Examples
The sample application does not have sufficient data to illustrate the effects of optimizer hints so the remainder of the examples in this section will use a banking database created by the pgbench application located in the Advanced Server bin subdirectory.
The following steps create a database named, bank, populated by the tables, pgbench_accounts, pgbench_branches, pgbench_tellers, and pgbench_history. The –s 20 option specifies a scaling factor of twenty, which results in the creation of twenty branches, each with 100,000 accounts, resulting in a total of 2,000,000 rows in the pgbench_accounts table and twenty rows in the pgbench_branches table. Ten tellers are assigned to each branch resulting in a total of 200 rows in the pgbench_tellers table.
The following initializes the pgbench application in the bank database.
A total of 500,00 transactions are then processed. This will populate the pgbench_history table with 500,000 rows.
The table definitions are shown below:
The EXPLAIN command shows the plan selected by the query planner. In the following example, aid is the primary key column, so an indexed search is used on index, pgbench_accounts_pkey.
The FULL hint is used to force a full sequential scan instead of using the index as shown below:
The NO_INDEX hint forces a parallel sequential scan instead of use of the index as shown below:
In addition to using the EXPLAIN command as shown in the prior examples, more detailed information regarding whether or not a hint was used by the planner can be obtained by setting the trace_hints configuration parameter as follows:
The SELECT command with the NO_INDEX hint is repeated below to illustrate the additional information produced when the trace_hints configuration parameters is set.
Note that if a hint is ignored, the INFO: [HINTS] line will not appear. This may be an indication that there was a syntax error or some other misspelling in the hint as shown in the following example where the index name is misspelled.