-
Notifications
You must be signed in to change notification settings - Fork 726
fix: optimizations for query members #3721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -309,49 +309,75 @@ const buildActivityCountOptimizedQuery = ({ | |
| const ctes: string[] = [] | ||
| if (searchConfig.cte) ctes.push(searchConfig.cte.trim()) | ||
|
|
||
| const searchJoinForTopMembers = searchConfig.cte | ||
| // We must keep msa available in the outer SELECT if "fields" references msa.* | ||
| const needsMsaInOuterSelect = /\bmsa\./.test(fields) | ||
| const filterNeedsMsa = /\bmsa\./.test(filterString) | ||
|
|
||
| const searchJoinForFiltering = searchConfig.cte | ||
| ? `\n INNER JOIN member_search ms ON ms."memberId" = msa."memberId"` | ||
| : '' | ||
|
|
||
| const baseNeeded = limit + offset | ||
| const oversampleMultiplier = hasNonIdMemberFields ? 10 : 1 // 10x oversampling for m.* filters | ||
| const totalNeeded = Math.min(baseNeeded * oversampleMultiplier, 50000) // Cap at 50k | ||
|
|
||
| const prefetchLimit = Math.min(totalNeeded * 10, 50000) | ||
|
|
||
| const msaJoinForFiltering = filterNeedsMsa | ||
| ? `\n INNER JOIN "memberSegmentsAgg" msa ON msa."memberId" = m.id AND msa."segmentId" = $(segmentId)` | ||
| : '' | ||
|
|
||
| ctes.push( | ||
| ` | ||
| top_members AS ( | ||
| top_msa AS ( | ||
| SELECT | ||
| msa."memberId", | ||
| msa."activityCount" | ||
| FROM "memberSegmentsAgg" msa | ||
| INNER JOIN members m ON m.id = msa."memberId" | ||
| ${searchJoinForTopMembers} | ||
| WHERE | ||
| msa."segmentId" = $(segmentId) | ||
| AND (${filterString}) | ||
| ORDER BY | ||
| msa."activityCount" ${direction} NULLS LAST | ||
| LIMIT ${prefetchLimit} | ||
| ), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefetch excludes filtered members with low activity countsThe |
||
| top_members AS ( | ||
| SELECT | ||
| t."memberId", | ||
| t."activityCount" | ||
| FROM top_msa t | ||
| INNER JOIN members m ON m.id = t."memberId" | ||
| ${msaJoinForFiltering} | ||
| ${searchJoinForFiltering} | ||
| WHERE | ||
| (${filterString}) | ||
| ORDER BY | ||
| t."activityCount" ${direction} NULLS LAST | ||
| LIMIT ${totalNeeded} | ||
| ) | ||
| `.trim(), | ||
| ) | ||
|
|
||
| const withClause = `WITH ${ctes.join(',\n')}` | ||
|
|
||
| // Outer query is much simpler now - no more filtering needed | ||
| const msaOuterJoin = needsMsaInOuterSelect | ||
| ? ` | ||
| INNER JOIN "memberSegmentsAgg" msa | ||
| ON msa."memberId" = m.id | ||
| AND msa."segmentId" = $(segmentId) | ||
| ` | ||
| : '' | ||
|
|
||
| return ` | ||
| ${withClause} | ||
| SELECT ${fields} | ||
| FROM top_members tm | ||
| JOIN members m | ||
| ON m.id = tm."memberId" | ||
| INNER JOIN "memberSegmentsAgg" msa | ||
| ON msa."memberId" = m.id | ||
ulemons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| AND msa."segmentId" = $(segmentId) | ||
| ${msaOuterJoin} | ||
| LEFT JOIN "memberEnrichments" me | ||
| ON me."memberId" = m.id | ||
| ORDER BY | ||
| msa."activityCount" ${direction} NULLS LAST | ||
| tm."activityCount" ${direction} NULLS LAST | ||
| LIMIT ${limit} | ||
| OFFSET ${offset} | ||
| `.trim() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.