Add partial index on material(is_active) for the catalogue read

The modelling_e2e Lambda reads the active catalogue once per invocation
(SELECT * FROM material WHERE is_active ORDER BY id). Without an index this
was a full seq scan every time (~23ms), paid by all 32 concurrent Lambdas at
startup. Partial index on id WHERE is_active returns the active rows already
id-ordered — no sort, no filter.

Verified on dev: seq scan 23.2ms -> index scan 4.2ms.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jun-te Kim 2026-06-29 11:43:13 +00:00
parent 4ae4487055
commit f51fdca3d8
4 changed files with 11671 additions and 2 deletions

View file

@ -0,0 +1 @@
CREATE INDEX "idx_material_active" ON "material" USING btree ("id") WHERE "material"."is_active";

File diff suppressed because it is too large Load diff

View file

@ -1723,6 +1723,13 @@
"when": 1782498864047,
"tag": "0246_minor_lady_bullseye",
"breakpoints": true
},
{
"idx": 247,
"version": "7",
"when": 1782730761916,
"tag": "0247_glorious_darkhawk",
"breakpoints": true
}
]
}

View file

@ -1,4 +1,4 @@
import { InferModel } from "drizzle-orm";
import { InferModel, sql } from "drizzle-orm";
import {
bigserial,
text,
@ -8,6 +8,7 @@ import {
pgEnum,
json,
boolean,
index,
} from "drizzle-orm/pg-core";
export const MaterialType: [string, ...string[]] = [
@ -125,6 +126,12 @@ export const material = pgTable("material", {
includesScaffolding: boolean("includes_scaffolding").default(false),
includesBattery: boolean("includes_battery").default(false),
batterySize: real("battery_size"),
});
}, (table) => [
// The modelling_e2e Lambda reads the active catalogue once per invocation
// (WHERE is_active ORDER BY id). Without this index that is a full seq scan
// every time (~23ms, ×32 concurrent at startup). Partial on id so the active
// rows come back already id-ordered — no sort, no filter.
index("idx_material_active").on(table.id).where(sql`${table.isActive}`),
]);
export type Material = InferModel<typeof material, "select">;