Fix list_persons N+1 (the ~4s person-page load) #246
Reference in New Issue
Block a user
Delete Branch "fix-person-list-n-plus-one"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Symptom: opening a person page took 4–5s on an idle server, single user.
Root cause:
list_personslooped over every person callingprivacy.person_visibility(which runs twoget_membership_rolequeries per call) and_attach_primary_name(one name query per person). On the 2,324-person tree in question that is ~7,000 serialized DB round-trips per page load — and the person page fetches the whole person list to build its name-lookup map. Each query is ~1ms, but thousands serialized = seconds.Fix:
_attach_primary_names: one batched names query (person_id IN (...), ordered identically to the single-person query so it selects the same name) instead of one per person.search_persons, the deleted-persons list, andpublic_view_service.list_public_persons.Member-path
list_personsdrops from ~3·N queries to ~3 total. The other tree-wide lists (events/relationships/media/citations) were already flat selects.Regression test asserts
list_personsissues a constant number of queries regardless of person count. Suite 103 passing. Backend-only, no migration.🤖 Generated with Claude Code