Bird
Raised Fist0
Interview Prepdatabase-management-systemshardAmazonGoogleFlipkartZeptoRazorpay

Query Optimization - Explain Plan, Cost Estimation, Cardinality

Choose your preparation mode3 modes available
🎯
Query Optimization - Explain Plan, Cost Estimation, Cardinality
hardDBMSAmazonGoogleFlipkart

Imagine a busy e-commerce platform where millions of queries run every day; optimizing these queries efficiently saves significant compute resources and improves user experience.

💡 Beginners often think query optimization is just about indexes or rewriting queries, but it fundamentally relies on understanding how the database estimates costs and chooses execution plans based on data statistics. Think of it like planning a trip: the database needs to estimate how long each route will take (cost) and how many stops it will make (cardinality) before choosing the best path (execution plan).
📋
Interview Question

Explain what an Explain Plan is in query optimization, how cost estimation works, and the role of cardinality in this process.

Explain Plan as a representation of the query execution strategyCost Estimation based on resource usage predictionsCardinality as an estimate of the number of rows processed at each step
💡
Scenario & Trace
ScenarioA user runs a complex SQL query joining two large tables with filters on indexed and non-indexed columns.
The database optimizer generates multiple possible execution plans → It uses statistics to estimate cardinality for each join and filter step → Cost estimation calculates CPU, I/O, and memory usage for each plan → The optimizer selects the plan with the lowest estimated cost → Explain Plan shows the chosen plan with stepwise operations and estimated row counts.
ScenarioA query with a WHERE clause on a highly selective indexed column versus a non-selective column.
Optimizer uses statistics to estimate cardinality after applying the filter → For the selective column, cardinality is low, leading to a plan using index seek → For the non-selective column, cardinality is high, leading to a full table scan plan → Explain Plan reflects these differences with cost estimates.
  • Statistics are outdated or missing → optimizer makes poor cardinality estimates → suboptimal plans chosen
  • Highly skewed data distribution → average statistics mislead cardinality estimation
  • Complex predicates or user-defined functions → cardinality estimation becomes inaccurate
⚠️
Common Mistakes
Confusing cardinality with actual row count

Interviewer thinks candidate lacks understanding of estimation vs reality

✅ Clarify cardinality is an estimate used during optimization, not the actual number of rows returned

Assuming Explain Plan shows actual runtime execution

Interviewer doubts candidate’s grasp of optimization vs execution

✅ Explain that Explain Plan shows the optimizer’s chosen plan before execution, which may differ from actual runtime due to dynamic factors

Ignoring the role of statistics in cost estimation

Interviewer perceives superficial knowledge of optimizer internals

✅ Emphasize that statistics are foundational for cardinality estimates and cost calculations

Believing cost estimation is always accurate

Interviewer questions candidate’s practical understanding

✅ Acknowledge that cost estimation is heuristic and can be inaccurate due to data distribution or complex predicates

🧠
Basic Definition - What It Is
💡 This level covers the essential concepts you must know to answer basic interview questions confidently. Think of it as knowing the names and roles of players on a sports team before understanding their strategies.

Intuition

Explain Plan shows how the database intends to execute a query, cost estimation predicts resource usage, and cardinality estimates rows processed.

Explanation

An Explain Plan is a tool that reveals the steps the database engine will take to execute a query. It breaks down the operations like scans, joins, and filters. Cost estimation is the process where the optimizer predicts how expensive each operation will be in terms of CPU, I/O, and memory. Cardinality refers to the estimated number of rows that will be processed or returned at each step. Together, these help the optimizer choose the most efficient execution plan.

Memory Hook

💡 Think of Explain Plan as a GPS route, cost estimation as the travel time prediction, and cardinality as the number of stops along the way.

Illustrative Code

Interview Questions

❓ What is an Explain Plan and why is it useful?
  • Explain Plan shows the query execution steps
  • It helps understand and optimize query performance
❓ Define cardinality in query optimization.
  • Cardinality is the estimated number of rows processed
  • It influences cost estimation and plan choice
⏱
Depth Level
Interview Time30 seconds
Depthbasic

This approach covers fundamental definitions and their purpose; no algorithmic complexity applies as it is conceptual.

Interview Target: Minimum floor - never go below this

Knowing only this helps pass initial screening but is insufficient for deeper technical rounds.

🧠
Mechanism Depth - How It Works
💡 This level explains the internal workings and is expected in product company interviews. Imagine the optimizer as a chef who estimates ingredient quantities (cardinality) and cooking time (cost) before choosing the best recipe (execution plan).

Intuition

The optimizer uses statistics to estimate cardinality, calculates costs for each plan, and selects the plan with the lowest cost.

Explanation

Query optimization starts with the optimizer generating multiple candidate execution plans for a query. It relies on database statistics-such as histograms, table row counts, and index selectivity-to estimate cardinality at each operation node (e.g., scans, joins, filters). Cardinality estimates feed into cost models that predict resource consumption like CPU cycles, disk I/O, and memory usage. The optimizer compares these costs across plans and picks the one with the minimal estimated cost. The Explain Plan output details this chosen plan, showing operation order, estimated row counts, and cost metrics. Accurate statistics are critical; stale or missing stats lead to poor cardinality estimates and suboptimal plans. Additionally, cost models may vary between databases but generally balance CPU, I/O, and memory trade-offs.

Memory Hook

💡 Imagine a chef planning a recipe: cardinality is the ingredient quantity estimate, cost estimation is the cooking time and effort, and the Explain Plan is the final recipe chosen.

Illustrative Code

Interview Questions

❓ How does the optimizer use cardinality estimates in cost calculation?
  • Cardinality estimates rows processed at each step
  • Higher cardinality increases estimated CPU and I/O cost
  • Accurate estimates lead to better plan selection
❓ What happens if statistics are outdated?
  • Cardinality estimates become inaccurate
  • Cost estimation is unreliable
  • Optimizer may choose inefficient plans
⏱
Depth Level
Interview Time2-3 minutes
Depthintermediate

This approach explains the internal mechanics of the optimizer, including how statistics and cardinality estimates influence cost calculations and plan selection. While no code is provided, understanding these concepts is critical for technical interviews.

Interview Target: Target level for FAANG on-sites

Mastering this level distinguishes you from most candidates and shows deep understanding.

📊
Explanation Depth Levels
💡 Choose your explanation depth based on the interview stage and company expectations.
LevelInterview TimeSuitable ForRisk
Basic Definition30sScreening call or initial roundsToo shallow for on-site interviews at top tech companies
Mechanism Depth2-3 minutesOn-site interviews at FAANG and similar companiesRequires solid understanding; missing details may lead to follow-up questions
💼
Interview Strategy
💡 Use this guide to structure your explanation and anticipate interviewer questions before every mock or real interview. Think of it as your roadmap to cover all key points clearly and confidently.

How to Present

Start with a clear definition of Explain Plan, cost estimation, and cardinality.Give a relatable example or analogy to make the concept tangible.Explain how the optimizer uses statistics to estimate cardinality and costs.Discuss edge cases like outdated statistics and their impact on optimization.

Time Allocation

Definition: 30s → Example: 1min → Mechanism: 2min → Edge cases: 30s. Total ~4min

What the Interviewer Tests

Interviewer checks if you understand the purpose of Explain Plan, how cost and cardinality influence plan choice, and the importance of accurate statistics.

Common Follow-ups

  • What are common sources of cardinality estimation errors? → Data skew, outdated stats, complex predicates
  • How do cost models differ between databases? → Weighting of CPU vs I/O, memory considerations
💡 These follow-ups test your depth and practical understanding beyond definitions.
🔍
Pattern Recognition

When to Use

Interviewers ask about this topic when discussing query performance, database internals, or optimization strategies.

Signature Phrases

'Explain the Explain Plan''How does cost estimation work?''What is cardinality and why is it important?'

NOT This Pattern When

Similar Problems

Practice

(1/5)
1. Trace the sequence of events when a denormalized database updates a record that exists in multiple redundant tables.
easy
A. The update must be manually or programmatically applied to each redundant copy, risking temporary inconsistency
B. The database engine prevents updates to denormalized fields to maintain integrity
C. The update occurs once in the primary table and automatically propagates to all redundant copies without delay
D. Updates are queued and applied asynchronously, ensuring eventual consistency but possible stale reads

Solution

  1. Step 1: Understand denormalized data updates

    Denormalization duplicates data, so updates must be applied to all copies to maintain consistency.
  2. Step 2: Analyze update propagation

    Most systems require explicit or programmatic updates to each redundant copy; automatic propagation is rare.
  3. Step 3: Evaluate options

    The update occurs once in the primary table and automatically propagates to all redundant copies without delay incorrectly assumes automatic propagation without delay. The database engine prevents updates to denormalized fields to maintain integrity is false; updates are allowed but complex. Updates are queued and applied asynchronously, ensuring eventual consistency but possible stale reads describes an eventual consistency model, which may apply but is not guaranteed.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Manual or programmatic updates risk temporary inconsistency in denormalized data [OK]
Hint: Denormalized updates require explicit multi-copy changes, risking inconsistency [OK]
Common Mistakes:
  • Assuming automatic update propagation
  • Believing denormalized fields are immutable
  • Confusing synchronous and asynchronous update models
2. Trace the sequence of events when a network partition occurs in a distributed NoSQL database that prioritizes availability and partition tolerance. What happens to consistency and client reads during the partition?
easy
A. Clients may read stale data as writes continue independently on partitioned nodes
B. Consistency is immediately restored by synchronizing all nodes during the partition
C. The database rejects all reads and writes to prevent inconsistency
D. The database blocks all writes until the partition heals to maintain consistency

Solution

  1. Step 1: Understand CAP priorities

    The database prioritizes availability and partition tolerance, so it must remain operational during partitions.
  2. Step 2: Effect on writes and reads

    Writes continue independently on partitioned nodes to maintain availability, which can cause data divergence.
  3. Step 3: Impact on consistency

    Because nodes can't synchronize during partition, clients may read stale or divergent data temporarily.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Availability + Partition tolerance -> possible stale reads during partition [OK]
Hint: Availability + Partition tolerance -> stale reads allowed during partition
Common Mistakes:
  • Assuming writes are blocked to maintain consistency
  • Believing reads are rejected during partitions
  • Thinking consistency is instantly restored during partition
3. In which scenario is the Two-Phase Locking (2PL) protocol most appropriate to ensure serializability in a database system?
easy
A. When transactions require only read operations and no writes
B. When transactions involve both reads and writes and strict serializability is required
C. When transactions are short-lived and deadlocks are impossible
D. When optimistic concurrency control is preferred over locking

Solution

  1. Step 1: Understand 2PL's purpose

    2PL ensures serializability by acquiring locks in a growing phase and releasing them in a shrinking phase, which is essential when transactions perform both reads and writes.
  2. Step 2: Analyze each option

    A is incorrect because 2PL is not necessary if only reads occur; shared locks suffice without strict 2PL. B is unrelated as optimistic concurrency control avoids locking. C is wrong since deadlocks can occur regardless of transaction length. D correctly identifies the scenario where 2PL is most appropriate.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    2PL is designed for mixed read-write transactions requiring strict serializability.
Hint: 2PL is for mixed read-write transactions needing serializability [OK]
Common Mistakes:
  • Assuming 2PL is needed for read-only transactions
  • Believing short transactions cannot deadlock
  • Confusing locking with optimistic concurrency
4. Consider a relation R(A,B,C,D) with functional dependencies F = {A -> B, B -> C, C -> D}. You decompose R into R1(A,B), R2(B,C), and R3(C,D). Which of the following statements about this decomposition is TRUE regarding lossless join and dependency preservation?
hard
A. The decomposition is neither lossless nor dependency preserving because the chain of dependencies is broken across relations.
B. The decomposition is lossless and dependency preserving because each FD is represented in one of the decomposed relations.
C. The decomposition is dependency preserving but not lossless because the common attributes do not form keys in the decomposed relations.
D. The decomposition is lossless but not dependency preserving because enforcing A -> B requires joining R1 and R2.

Solution

  1. Step 1: Check lossless join condition

    Each pair of relations shares a key attribute: R1 and R2 share B (key in R2), R2 and R3 share C (key in R3), so lossless join holds.
  2. Step 2: Check dependency preservation

    All FDs appear in one of the relations: A -> B in R1, B -> C in R2, C -> D in R3.
  3. Step 3: Confirm no FD requires join enforcement

    Each FD is enforceable in its own relation, so dependency preservation holds.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Options A, B, and C incorrectly claim loss or preservation failures.
Hint: If each FD is in one relation and common attributes are keys, decomposition is lossless and dependency preserving.
Common Mistakes:
  • Assuming chain dependencies require joins to enforce
  • Believing lossless join requires all keys in all relations
  • Confusing dependency preservation with global key presence
5. Suppose you want to extend the SQL query execution simulation to handle queries where LIMIT is applied without an ORDER BY clause. Which of the following statements about the behavior of LIMIT in this case is correct, and what adjustment should be made to the simulation to reflect real SQL behavior?
hard
A. LIMIT cannot be used without ORDER BY; simulation should raise an error if ORDER BY is missing.
B. LIMIT returns an arbitrary subset of rows without ORDER BY; simulation should note LIMIT applies after FROM and WHERE but before ORDER BY.
C. LIMIT always returns the first N rows sorted by default; no change needed in simulation.
D. LIMIT returns arbitrary rows without ORDER BY; simulation should reflect that LIMIT applies last but does not guarantee order.

Solution

  1. Step 1: Understand LIMIT behavior without ORDER BY

    Without ORDER BY, LIMIT returns an arbitrary subset of rows, not sorted, so results may be inconsistent.
  2. Step 2: Adjust simulation to reflect logical execution

    Simulation should keep LIMIT as the last clause executed but clarify it does not guarantee sorted output without ORDER BY.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    LIMIT applies last but unordered without ORDER BY [OK]
Hint: LIMIT without ORDER BY returns arbitrary rows [OK]
Common Mistakes:
  • Assuming LIMIT sorts rows by default
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard