Files
outline/server/services
Tom MoorandClaude Fable 5 0c2374dec1 perf: Index-friendly popularity score scan, spread runs over an hour (#13306)
* perf: Index-friendly candidate scan in popularity score task

The chunked scan that populates the working table combined "has a non-zero
score" and "has recent insights" into a single OR, which forced a scan of
every document in the partition with a correlated subquery per row. In
production this reached a p95 of 16.9s per chunk on the read replica, which
also serves user reads.

Query each source separately so both can use an index, then union. Limiting
each branch and then the union still yields the globally lowest ids, so keyset
pagination is unchanged. Validity is now a join rather than a predicate, with
the cursor tracked over candidates so filtered rows can't end the walk early.

Adds a partial index on documents(id) where the score is non-zero; the full
index on popularityScore was dropped as unused in 20251217161051 and would not
help this access pattern.

* perf: Drop per-batch COUNT(*) from popularity score task

Every batch issued a COUNT(*) over the working table purely to decide whether
to continue, doubling the round-trips for a loop that already runs thousands of
iterations. processBatch already reports how many rows it handled, so a short
batch is sufficient to detect a drained table.

setupWorkingTable now returns the row count it inserted, which removes the
remaining call site.

* perf: Spread popularity scoring across teams over six hours

The task partitioned by document id over a 30 minute window, which the cron
route expands to one partition per minute. Thirty partitions each took minutes
to run, so they piled up and saturated the database for the whole window.

Partition by team over six hours instead: 360 slices, one starting each minute,
each covering a fraction of the team space. Peak concurrency is bounded by how
long a slice takes rather than by the number of partitions.

Spreading past the hour breaks the interval gate, which read the wall clock at
execution time and so would skip every partition that ran in a later hour --
and worse, would let the next hour's cron schedule a second full run. Cron now
records when a run was scheduled and the gate reads that instead.

The partial index added for the score walk leads with teamId to match the new
access pattern; it is not deployed yet, so it is amended in place.

* perf: Re-key popularity partitions to document id, reduce spread to one hour

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: Compute scheduledAt once per cron run

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-05 07:12:11 -04:00
..