Bird
Raised Fist0
Interview Prepdatabase-management-systemshardAmazonGoogleFlipkartZeptoRazorpay

Query Optimization - Explain Plan, Cost Estimation, Cardinality

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize Explain Plan Tree

The optimizer initializes the explain plan as an empty B-tree with order 3, preparing to insert query operations as nodes.

💡 Setting up the tree structure is essential to organize query operations hierarchically for cost estimation.
Line:plan_tree = BTree(order=3) plan_tree.root = plan_tree.create_node(is_leaf=True)
💡 The explain plan starts empty and will grow as operations are added.
📊
Query Optimization - Explain Plan, Cost Estimation, Cardinality - Watch the Algorithm Execute, Step by Step
Watching the algorithm execute visually helps you understand how query plans are constructed and optimized, revealing the interplay between cost estimation and cardinality at each node.
Step 1/10
·Active fill★Answer cell
insert
leaf
Initialized empty explain plan tree with root node.
insertKey: {"operation":"scan","cardinality":1000,"cost":10}
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Inserted scan operation node with cardinality 1000 and cost 10.
insertKey: {"operation":"filter","cardinality":500,"cost":5}
{"operation":"filter","cardinality":500,"cost":5}{"operation":"scan","cardinality":1000,"cost":10}
leaf
Inserted filter operation node with cardinality 500 and cost 5.
splitKey: {"operation":"join","cardinality":200,"cost":50}↑ promoted: {"operation":"join","cardinality":200,"cost":50}
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Root node split due to join insertion; join key promoted.
traverseKey: {"operation":"filter","cardinality":500,"cost":5}
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Traversing left child node to estimate filter subtree cost.
compareKey: {"operation":"filter","cardinality":500,"cost":5}
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Confirmed cost and cardinality at filter node.
traverseKey: {"operation":"scan","cardinality":1000,"cost":10}
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Traversing right child node to estimate scan subtree cost.
compareKey: {"operation":"scan","cardinality":1000,"cost":10}
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Confirmed cost and cardinality at scan node.
compareKey: {"operation":"join","cardinality":200,"cost":50}
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Calculated join node cost and cardinality from children.
traverse
{"operation":"join","cardinality":200,"cost":50}
{"operation":"filter","cardinality":500,"cost":5}
leaf
{"operation":"scan","cardinality":1000,"cost":10}
leaf
Explain plan finalized with all cost and cardinality estimates.

Key Takeaways

✓ Query optimization builds a hierarchical explain plan using a B-tree structure to organize operations.

This hierarchical structure is hard to grasp from code alone but becomes clear when visualized step-by-step.

✓ Cost and cardinality estimates propagate bottom-up, with leaf nodes providing base metrics and parent nodes aggregating them.

Understanding how these metrics accumulate is easier when you see each node's calculation in the tree.

✓ Node splits maintain balance in the explain plan tree, ensuring efficient organization of query operations.

The decision to split and promote keys is subtle in code but visually obvious in the tree structure.

Practice

(1/5)
1. In a database system that requires efficient range queries on large datasets stored on disk, which tree structure is most suitable and why?
easy
A. B+ Tree, because all data records are stored in leaf nodes linked sequentially, optimizing range scans.
B. B-Tree, because it stores keys and data in all nodes, allowing faster point queries.
C. B-Tree, because it reduces the height of the tree by storing data only in internal nodes.
D. B+ Tree, because it stores data only in internal nodes, minimizing disk I/O for all queries.

Solution

  1. Step 1: Understand data storage in B-Tree vs B+ Tree

    B-Tree stores keys and data in all nodes, while B+ Tree stores data only in leaf nodes, which are linked sequentially.
  2. Step 2: Consider range query efficiency

    Range queries benefit from sequential access to data, which B+ Tree's linked leaf nodes provide, reducing disk I/O.
  3. Step 3: Analyze why other options are incorrect

    B-Tree, because it stores keys and data in all nodes, allowing faster point queries. is incorrect because storing data in all nodes does not optimize range queries. B-Tree, because it reduces the height of the tree by storing data only in internal nodes. is wrong as B-Trees store data in all nodes, not only internal nodes. B+ Tree, because it stores data only in internal nodes, minimizing disk I/O for all queries. incorrectly states B+ Tree stores data only in internal nodes.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    B+ Tree's leaf-level linked list is key for efficient range queries.
Hint: B+ Tree's linked leaves -> efficient range scans
Common Mistakes:
  • Confusing where data is stored in B-Tree vs B+ Tree
  • Assuming data in internal nodes always speeds up queries
  • Ignoring the impact of leaf node linkage on range queries
2. In a high-concurrency OLTP system where multiple transactions frequently update different rows of the same table, which locking strategy is most appropriate to maximize throughput while maintaining consistency?
easy
A. Table-level locking to ensure simplicity and avoid deadlocks
B. Row-level locking to allow concurrent updates on different rows
C. No locking, relying solely on optimistic concurrency control
D. Page-level locking to balance granularity and overhead

Solution

  1. Step 1: Understand the concurrency needs

    High concurrency with frequent updates on different rows requires fine-grained locking to avoid unnecessary blocking.
  2. Step 2: Evaluate locking granularity

    Table-level locking (B) is too coarse and reduces concurrency. Page-level locking (D) is intermediate but still blocks unrelated rows. No locking (C) with optimistic control risks conflicts and rollbacks under high contention.
  3. Step 3: Confirm best strategy

    Row-level locking (A) allows multiple transactions to update different rows simultaneously, maximizing throughput while preserving consistency.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Row-level locking allows multiple transactions to update different rows simultaneously, maximizing throughput while preserving consistency.
Hint: Use row-level locks when many transactions update different rows concurrently.
Common Mistakes:
  • Assuming table-level locking is always simpler and better
  • Believing optimistic concurrency always outperforms locking
  • Confusing page-level locking benefits
3. Suppose a database uses snapshot isolation to handle concurrent transactions. Which scenario can still violate Consistency despite snapshot isolation, and why?
hard
A. A transaction commits without flushing changes to disk, violating durability.
B. Two concurrent transactions update different rows, causing no conflicts and preserving consistency.
C. A transaction reads uncommitted data from another transaction, causing dirty reads.
D. Two concurrent transactions read the same snapshot and update overlapping data, leading to write skew anomalies.

Solution

  1. Step 1: Understand snapshot isolation

    Snapshot isolation prevents many anomalies but can allow write skew, where concurrent transactions read the same snapshot and update overlapping data inconsistently.
  2. Step 2: Analyze options

    A describes the write skew anomaly possible under snapshot isolation. B is safe and consistent. C describes read uncommitted, not snapshot isolation. D relates to durability, not consistency.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Write skew under snapshot isolation can violate consistency.
Hint: Snapshot isolation can allow write skew anomalies
Common Mistakes:
  • Assuming snapshot isolation prevents all anomalies
  • Confusing dirty reads with snapshot isolation behavior
4. 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
5. In a geo-distributed NoSQL system designed for high availability and partition tolerance, an application requires strict consistency for a subset of critical transactions. Which approach best addresses this requirement without sacrificing overall system availability?
hard
A. Switch the entire system to a strongly consistent SQL database cluster
B. Accept eventual consistency for all transactions to maintain availability
C. Implement a hybrid approach using multi-model databases or selective consistency zones for critical data
D. Use synchronous replication across all nodes for all data to guarantee consistency

Solution

  1. Step 1: Understand system constraints

    The system is geo-distributed, prioritizing availability and partition tolerance, but needs strict consistency for some transactions.
  2. Step 2: Evaluate options

    Switch the entire system to a strongly consistent SQL database cluster sacrifices availability globally, which is undesirable. Accept eventual consistency for all transactions to maintain availability ignores strict consistency needs. Use synchronous replication across all nodes for all data to guarantee consistency sacrifices availability due to synchronous replication delays.
  3. Step 3: Hybrid solution

    Implement a hybrid approach using multi-model databases or selective consistency zones for critical data proposes selective consistency zones or multi-model databases, allowing critical transactions to be strongly consistent while maintaining availability elsewhere.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Hybrid approaches balance consistency and availability per data criticality [OK]
Hint: Hybrid selective consistency zones enable strict consistency without losing availability globally
Common Mistakes:
  • Thinking entire system must switch to SQL for consistency
  • Assuming eventual consistency suffices for critical transactions
  • Believing synchronous replication is always feasible in geo-distributed setups
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