Query Optimization Examples
Query Tuning is in Beta
This functionality may still change without notice. Let us know your feedback!
Query optimization often involves recognizing and addressing common patterns that lead to suboptimal performance. While seasoned database engineers may spot these patterns through experience, Query Tuning helps identify and solve them systematically.
We'll explore three common patterns that often impact query performance:
- Inefficient Nested Loops
- ORDER BY + LIMIT causing wrong Index Selection
- GROUP BY causing wrong Index Selection
Inefficient Nested Loops
When the query planner chooses nested loops for joins where other strategies might be more efficient, Query Tuning can help identify and resolve the issue.
Detection
Query Tuning automatically flags cases where Nested Loop joins show actual rows significantly higher than estimated, using EXPLAIN Insights.
You can also identify cases where Inner side operations consume substantial time by viewing the individual plans.
Investigation
- Create a new workbook and run
EXPLAIN ANALYZE
using the collector or upload your query'sEXPLAIN ANALYZE
plan manually - Review the automated analysis that highlights potential nested loop inefficiencies
- Create variants to test alternative approaches:
- Create a variant with
enable_nestloop = off
- If available, use pg_hint_plan to force Hash or Merge Join in another variant
- Create a variant with
- Use Compare Plans to compare the execution plans side-by-side
Benchmarking Solutions
- Create a variant using MATERIALIZED to prevent parameterized scans
- Compare the execution metrics between variants
- Validate performance across different parameter sets
ORDER BY + LIMIT causing wrong Index Selection
This pattern involves cases where ORDER BY combined with LIMIT leads to suboptimal index usage.
Detection
View individual plans in a Query Tuning Workbook to identify:
- Index Scans with high filter removal rates
- Indexes being used primarily for sort ordering
- Potential quick sort scan inefficiencies
Investigation Process
- Initial Analysis:
- Review highlighted metrics on filter removal rates
- Check available indexes against WHERE conditions
- Create variants to test:
- Different index hints (using pg_hint_plan if available)
- ORDER BY col+0 modification (learn more)
enable_incremental_sort = off
setting (learn more)
- Compare performance across variants
Benchmarking Solutions
- Test different statistics target values
- Compare plans with and without incremental sort
- Verify performance across different
LIMIT
values - Document the most effective approach
GROUP BY causing wrong Index Selection
This pattern occurs when GROUP BY operations lead to suboptimal index choices.
Detection
View individual plans in a Query Tuning Workbook to identify:
- Index Scans lacking Index Conditions (using Filter only)
- Merge Joins where index sort order appears to take precedence over filtering
- Cases where parameterized index scans might be beneficial
Investigation Process
- Start with baseline plan analysis:
- Upload your current EXPLAIN plan
- Review highlighted index usage patterns
- Check scan efficiency metrics
- Create test variants
- Disable merge joins to test nested loop performance
- Use pg_hint_plan if available to test specific join orders
- Compare variant performance
Benchmarking Solutions
- Test
MATERIALIZED
query variants - Verify performance across different parameter sets
- Use Compare Plans to understand execution strategy changes
Couldn't find what you were looking for or want to talk about something specific?
Start a conversation with us →