Skip to main content

Diagnose and fix a slow multi-filter + sort query

A query filtering WHERE workspace_id = $1 AND status = 'open' ORDER BY updated_at DESC LIMIT 25 takes 900ms. Walk through fixing it.

Official Reference

Model Answer & Explanation

Run EXPLAIN (ANALYZE, BUFFERS). Likely it’s scanning an index on workspace_id only, filtering out closed rows, and sorting all matches in memory/disk. Create a partial composite index: CREATE INDEX CONCURRENTLY idx_open_ws ON tickets (workspace_id, updated_at DESC) WHERE status = 'open'. Re-run EXPLAIN to verify Index Scan Backward/Forward with LIMIT 25 and tiny buffer reads.

Follow-up Interview Question · What if the user can also filter by arbitrary combinations of 5 optional columns?

Module Context & References

PostgreSQL & Redis · Practice SQL & schema drills The classic SQL queries, locking patterns and optimization problems tested repeatedly
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.