Skip to content

Conversation

@ulemons
Copy link
Contributor

@ulemons ulemons commented Nov 7, 2025

Query Optimization — Extracted Builder & Smarter Execution Paths

Summary

  • Refactored queryMembersAdvanced to delegate SQL construction to a new queryBuilder module.
  • Moved all complex query logic (filters, search, ordering, joins) into a dedicated builder for better maintainability and safer optimizations.
  • Introduced specialized execution paths to improve performance for common scenarios.

Key Changes

  • New module exports:
    • buildQuery — main data query supporting multiple optimized paths.
    • buildCountQuery — lightweight query for counting members, without unnecessary joins.
  • Added member_search and member_orgs CTEs only when required.
  • Introduced a clear and safe mapping for ORDER BY fields with sensible defaults.

Execution Paths in buildQuery

1️⃣ Direct ID Path (useDirectIdPath)

  • filterString pins m.id to a single value or a small list (≤ ~100)
  • Queries start directly from memberSegmentsAgg (msa)
  • Minimal joins (members, optional member_orgs / memberEnrichments)

2️⃣ ActivityCount-Optimized Path

  • Sorting by activityCount (or no explicit orderBy)
  • No filters on me.*
  • Builds a top_members CTE selecting best members by msa."activityCount" (with oversampling)
  • This is the main optimization path — reduces load and latency for active-member views.

3️⃣ Generic Fallback Path

  • Any other case (different sort, me.* filters, no aggregates, etc.)

Count Query

  • buildCountQuery generates a minimal COUNT(DISTINCT m.id) query.
  • Includes only the necessary joins and CTEs (msa, member_orgs, or member_search when relevant).

Why This Matters

  • Performance: avoids unnecessary joins and top-N operations when IDs are pinned.
  • Efficiency: pushes ranking to msa (indexed) and filters externally to keep pages filled.
  • Maintainability: clear separation of query-building logic, easier to test and evolve.
  • Safety: smart defaults and fallback paths prevent invalid SQL and keep ordering consistent.

Note

Refactors member querying to a new query builder with optimized execution paths and CTE-based search, plus utilities for org/segment enrichment.

  • Members DAL (src/members)
    • Refactor queryMembersAdvanced: delegates SQL to buildQuery/buildCountQuery; parameterized CTE search via buildSearchCTE; logs constructed query; defaults orderBy to activityCount_DESC.
    • New utilities:
      • queryBuilder.ts: builds queries with three paths (direct ID, activityCount-optimized with top-N CTE, fallback); conditional CTEs/joins for member_search and member_orgs; safe ORDER BY mapping.
      • dataProcessor.ts: fetchOrganizationData, fetchSegmentData, and sortActiveOrganizations to enrich and order orgs/segments.
    • Data shaping: parallel fetching for organizations, identities, segments, and maintainers; simplified identity projection; optional inclusion of orgs/segments/maintainers respected.
    • Misc: switch to getServiceLogger; minor type tweaks; cleanup of column/field maps and constants.

Written by Cursor Bugbot for commit 6355108. This will update automatically on new commits. Configure here.

@ulemons ulemons self-assigned this Nov 7, 2025
@ulemons ulemons added the Feature Created by Linear-GitHub Sync label Nov 7, 2025
@ulemons ulemons force-pushed the feat/improve-member-query branch 5 times, most recently from 7a3d08b to f2e6d80 Compare November 13, 2025 08:51
@ulemons ulemons marked this pull request as ready for review November 13, 2025 09:44
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@ulemons ulemons force-pushed the feat/improve-member-query branch from f37ad7d to 92e3cb0 Compare November 14, 2025 09:29
@ulemons ulemons force-pushed the feat/improve-member-query branch from c65997d to c806dd5 Compare November 17, 2025 08:12
@ulemons ulemons force-pushed the feat/improve-member-query branch from c872b83 to 6355108 Compare November 17, 2025 15:27
@ulemons ulemons requested a review from themarolt November 17, 2025 15:40
@ulemons ulemons merged commit 8fdd59e into main Nov 19, 2025
19 checks passed
@ulemons ulemons deleted the feat/improve-member-query branch November 19, 2025 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Created by Linear-GitHub Sync

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants