Practice
FROM clause node?
class QueryPlanNode:
def __init__(self, name):
self.name = name
self.next = None
def set_next(self, node):
self.next = node
def execute(self):
print(f"Executing {self.name} clause")
if self.next:
self.next.execute()
from_clause = QueryPlanNode('FROM')
where_clause = QueryPlanNode('WHERE')
group_by_clause = QueryPlanNode('GROUP BY')
having_clause = QueryPlanNode('HAVING')
select_clause = QueryPlanNode('SELECT')
order_by_clause = QueryPlanNode('ORDER BY')
limit_clause = QueryPlanNode('LIMIT')
from_clause.set_next(where_clause)
where_clause.set_next(group_by_clause)
group_by_clause.set_next(having_clause)
having_clause.set_next(select_clause)
select_clause.set_next(order_by_clause)
order_by_clause.set_next(limit_clause)
from_clause.execute()Solution
Step 1: Trace execution starting from FROM node
Theexecute()method prints the current node's clause name, then recursively callsexecute()on the next node if it exists. The chain is FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT.Step 2: Confirm output order matches linked nodes
Each clause prints in the linked order, so output lines are exactly the clauses in the chain order.Final Answer:
Option A -> Option AQuick Check:
Output matches linked list traversal order [OK]
- Confusing printed order with written SQL order
Solution
Step 1: Understand AFTER INSERT trigger semantics
AFTER INSERT triggers execute only after the row has been successfully inserted into the table.Step 2: Exclude options contradicting this order
Triggers do not execute before insertion or simultaneously; they also do not execute on failure.Final Answer:
Option C -> Option CQuick Check:
AFTER triggers run post-operation, confirming the insertion occurred.
- Assuming triggers run before the data change
- Believing triggers run on failure
- Confusing BEFORE and AFTER triggers
Solution
Step 1: Understand optimistic concurrency assumptions
Optimistic control assumes conflicts are rare and avoids locking during transaction execution.Step 2: Analyze behavior under high contention
When conflicts are frequent, many transactions abort and retry, reducing throughput.Step 3: Evaluate misconception
Believing optimistic control always improves throughput ignores the cost of retries and aborts in high-contention scenarios.Final Answer:
Option A -> Option AQuick Check:
Optimistic concurrency is beneficial mainly when conflicts are infrequent.
- Assuming no locks means always better performance
- Ignoring the cost of transaction retries
- Confusing optimistic validation with locking
Solution
Step 1: Understand explain plan cardinality
Explain plans show estimated--not actual--row counts before execution.Step 2: Analyze options
Explain plans always show the exact number of rows that will be processed at each step during execution: Incorrect because explain plans show estimates, not exact counts.
Cardinality estimates in explain plans are predictions, not guaranteed counts: Correct--estimates can differ from actuals.
Explain plans help identify inefficient operations by showing estimated costs and row counts: Correct--explain plans reveal cost and cardinality to spot inefficiencies.
Cardinality estimates influence the optimizer's choice of join algorithms and access paths: Correct--cardinality guides join and access path choices.Final Answer:
Option C -> Option CQuick Check:
Explain plans provide estimates, not runtime exacts.
- Confusing estimated cardinality with actual runtime row counts
- Assuming explain plans reflect real-time execution
- Ignoring the predictive nature of cost and cardinality
Solution
Step 1: Understand update challenges in denormalization
Synchronously updating all copies can degrade write performance and increase latency.Step 2: Evaluate asynchronous update benefits
Asynchronous propagation allows writes to complete quickly, trading immediate consistency for eventual consistency.Step 3: Consider alternatives
Avoid denormalization entirely and rely solely on normalized tables with optimized indexes avoids denormalization but sacrifices read speed. Manually update redundant data only during off-peak hours to minimize performance impact delays updates, risking long inconsistency windows.Step 4: Assess triggers/stored procedures
Use triggers or stored procedures to synchronously update all redundant copies during each write operation can cause write bottlenecks and complexity.Final Answer:
Option D -> Option DQuick Check:
Eventual consistency balances performance and data freshness in denormalized systems [OK]
- Assuming synchronous updates are always best
- Ignoring the trade-off between consistency and performance
- Believing manual batch updates are practical for real-time systems
