Skip to content
RED SAGSkills
easyBackend Developer interview question

What is a database index, and what are its costs? How do you decide which columns to index?

What the interviewer wants to hear

  • Index is a data structure (usually B-tree) for fast lookups
  • Speeds up WHERE, JOIN and ORDER BY on indexed columns
  • Costs: extra storage and slower inserts and updates
  • Index based on real query patterns and selectivity
  • Use EXPLAIN to verify the index is used

Model answer

An index is a separate data structure, usually a B-tree, that keeps column values sorted with pointers to rows. Instead of a full table scan, the database can seek directly to matching rows, which makes WHERE filters, joins and ORDER BY much faster. The costs are extra disk and memory, and slower writes, because every INSERT, UPDATE and DELETE must also update each index. So I do not index every column. I look at the slow and frequent queries, check them with EXPLAIN or EXPLAIN ANALYZE, and index columns used in filters and joins, especially foreign keys. Columns with high selectivity, like email, benefit most. For queries that filter on several columns I use a composite index with the most commonly filtered column first. Then I confirm with EXPLAIN that the plan really uses the index.

Can you answer it under time pressure?

Take a timed easy round and get scored on every answer.

Start mock interview

More easy Backend Developer questions