From 93eaec8d2757ee6f04563d216ed40d3c9f672cb4 Mon Sep 17 00:00:00 2001 From: Daan Meijer Date: Sun, 11 Oct 2026 19:39:42 +0200 Subject: [PATCH] Rebuild: sort indexes that match newest-first The default page (no search words) took 23-27 s: SearchService sorts posted_at DESC NULLS LAST, id DESC, the indexes were on posted_at DESC (nulls first), so Postgres sorted all 68M releases. New indexes match the sort (also per category), built concurrently; the old ones dropped. Co-Authored-By: Claude Opus 5.5 --- .../1770000002000_releases_sort_indexes.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 database/migrations/1770000002000_releases_sort_indexes.ts diff --git a/database/migrations/1770000002000_releases_sort_indexes.ts b/database/migrations/1770000002000_releases_sort_indexes.ts new file mode 100644 index 0000000..edb987c --- /dev/null +++ b/database/migrations/1770000002000_releases_sort_indexes.ts @@ -0,0 +1,25 @@ +import { BaseSchema } from '@adonisjs/lucid/schema' + +/** + * Indexes that match the newest-first sort (SearchService: posted_at DESC + * NULLS LAST, id DESC). The old ones (posted_at DESC: nulls first) didn't, + * so the default page sorted all 68M releases: 23 s. Built concurrently, the + * old ones dropped after. + */ +export default class extends BaseSchema { + static disableTransactions = true + + async up() { + this.schema.raw(`CREATE INDEX CONCURRENTLY IF NOT EXISTS releases_newest_idx ON releases (posted_at DESC NULLS LAST, id DESC)`) + this.schema.raw(`CREATE INDEX CONCURRENTLY IF NOT EXISTS releases_category_newest_idx ON releases (category, posted_at DESC NULLS LAST, id DESC)`) + this.schema.raw(`DROP INDEX CONCURRENTLY IF EXISTS releases_posted_at_idx`) + this.schema.raw(`DROP INDEX CONCURRENTLY IF EXISTS releases_category_posted_idx`) + } + + async down() { + this.schema.raw(`CREATE INDEX CONCURRENTLY IF NOT EXISTS releases_posted_at_idx ON releases (posted_at DESC)`) + this.schema.raw(`CREATE INDEX CONCURRENTLY IF NOT EXISTS releases_category_posted_idx ON releases (category, posted_at DESC)`) + this.schema.raw(`DROP INDEX CONCURRENTLY IF EXISTS releases_newest_idx`) + this.schema.raw(`DROP INDEX CONCURRENTLY IF EXISTS releases_category_newest_idx`) + } +}