Skip to content

perf: optimize enrichable members selection query#4389

Merged
skwowet merged 5 commits into
mainfrom
perf/enrichable-members-query
Jul 23, 2026
Merged

perf: optimize enrichable members selection query#4389
skwowet merged 5 commits into
mainfrom
perf/enrichable-members-query

Conversation

@skwowet

@skwowet skwowet commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

fetchMembersForEnrichment was selecting enrichable members by scanning a huge identity join, aggregating, then sorting — so LIMIT 500 only applied at the end and the query regularly ran for tens of minutes.

Changes

  • Rewrite the query to walk membersGlobalActivityCount high → low, apply the same enrichability / cache / bot filters, take LIMIT first, then json_agg identities for those candidates only
  • Keep the same result shape and enrichable identity filtering used by the enrichment worker
  • Express cache freshness as updatedAt > now() - make_interval(...) (same meaning as the old epoch check)

Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
@skwowet skwowet self-assigned this Jul 23, 2026
Copilot AI review requested due to automatic review settings July 23, 2026 10:13
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Jira Issue Key Missing

Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.

Example:

  • feat: add user authentication (CM-123)
  • feat: add user authentication (IN-123)

Projects:

  • CM: Community Data Platform
  • IN: Insights

Please add a Jira issue key to your PR title.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes member-enrichment selection by limiting ranked candidates before aggregating identities.

Changes:

  • Uses an activity-ranked candidate CTE.
  • Replaces epoch cache checks with make_interval.
  • Preserves identity aggregation after candidate selection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings July 23, 2026 10:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

services/libs/data-access-layer/src/old/apps/members_enrichment_worker/index.ts:162

  • This inner join changes the eligible population. membersGlobalActivityCount only contains members represented in memberSegmentsAgg, while the currently enabled ProgAI and LinkedIn-scraper filters do not require activity. The previous LEFT JOIN returned those members with an activity count of 0; now they can never be selected for enrichment. Preserve a fallback branch for enrichable members absent from the materialized view (after the indexed activity-ranked branch) so the optimization does not silently drop them.
      FROM "membersGlobalActivityCount"
           INNER JOIN members ON members.id = "membersGlobalActivityCount"."memberId"

services/libs/data-access-layer/src/old/apps/members_enrichment_worker/index.ts:198

  • enrichableBySql is documented to allow references to the members alias, and the SERP source does so. This outer query only defines c, mi, and membersGlobalActivityCount, so using that valid source makes PostgreSQL fail with missing FROM-clause entry for table "members" when this predicate is reapplied. Join the base member row for the limited candidate set before evaluating the identity filter.
    WHERE ${enrichableBySqlJoined}

skwowet added 2 commits July 23, 2026 15:52
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
…ation/crowd.dev into perf/enrichable-members-query
Copilot AI review requested due to automatic review settings July 23, 2026 10:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 23, 2026 10:29
@skwowet
skwowet merged commit 370e3f0 into main Jul 23, 2026
13 checks passed
@skwowet
skwowet deleted the perf/enrichable-members-query branch July 23, 2026 10:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants