Why a project_id index fails 'latest 20 issues' queries on large projects in PostgreSQL
A developer found that a common 'newest 20 issues per project' query scanned about 200,000 rows for their largest project even with a project_id index. Benchmarks on PostgreSQL 16 with 2 million issues showed the plain index touching 1,941 buffers on the big project versus 23 with a composite index. The problem is that the index finds the project's rows but cannot provide date order, forcing a full fetch and sort that scales with project size. A composite index on (project_id, created_at DESC, id DESC) lets Postgres read the newest rows directly and stop after 20. The author also advises building the index with CONCURRENTLY and benchmarking against the largest tenant.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in