Query Optimization, Partition Pruning & Avoiding Full Table Scans
Rewrite predicates and join order so the planner can prune partitions and avoid unnecessary full scans.
In plain words
Actual time, rows, reads, and spills. Next, projection, predicate, and partition pruning. Finally, prove cost reduction without regressions.
Think of it like…
Optimization is removing unopened crates from the train before buying a faster locomotive: prune and project first, then tune the remaining physical movement.
Where you’ve seen it
event_time >= DATE '2026-11-01' allows monthly range pruning, while date_trunc('day', event_time) >= ... can prevent a direct partition/index lookup; test pg_stats and the actual plan.
Words you’ll hear
- Optimization
- The practice of changing query shape, access paths, or physical design to reduce measured total resource work.
- Partition Pruning
- Eliminating partitions by query metadata because their stored key ranges cannot contain matching rows.
- Scans
- Reading or processing far more rows, columns, or partitions than are needed for the final result.
- Operational boundary
- Wrapping a partition key in a function or comparing unlike types prevents pruning;
SELECT *pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan. InspectEXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.
Visual diagram
Theory & deep dive
Optimization: the working mechanism
Query optimization removes work before asking about hardware: selective predicates become sargable, partitions are pruned by typed constants, joins are ordered by cardinality, and redundant sorts/scans are eliminated.
Actual time, rows, reads, and spills. Projection, predicate, and partition pruning, then cardinality order and measured access path; this exposes the state governed by Scans.
event_time >= DATE '2026-11-01' allows monthly range pruning, while date_trunc('day', event_time) >= ... can prevent a direct partition/index lookup; test pg_stats and the actual plan.
Query Optimization, Partition Pruning & Avoiding Full Table Scans: failure mode and production response
Wrapping a partition key in a function or comparing unlike types prevents pruning; SELECT * pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan.
Inspect EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path. Track Rows removed by filter as "a proxy for late rejection and poor access", and make Bytes scanned visible to the dataset owner.
EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.How it works, step by step
Step 1 of 41. Measure baseline
Actual time, rows, reads, and spills. Query optimization removes work before asking about hardware: selective predicates become sargable, partitions are pruned by typed constants, joins are ordered by cardinality, and redundant sorts/scans are eliminated.
- Rows removed by filter
- a proxy for late rejection and poor access
- Partition Pruning
- Eliminating partitions by query metadata because their stored key ranges cannot contain matching rows.
When to use it & trade-offs
What it is
Query optimization removes work before asking about hardware: selective predicates become sargable, partitions are pruned by typed constants, joins are ordered by cardinality, and redundant sorts/scans are eliminated. event_time >= DATE '2026-11-01' allows monthly range pruning, while date_trunc('day', event_time) >= ... can prevent a direct partition/index lookup; test pg_stats and the actual plan.
Why it matters: Wrapping a partition key in a function or comparing unlike types prevents pruning; SELECT * pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan. Inspect EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.
When to reach for it
- Rewrite predicates and join order so the planner can prune partitions and avoid unnecessary full scans.
- Choose Optimization because it provides this capability: The practice of changing query shape, access paths, or physical design to reduce measured total resource work.
- Keep evidence for Rows removed by filter at "a proxy for late rejection and poor access" so the last production stage-Re-measure - prove cost reduction without regressions-remains testable.
When to avoid or be careful
- Wrapping a partition key in a function or comparing unlike types prevents pruning;
SELECT *pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan. - Do not conflate Partition Pruning with Optimization. The second concept contributes different behavior: Eliminating partitions by query metadata because their stored key ranges cannot contain matching rows.
- Release only after this guardrail is in place: Inspect
EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.
Optimization / Partition Pruning / Scans - roles, limits, and operating cost
Strengths: The practice of changing query shape, access paths, or physical design to reduce measured total resource work. Query optimization removes work before asking about hardware: selective predicates become sargable, partitions are pruned by typed constants, joins are ordered by cardinality, and redundant sorts/scans are eliminated.
Watch out: Wrapping a partition key in a function or comparing unlike types prevents pruning; SELECT * pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan.
Strengths: Eliminating partitions by query metadata because their stored key ranges cannot contain matching rows. In this workflow it supports: Join efficiently - cardinality order and measured access path.
Watch out: The operating cost is real: target a small fraction of the input for selective queries.
Strengths: Reading or processing far more rows, columns, or partitions than are needed for the final result. Its output can be checked with Spill.
Watch out: Operating guardrail: Inspect EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Optimization, and how would Rows removed by filter reveal it?”
- “Why is Partition Pruning distinct from this behavior: reading or processing far more rows, columns, or partitions than are needed for the final result?”
- Production clue: Wrapping a partition key in a function or comparing unlike types prevents pruning; SELECT * pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan.
- Design probe: Inspect EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.
Benchmarks & rules of thumb
- Rows removed by filter
- a proxy for late rejection and poor access
- Bytes scanned
- target a small fraction of the input for selective queries
- Spill
- sort/hash work exceeded memory and wrote to local/distant disk
Common mistakes & follow-ups
Common interview mistakes
- Wrapping a partition key in a function or comparing unlike types prevents pruning;
SELECT *pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan. - Reporting success from a job exit code without checking Rows removed by filter and Bytes scanned.
- Treating this example as complete without replay, rollback, or access boundaries:
event_time >= DATE '2026-11-01'allows monthly range pruning, whiledate_trunc('day', event_time) >= ...can prevent a direct partition/index lookup; testpg_statsand the actual plan.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: The practice of changing query shape, access paths, or physical design to reduce measured total resource work.
- What does the recovery plan do when this occurs? Wrapping a partition key in a function or comparing unlike types prevents pruning;
SELECT *pulls wide blobs, and adding indexes without cardinality estimates can make writes slower without improving the plan. - When is Optimization insufficient for Bytes scanned, and what does adding Partition Pruning cost in Rows removed by filter?
EXPLAIN (ANALYZE, BUFFERS), apply row-level predicates before joins, compare typed constants, project only needed columns, update statistics, and add the smallest index that matches the measured hot path.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Query Optimization, Partition Pruning & Avoiding Full Table Scans with data engineer interview Q&A and hands-on practice.