Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 34/ 160
02. Advanced SQL & Relational Databases

Query Optimization, Partition Pruning & Avoiding Full Table Scans

Rewrite predicates and join order so the planner can prune partitions and avoid unnecessary full scans.

AdvancedOptimization · Partition Pruning · Scans3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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. 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.

Visual diagram

Loading interactive 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.

Query Optimization, Partition Pruning & Avoiding Full Table Scans in one concrete design

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.

Key takeaway: 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

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
Optimization

1. 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.

actual time, rows, reads, and spills
Key numbers at this step
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

Optimization
Best for: The practice of changing query shape, access paths, or physical design to reduce measured total resource work.

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.

Partition Pruning
Best for: Eliminating partitions by query metadata because their stored key ranges cannot contain matching rows.

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.

Scans
Best for: Reading or processing far more rows, columns, or partitions than are needed for the final result.

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, while date_trunc('day', event_time) >= ... can prevent a direct partition/index lookup; test pg_stats and 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?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Reading EXPLAIN ANALYZE & Query Execution Plans
Next topic
Table Partitioning (Range, List, Hash) & Sharding Concepts