SQL · Queries that survive scale

What is an index and what does it cost?

MediumindexesbtreeperformanceUpdated Oct 2026
Quiz this track

Short answer

An index is a separate data structure that speeds up lookups and ordering at the cost of extra storage and slower writes. Choose indexes based on real query patterns, not on every column.

Most relational indexes are B-trees. They support equality, range, prefix, and ordered scans efficiently. A query can use an index to find a small set of rows without scanning the whole table, but the planner may still choose a sequential scan when the index would touch too much of the table.

sql
CREATE INDEX idx_orders_customer_created
  ON orders (customer_id, created_at DESC);

SELECT id, total_cents
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 10;
The index supports both the equality filter and the requested ordering.

The trade-offs

  • Reads improve when the predicate is selective and the index is used
  • Every insert, update, and delete must maintain the index
  • Indexes consume disk and memory, which can evict useful pages
  • Covering indexes can satisfy a query from the index alone
  • Low-selectivity columns such as a boolean often do not benefit from a plain index
  • Unused indexes still cost writes, so audit them

Index the columns used for joins, selective filters, and required sort orders. For write-heavy tables, keep the number of indexes small and validate each one against query plans and actual usage statistics.

An index is not a guarantee. A function applied to an indexed column, a type mismatch, or a leading wildcard such as LIKE '%term' can prevent normal index use.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why can an index make writes slower?
  2. 2When would the planner ignore an index?
  3. 3What is a covering index?
  4. 4Would you index a boolean column?

Say it in the interview

An index is a separate structure, usually a B-tree, that speeds up selective lookups, ranges, and ordered reads. The cost is storage and write maintenance, because every change must update the index. I create indexes from real query patterns and check the plans. I avoid indexing low-selectivity columns unless a partial or composite index makes them useful.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live SQL mock interview and get scored feedback on depth, clarity and confidence.