perf(discovery): batch suggestSimilarInstances thumbnail resolution (ISSUE-140) #1146

Merged
chris merged 1 commit from fix/issue-140-suggest-thumb-latency into main 2026-10-12 16:33:49 +02:00
Owner

Summary

PR#1144 introduced a latency regression in the media-viewer "Assign person" dropdown: REPRESENTATIVE_FACE_THUMBNAIL_SUBQUERY was interpolated into the embedding-path candidate-rows statement of suggestSimilarInstances, where the correlated subquery executed once per candidate embedding row (~4,675 executions for ~350 persons — live prod measurement: 39.3 s per dropdown open; before #1144: 75 ms).

Change

Thumbnails now resolve in one batched query after the scoring loop (step 2b):

  • chunks of RANKING_IN_CHUNK_SIZE (1000) scored instance ids — 1 statement for ~350 persons
  • same statement shape and scoping as personRanking's personRows query (~113 ms for the full universe)
  • entries built with null thumbnail in the loop, rebuilt with the resolved map before step 3 filter/sort
  • the two LIMIT-bounded fallback statements keep their inline subqueries (name search, blank-query listing)

Display-only: scores, ordering, filters, occurrence counts and row sets unchanged. Expected endpoint latency ~200 ms (from 39.3 s).

Validation

  • All six #1144 regression tests pass unchanged — the four embedding-path ones now exercise the batched resolution (named-only-tentative → null, unnamed keeps tentative, accepted beats newer tentative, pin wins)
  • ClassInstanceDiscoveryServiceTest: 326/326
  • Live A/B on prod data (read-only psql): old form 75 ms / #1144 form 39,256 ms / new = candidate query (no subquery) + batch ≈ 190 ms

Fixes the regression from #1144 (ISSUE-140 in KNOWN_ISSUES.md follows).

## Summary PR#1144 introduced a latency regression in the media-viewer "Assign person" dropdown: `REPRESENTATIVE_FACE_THUMBNAIL_SUBQUERY` was interpolated into the embedding-path candidate-rows statement of `suggestSimilarInstances`, where the correlated subquery executed once per candidate embedding **row** (~4,675 executions for ~350 persons — live prod measurement: **39.3 s** per dropdown open; before #1144: 75 ms). ## Change Thumbnails now resolve in **one batched query after the scoring loop** (step 2b): - chunks of `RANKING_IN_CHUNK_SIZE` (1000) scored instance ids — 1 statement for ~350 persons - same statement shape and scoping as `personRanking`'s personRows query (~113 ms for the full universe) - entries built with null thumbnail in the loop, rebuilt with the resolved map before step 3 filter/sort - the two LIMIT-bounded fallback statements keep their inline subqueries (name search, blank-query listing) Display-only: scores, ordering, filters, occurrence counts and row sets unchanged. Expected endpoint latency ~200 ms (from 39.3 s). ## Validation - All six #1144 regression tests pass **unchanged** — the four embedding-path ones now exercise the batched resolution (named-only-tentative → null, unnamed keeps tentative, accepted beats newer tentative, pin wins) - `ClassInstanceDiscoveryServiceTest`: 326/326 - Live A/B on prod data (read-only psql): old form 75 ms / #1144 form 39,256 ms / new = candidate query (no subquery) + batch ≈ 190 ms Fixes the regression from #1144 (ISSUE-140 in KNOWN_ISSUES.md follows).
perf(discovery): batch suggestSimilarInstances thumbnail resolution (ISSUE-140)
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 10m19s
e1799e2b1d
PR#1144 interpolated REPRESENTATIVE_FACE_THUMBNAIL_SUBQUERY into the
embedding-path candidate-rows statement of suggestSimilarInstances, where
the correlated subquery executed once per candidate embedding ROW
(~4,675 executions for ~350 persons). Live measurement on prod data:
39.3 s per dropdown open (before #1144: 75 ms).

The thumbnail now resolves in ONE batched query after the scoring loop
(step 2b): chunks of RANKING_IN_CHUNK_SIZE (1000) scored instance ids,
same statement shape and scoping as the personRanking personRows query,
so the correlated subquery runs once per DISTINCT person (~113 ms for
the full universe) instead of once per embedding row. Entries are built
with a null thumbnail in the loop and rebuilt with the resolved map
before step 3 filter/sort. The two LIMIT-bounded fallback statements
(name search, blank-query listing) keep their inline subqueries.

Display-only restructuring: scores, ordering, filters, occurrence
counts and row sets unchanged. All six #1144 regression tests pass
unchanged (the four embedding-path ones now exercise the batched
resolution); ClassInstanceDiscoveryServiceTest 326/326.
chris merged commit d5c373e32f into main 2026-10-12 16:33:49 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
chris/memories!1146
No description provided.