SQL Performance and Anti Patterns Questions
Recognition and remediation of common SQL performance anti patterns and pitfalls, such as accidental cartesian joins, N plus one query patterns, inefficient correlated subqueries, using functions in WHERE clauses that prevent index use, SELECT star usage, lack of appropriate indexes, large unbounded sorts or aggregations, and poor join ordering. Covers methods to diagnose problems using execution plans, explain analyze, and rewriting queries for better performance and scalability.
EasyTechnical
48 practiced
You ran EXPLAIN on a query and saw lines like: Seq Scan on orders (cost=0.00..1200.00 rows=100000 width=48) Filter: (status = 'completed') and elsewhere Index Scan using idx_orders_customer_id on orders (cost=0.30..50.00 rows=10 width=40). Explain what 'seq scan', 'index scan', 'cost', and 'rows' mean in planner output and what signals you would look for to decide whether to add an index.
MediumTechnical
58 practiced
Explain index selectivity and cardinality and how they affect index design. Given two columns status (low-cardinality: 4 distinct values) and user_id (high-cardinality), recommend ordering for a composite index used in queries that filter both columns and explain why.
EasyTechnical
50 practiced
Explain how missing indexes can cause slow queries. Given query SELECT SUM(total) FROM orders WHERE customer_id = 123 AND order_date >= '2024-01-01'; propose an index to improve performance and explain why column order matters for composite indexes. When might separate indexes be preferred?
HardTechnical
46 practiced
Hash join in Postgres spills to disk for your large join. How do you diagnose and tune work_mem to avoid spills, and how do you estimate a safe work_mem given table sizes and expected concurrency? Discuss the trade-offs of increasing per-session memory versus alternative plan changes.
MediumTechnical
61 practiced
Given this query: SELECT id, name FROM users WHERE country = 'US' AND created_at >= '2024-01-01' ORDER BY created_at DESC LIMIT 50; which composite index would you create and why? Discuss column order, selectivity, and how the planner uses the index for this pattern.
Unlock Full Question Bank
Get access to hundreds of SQL Performance and Anti Patterns interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.