perf: stop Tribe list and care summary from scanning the whole contact table (#6024) - #6121
Merged
Conversation
…t table (#6024) Every Tribe list request made PostgreSQL run two correlated COUNT(*) subqueries per person row — 400 extra index scans for a 200-contact install — to produce touchpointCount/linkedMemoryCount that no view in the client ever renders. listPeople() now selects the person rows only; rowToPerson already defaults both counts to 0, and the person detail query (getPerson) still hydrates them for the views that show them. getCareSummary() went through that same list to answer "who is overdue", hydrating notes, next move, tags, emails and phones for the entire table before throwing all of it away in JavaScript. It now issues its own lean query for the six columns the cadence math and the response actually use, and excludes `external` members in SQL. That path runs on every Dashboard mount and on the proactive-alerts poll every two minutes, so it was the most frequently paid cost of the two. Response shape and ordering of GET /api/tribe/people and GET /api/tribe/care are unchanged. Claude-Session: https://claude.ai/code/session_01VjkWVTfzKyRuAv3HEsspwN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two Tribe read paths were paying for data nothing consumed.
listPeople()ran two correlatedCOUNT(*)subqueries per person row — one overtribe_touchpoints, one overtribe_memory_links— so a 200-contact install did 400 extra index scans on every Tribe page load, every identity-resolution context load, and every contact-sync pass. NeithertouchpointCountnorlinkedMemoryCountis read anywhere inclient/src/. The subqueries are gone;rowToPersonalready defaults both to0when the columns are absent, andgetPerson(id)still hydrates them for the detail view.getCareSummary(limit)answered "who is overdue" by callinglistPeople()with no filter — hydratingnotes,next_move, and thetags/emails/phonesarrays for every contact (plus those 2N subqueries) only to discard all of it in JavaScript. It now issues its own lean query for the six columns the cadence math and the response actually use, with theexternalexclusion pushed into SQL:ringisNOT NULL DEFAULT 'tribe', so moving the exclusion into SQL matches the previous JS filter exactly. This is the hotter of the two paths: it runs on every Dashboard mount and on the proactive-alerts poll every two minutes.Response shape and ordering of
GET /api/tribe/peopleandGET /api/tribe/careare unchanged.Test plan
cd server && npm test— full suite green (1917 files, 38679 tests).server/services/tribe.test.jsextended:listPeopleasserts noCOUNT(*)/tribe_touchpoints/tribe_memory_linksin the SQL, that the ring-rank → oldest-contact → name ordering is intact, and that rows still map to full person objects with both counts defaulted to0.getPersonasserts the counts are still hydrated from its own subqueries.getCareSummaryasserts the lean column projection plus thering <> 'external'/deleted = FALSEpredicate, keeps the missing-first → most-overdue ordering case, pins the exact key set of eachoverdueitem (id, name, ring, channel, lastContact, state, daysOverdue), and covers aDate-typedlast_contact_onrendering to an ISO date string.*.db.test.jscovers Tribe, so nothing DB-backed needed to run.Closes #6024
https://claude.ai/code/session_01VjkWVTfzKyRuAv3HEsspwN