Skip to main content

B-tree composite column order and index-only scans

Given WHERE org_id = $1 AND created_at > $2 ORDER BY created_at DESC, what index order works best and why?

Official Reference

Model Answer & Explanation

Put equality columns first and range/sort columns after: CREATE INDEX ON events (org_id, created_at DESC). Postgres jumps straight to org_id and scans created_at in order without sorting. An index on (created_at, org_id) cannot narrow by org_id efficiently. Add INCLUDE (title) for an Index-Only Scan when the visibility map allows.

Follow-up Interview Question · Why does LIKE '%foo' bypass a normal B-tree index, while LIKE 'foo%' can use one (with text_pattern_ops/C locale)?

Module Context & References

PostgreSQL & Redis · Popular interview theory High-yield indexing, MVCC, query plan and Redis caching questions
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.