Thus far, hints have been applied directly to tables that are referenced in the SQL command. It is also possible to apply hints to tables that appear in a view when the view is referenced in the SQL command. The hint does not appear in the view, itself, but rather in the SQL command that references the view.
When specifying a hint that is to apply to a table within a view, the view and table names are given in dot notation within the hint argument list.
A view named, tx, is created from the three-table join of pgbench_history, pgbench_branches, and pgbench_accounts shown in the final example of Joining Relations Hints.
The query plan produced by selecting from this view is shown below:
The same hints that were applied to this join at the end of Joining Relations Hints can be applied to the view as follows:
In addition to applying hints to tables within stored views, hints can be applied to tables within subqueries as illustrated by the following example. In this query on the sample application emp table, employees and their managers are listed by joining the emp table with a subquery of the emp table identified by the alias, b.
The plan chosen by the query planner is shown below:
A hint can be applied to the emp table within the subquery to perform an index scan on index, emp_pk, instead of a table scan. Note the difference in the query plans.