SQL Hints
The SQL Hints mechanism allows users to influence SQL execution by adding hints within comments, enabling custom execution plans. Tacnode supports this capability via the pg_hint_plan
extension. This lets developers guide the optimizer to generate specific plans in certain scenarios, improving performance. Especially useful when the default optimizer cannot make optimal decisions, such as with insufficient statistics or complex queries.
Usage Scenarios
Primary use cases for pg_hint_plan
:
- Complex Query Optimization: Handling JOINs, multi-table queries, and index intervention.
- Static Plan Comparison: Forcing fixed plans during performance debugging for comparative testing.
- Workaround for Optimizer Limitations: Correcting suboptimal plans when the optimizer fails to utilize indexes or efficient paths.
- Stress Testing/Lab Tuning: Testing the impact of different execution plans on overall performance.
Extension Loading
Load the extension Run the following command with superuser privileges. For more details, see pg_hint_plan:
Verify installation Run the following query to check if the extension is loaded:
Supported Hint Types
pg_hint_plan
injects hints via SQL comments. If table aliases are used, parameters must match the alias. Syntax:
-
Scan Method Control Specify scan method for queries. Supported:
SeqScan
,IndexScan
. -
Join Method Control Specify join order and algorithm. Supported:
NestLoop
,HashJoin
,MergeJoin
. -
Force Join Order Override join order. Use parentheses to group join sides; for multiple tables, nest parentheses to form a tree structure.
-
Set GUC mannually Set query runtime GUC, effective for the current query
Common Operations
Table structure and data preparation
Create sample tables: users
(user info), orders
(order info), and products
(product info), with necessary indexes.
Insert test data for query validation.
Force Index Scan
To query all orders for a specific user, the optimizer may choose a full table scan. Force index scan as follows:
Verify execution plan using EXPLAIN
:
Adjust JOIN Order
To query users and their orders, the optimizer may select a suboptimal join order. Use the Leading
hint to specify join order.
Verify execution plan:
To optimize further, place products
first in the join order.
Verify execution plan:
Force Nested Loop Join
In some cases, force the use of nested loop join (Nested Loop
):
Verify execution plan:
Force Hash Join
To force a hash join when the optimizer chooses nested loop:
Verify execution plan:
Force Sequential Scan
When the optimizer chooses index scan but sequential scan is preferred:
Verify execution plan:
Setting GUC Mannually
In certain scenarios, you may want to set GUC parameters specifically for a single query to exercise fine-grained control. For example, setting a much shorter timeout for a specific query acts as a kind of blacklist for that query.
pg_hint_plan.hint_table Configuration
The hint table feature allows you to dynamically assign optimizer hints to specific types of queries.
Enable the required parameters
hint_table Data Structure
The structure of the hint_table is as follows. By inserting rules into this table, you can dynamically adjust the behavior of pg_hint:
Examples
Change the query plan
Step 1: Obtain the query_id
-
Execute the target query with EXPLAIN:
-
Retrieve the query_id from the plan output:
Step 2: Insert the hint into hint_table
Step 3: Verify hint effectiveness
Re-execute the query and review the execution plan; it should now show an index scan in use.
Block Specific Query
- Execute the target query with EXPLAIN VERBOSE to obtain its query ID:
- Retrieve the query_id from the query plan output:
Step 2: Insert the hint into the hint_table
Step 3: Verify the effect of the hint
Re-run the query to confirm it is terminated with a 1ms timeout.
View Registered Hints
Notes
- Feature Support Limitations: For complex queries, hints may be ignored by the optimizer. Always verify the actual execution plan repeatedly via
EXPLAIN
. - Hint Syntax Requirements: Hints must strictly follow the extension’s syntax; otherwise, they are ignored.
- Statistics Accuracy: Make sure the table's statistics are up-to-date to avoid generating suboptimal plans.
- Avoid Over-optimization: Use hints only when the optimizer cannot deliver an ideal plan; prioritize relying on the native Tacnode statistical model.