Interview Databases & Caching

What is an index, and what are the trade-offs of adding one?

Databases & Caching · Basic level

Answer

An index is a structure that lets the database find rows without scanning the full table. It improves selected reads, joins, ordering, and uniqueness checks, but it costs storage and slows writes because the index must be maintained.

Technical explanation

Use real workload evidence from pg_stat_statements, slow logs, Performance Insights, or traces before adding indexes.

EXPLAIN shows the plan; EXPLAIN ANALYZE runs the query and compares estimated versus actual rows and timing.

Sequential scans are not always bad; for small tables or low-selectivity filters they may be optimal.

Hands-on example

Index tuning example:

EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = $1 ORDER BY created_at DESC LIMIT 20;

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

Re-run EXPLAIN and confirm lower execution time, fewer buffers read, and no large sort. For covering reads, add INCLUDE columns where appropriate.

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Databases & Caching interview questions

← All Databases & Caching questions