Skip to content
RED SAGSkills
easyFull Stack Developer interview question

How would you explain a database index to a junior developer, and how do you decide which columns to index?

What the interviewer wants to hear

  • An index is a sorted lookup structure, like a book index
  • It speeds up filters, joins and sorting on indexed columns
  • It costs disk space and slows writes
  • Choose columns from real slow queries, checked with EXPLAIN
  • Foreign keys and frequently filtered columns are common candidates

Model answer

I compare it to the index at the back of a book. Without it you read every page to find a topic, which is a full table scan. With it you jump straight to the right pages. Most database indexes are B-trees: sorted copies of one or more columns with pointers to the rows, so lookups, range filters, joins and ORDER BY on those columns become fast. The cost is that every insert, update and delete must also update each index, and indexes take disk space, so you do not index everything. I choose indexes from real queries: I find slow ones in logs or monitoring, run EXPLAIN ANALYZE, and look for sequential scans on large tables. Foreign keys used in joins and columns used in WHERE clauses on busy pages, like user_id on orders, are usually the first candidates.

Can you answer it under time pressure?

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

Start mock interview

More easy Full Stack Developer questions