KatInterview

Database Indexes: Read/Write Trade-offs and Query Tuning

System DesignModel answer
MediumModel answerSystem Designdatabasessqlperformance

Last updated

Explain how relational indexes work, why they help reads but hurt writes, and walk through a systematic process for diagnosing and fixing a slow SQL query.

Database Indexes and Query Performance Tuning

Relational databases are central to most production systems, and understanding how they store and retrieve data efficiently is a core engineering skill. This question tests two related ideas: how indexes work under the hood, and how to methodically diagnose a query that is running far too slowly.

Where a concrete example helps, assume a table like:

trades(trade_id, product, trade_time, quantity, price)

Before You Answer

Consider what you would want to clarify in a real setting:

  • Which database engine are we talking about (PostgreSQL, MySQL, SQL Server, a columnar warehouse like Redshift)? Tooling and index types vary.
  • Is the slow query a transactional point-lookup or an analytical scan over millions of rows?
  • Did it get slow recently, or has it always been this way?
  • Can the schema and indexes be changed, or is the query text the only lever?

Part 1 — How Indexes Work

Answer the following questions about relational indexes:

  1. What is an index, and how is it physically structured?
  2. Why does having an index make certain queries faster?
  3. Why would you not put an index on every column?
  4. What is the impact of indexes on write operations?

Your answer should cover the data structure a typical index uses, the kinds of queries it helps (and those it does not), and the concrete costs — storage, write overhead, maintenance — that make blanket indexing a bad idea.


Part 2 — Diagnosing a Slow Query

You inherit a complex SQL query that runs in production and takes an unacceptably long time. Walk through how you would troubleshoot and tune it.

Your answer should describe:

  • Where you start and what information you gather first
  • What the execution plan tells you and how to read it
  • The most common root causes and a concrete fix for each
  • How you know whether your change actually helped
  • What risks to watch for when applying a fix in production

Hints

Hint 1

Start with what a B-tree looks like on disk — an index is essentially a sorted copy of one or more columns with pointers back to the full rows. That structure explains both why range scans are fast and why updates are expensive.

Hint 2

For Part 2, resist the urge to list tips. Instead, describe a process: measure first (EXPLAIN ANALYZE), form a hypothesis about the bottleneck, make one change, measure again. The order matters.

Hint 3

Think about the asymmetry between reads and writes: an index turns an O(n) table scan into an O(log n) lookup for reads, but every INSERT, UPDATE, or DELETE now has to update the index tree as well as the table — that cost multiplies with the number of indexes.