
Understand it
A selective B-tree index can reduce work by narrowing the scanned range. Leading-column predicates are often especially useful, but they are not a universal eligibility rule: PostgreSQL can use later-column constraints and, since version 18, skip scans when its planner estimates that they are worthwhile. Every maintained index also adds storage and write work; measure those costs rather than declaring a table slow from its index count alone.
An index is another data structure maintained alongside table rows so some queries can avoid examining every row. A B-tree-family index commonly supports equality, range scans, and ordered traversal. For a composite index on (account_id, created_at), queries restricted by account can often use the leading part, and a query for one account ordered by time can benefit from the second part. A query only on created_at may not use the index efficiently, though optimizers and specialized access paths vary by database.
Indexes cost storage and write work. Each insert or update that changes an indexed field may need to modify the index. An index on a low-selectivity column can be less useful than it looks, and a query planner may choose a scan for a small table. Do not add an index solely because a WHERE clause exists. Inspect the actual query plan and measure it with representative row counts and distributions.
For an interview, start from a concrete query and data shape. Explain which predicate narrows the search, whether the requested columns are covered, and whether an ordered scan avoids a separate sort. Then state the cost to writes and memory. Test after adding the index: plans can change as statistics and data grow. The leftmost-prefix heuristic is useful, but a real planner makes the final decision.
A covering index may avoid fetching full table rows when it contains every value a query needs. That can be faster for a frequent narrow query but makes the index larger and more expensive to maintain. Discuss cardinality and selectivity with real data: an index useful on a huge selective table may do little on a tiny table.
Interview follow-up: How do row count, visibility, table access and write overhead affect the decision to add another index?
Read the code
-- PostgreSQL SQL; the events table already exists.
CREATE INDEX events_tenant_kind_at ON events (tenant_id, kind, at);
EXPLAIN SELECT tenant_id, kind, at
FROM events
WHERE kind = 'click' AND at > TIMESTAMP '2026-10-01';Inspect EXPLAIN with representative data. An absent leading-column filter does not universally prohibit an index scan.
Practice starter
-- Index: (tenant_id, kind, at).
EXPLAIN SELECT * FROM events WHERE tenant_id = 42 AND kind = 'click';
EXPLAIN SELECT * FROM events WHERE kind = 'click';Try it
Use EXPLAIN on both a leading-column query and a later-column query in the named database. Compare selectivity, data distribution and actual plans instead of predicting a universal yes or no.
What success looks like: You can compare the plans for leading-column and later-column filters, explain selectivity, and name the index maintenance cost.
Check your understanding
Why should these two PostgreSQL queries be compared with EXPLAIN and representative data?
Reveal the answer
- A non-leading predicate makes every composite B-tree index unusable.
- Predicate selectivity, statistics and available scan strategies determine the chosen plan.
- The existence of any matching column forces a full index-only scan.
Leading columns often improve selectivity, but eligibility, skip-scan possibilities and the cheapest plan are different questions.
Go deeper
Common misconception: The existence of any matching column forces a full index-only scan.
A matching column only makes an index a candidate. The planner compares estimated costs and may choose another scan, and an index-only scan also needs every selected column in the index.
- Identify the contract: An index is a sorted structure that trades write cost for read speed.
- Trace the concrete example: Inspect EXPLAIN with representative data. An absent leading-column filter does not universally prohibit an index scan.
- Test the changed constraint: How do row count, visibility, table access and write overhead affect the decision to add another index?
Transfer challenge: How do row count, visibility, table access and write overhead affect the decision to add another index?
Connections
- Transactions and Isolation: Which anomaly each level allows
- CAP in Practice: What happens during a partition
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Indexes and B-trees (courseId "interview", lessonId "q-indexes") Link: https://crabrix.com/learn/interview/q-indexes/ Goal: Leading columns often make a composite B-tree scan selective, but later-column predicates can still use an index, including through skip scan. The actual choice depends on the database, statistics and query plan. Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.