Database Indexes and Query Performance
Indexes are the first answer to "this query is slow". An index is a sorted structure, usually a B-tree, that lets the database find rows without scanning the whole table. Interviewers want to know how they work, what they cost and how to read a query plan.
What you will learn
- Explain how a B-tree index speeds up lookups and ranges
- Design composite indexes with the leftmost-prefix rule
- Read EXPLAIN ANALYZE output
- Know the costs of indexes on writes
Key concepts
B-tree index
A balanced tree of sorted keys pointing to rows. Lookups, ranges (BETWEEN, <, >) and ORDER BY on the indexed columns become O(log n) instead of a full scan.
Composite index
An index on (a, b, c) works for filters on a, a+b, or a+b+c, but not on b alone (leftmost prefix). Put equality columns first, then range or sort columns.
Covering index
An index that contains every column the query needs, so the database never reads the table (an index-only scan). In PostgreSQL use INCLUDE (col).
Selectivity
How many rows a value narrows down to. An index on email (unique) is very selective; an index on a boolean is usually useless.
Step by step
Fixing a slow query
EXPLAIN ANALYZE
SELECT id, total FROM orders WHERE user_id = 42 AND status = 'paid' ORDER BY created_at DESC LIMIT 20;
-- Seq Scan on orders (rows=2,000,000) ... 850 ms ← reads everything
CREATE INDEX CONCURRENTLY idx_orders_user_status_created
ON orders (user_id, status, created_at DESC);
-- Index Scan using idx_orders_user_status_created ... 0.4 msWhy an index is not used
- A function on the column: WHERE lower(email) = ... (use an expression index)
- Leading wildcard: LIKE '%abc'
- Type mismatch or implicit cast
- The query returns a large share of the table, so a scan is cheaper
- Stale statistics (run ANALYZE)
Examples
Partial and expression indexes
-- only index the rows you query often
CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending';
-- case-insensitive email lookup
CREATE UNIQUE INDEX idx_users_email_lower ON users (lower(email));Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Choose indexes for: login by email, a user's latest 20 orders, admin search by status and date range.
- Explain why adding 10 indexes to a write-heavy table is a bad idea.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
What is the cost of an index?
Every INSERT, UPDATE of an indexed column and DELETE must also update the index, so writes get slower, and indexes use disk and memory. Add indexes for real query patterns, and drop unused ones.
Clustered vs non-clustered index?
A clustered index stores the table rows in index order (InnoDB's primary key); there can be only one. A non-clustered (secondary) index is a separate structure pointing to rows. PostgreSQL tables are heaps, and every index is secondary.
Why use CREATE INDEX CONCURRENTLY?
A normal CREATE INDEX blocks writes to the table while it builds. CONCURRENTLY builds without blocking writes, which is what you want on a live production table.
Summary
B-tree indexes turn scans into fast lookups; design composite indexes for your exact filters and sort, verify with EXPLAIN ANALYZE, and remember every index slows writes.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus