In addition to applying hints directly to tables that are referenced in the SQL command, you can apply hints to tables that appear in a view when the view is referenced in the SQL command. The hint doesn't appear in the view but in the SQL command that references the view.
When specifying a hint that applies to a table in a view, give the view and table names in dot notation in 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 last example of Joining relations hints.
The query plan produced by selecting from this view is:
The same hints that were applied to this join at the end of Joining relations hints can be applied to the view:
Applying hints to tables in subqueries
In addition to applying hints to tables in stored views, you can apply hints to tables in subqueries. 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:
This code shows the plan chosen by the query planner:
You can apply a hint to the emp table in the subquery to perform an index scan on index emp_pk instead of a table scan. Note the difference in the query plans.