Index chunk.claim_id and chunk.document_id

enrich.embed looks up claims without a chunk on every batch. The existing
unique index is on COALESCE expressions, which that lookup can't use, so
after the history backfill each run took hours and starved the other
enrichers on the same queue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
simonandClaude Opus 5.5 committed 2026-11-14 11:28:25 +01:00
1 parent ccf1a4c755
commit d03b9643e9
1 file changed
+6
@@ -0,0 +1,6 @@
-- enrich.embed looks up claims and documents without a chunk on every batch. chunk_uniq is an expression index
-- (COALESCE(...)), which those lookups can't use, so each batch scanned the whole table: after the history
-- backfill (over a million claims) a run took hours and blocked the other enrichers. Built CONCURRENTLY by hand on
-- 2026-11-14; IF NOT EXISTS makes this a no-op there.
CREATE INDEX IF NOT EXISTS chunk_claim_idx ON chunk (claim_id) WHERE claim_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS chunk_document_idx ON chunk (document_id) WHERE document_id IS NOT NULL;