Skip to content

perf(entities): a scoped name search or detail reads only the entities it asks for (#6330) - #6410

Merged
nicoloboschi merged 1 commit into
mainfrom
triage/6330
Nov 6, 2026
Merged

nicoloboschi merged 1 commit into
mainfrom
triage/6330

Conversation

@nicoloboschi

@nicoloboschi nicoloboschi commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #6330. Thanks to @harshitgupta31415 for finding this and measuring it.

Problem

With a tag filter on, searching entities by name counted mentions for every entity visible in the scope, then kept only the ones whose name matched. It did that twice (once for the total, once for the page). Opening one entity's detail page did the same and kept one row.

Fix

The shared "visible mentions" query (used by list, detail and graph) now accepts the entities a read wants:

  • Few mentions (≤ 5000): it reads only those entities' mentions. It finds observations that inherit them with one GIN && lookup on source_memory_ids, instead of unnesting every visible observation.
  • Past the cap: it runs the unchanged query. Walking from mentions loses badly on a broad search (a search matching 20k entities ran for minutes that way).

A one-time check picks the branch, so only one of them runs. The visibility rules stay in one place.

Not taken from the issue's patch: it copied the visibility SQL into a second query (drift risk between list, detail and graph), only handled exactly one match with ≤ 64 mentions, and was Postgres only.

Numbers

300k memories (150k observations), 20k entities, one tag scope, median of 5, main and fix alternated on the same database:

Case main fix
Search, one name (20 mentions) 585 ms 58 ms 10×
Search, prefix matching 100 names 659 ms 76 ms 8.7×
Detail, entity with 20 mentions 68 ms 4 ms 18×
Search, one name with 10k mentions (past cap) 645 ms 692 ms same
Search, broad, all 20k names (past cap) 672 ms 744 ms same
Detail, entity with 10k mentions (past cap) 133 ms 134 ms same

Same rows and counts in every case.

Tests

test_observations_lend_their_sources_entities_under_a_filter now runs name search and detail through both branches (normal cap, and cap forced to 0). Its observation's two sources both name the same entity, so it also checks duplicates are removed. It fails if the narrowed branch returns nothing. oracle-tests label added for the Oracle path.

…s it asks for (#6330)

Under a tag filter, the entity list recomputed mention counts for every visible
entity in the bank before applying the name search (twice: total and page), and
the detail view did the same to keep one entity.

The shared visible-mentions query now takes the entity ids a read wants. While
they have at most FEW_POSTINGS (5000) postings it reads only their mentions,
finding inheriting observations with one GIN && probe on source_memory_ids
instead of unnesting every visible observation. Past the cap it runs the
unchanged query, because walking from postings loses badly on a broad search.
The visibility rule stays in one place for list, detail and graph.

300k memories, 20k entities, one tag scope, median of 5, main vs fix:
search one name 585 -> 58 ms, prefix of 100 names 659 -> 76 ms,
detail of a small entity 68 -> 4 ms; broad searches and entities past the cap
unchanged. Same rows and counts in every case.
@nicoloboschi nicoloboschi added the oracle-tests CI: run Oracle 23ai tests on this PR label Nov 6, 2026
@nicoloboschi nicoloboschi reopened this Nov 6, 2026
@nicoloboschi
nicoloboschi merged commit eb963cb into main Nov 6, 2026
284 of 336 checks passed
@nicoloboschi
nicoloboschi deleted the triage/6330 branch November 6, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

oracle-tests CI: run Oracle 23ai tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

filter entity names before counting mentions (8.51 s → 26.83 ms, 317.07× faster)

1 participant