Skip to content

Conversation

@keithwillcode
Copy link
Contributor

@keithwillcode keithwillcode commented Jan 27, 2026

What does this PR do?

Optimizes a slow SQL query that counts EventType children. The original Prisma-generated query was doing a full table sequential scan (~3.5M rows, 825ms execution time) because the _count.children aggregation generates a subquery with WHERE 1=1 that scans ALL rows.

The new approach uses a raw SQL query that filters the subquery to only count children for the specific IDs being queried, leveraging the existing parentId index.

Before (Prisma-generated):

SELECT ... COALESCE("aggr_selection_0_EventType"."_aggr_count_children", 0) 
FROM "public"."EventType" 
LEFT JOIN (
  SELECT "parentId", COUNT(*) FROM "public"."EventType" 
  WHERE 1=1  -- Full table scan!
  GROUP BY "parentId"
) ...

After (optimized raw SQL):

SELECT ... COALESCE(child_counts.children_count, 0) 
FROM "public"."EventType" et
LEFT JOIN (
  SELECT "parentId", COUNT(*) FROM "public"."EventType" 
  WHERE "parentId" = ANY($1::int[])  -- Uses parentId index
  GROUP BY "parentId"
) ...

This count is used only for displaying "(+N)" in workflow list tooltips to show child event type counts.

Updates since last revision

  • Added missing await to all enrichWorkflowsWithChildrenCount calls in WorkflowRepository
  • Reverted eventTypeRepository changes - the optimization is now only applied in WorkflowRepository. The eventTypeRepository methods (findById, findByIdForOrgAdmin) continue to use Prisma's _count.children because the tRPC types expect this structure and these are single event type queries with minimal performance impact.

Link to Devin run: https://app.devin.ai/sessions/be18fff405b642a7aa33be8bf5ca5146
Requested by: @keithwillcode

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. N/A - no documentation changes needed.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  1. Navigate to the Workflows page where workflows are listed with "Active on X event types"
  2. Hover over the event types to see the tooltip with child counts (e.g., "30 Min Meeting (+3)")
  3. Verify the counts display correctly
  4. For performance verification: Check database query logs to confirm the new query uses an index scan instead of sequential scan

Checklist for Human Review

  • Verify the raw SQL query produces identical results to the original Prisma query
  • Check if hardcoded "public"."EventType" schema is appropriate for all environments
  • Review the as EnrichActiveOn<T>[] type cast in enrichWorkflowsWithChildrenCount
  • Verify BigInt to Number conversion handles edge cases correctly

Replace Prisma's _count.children aggregation with an optimized raw SQL
query that uses the parentId index instead of doing a full table scan.

The original Prisma-generated query scanned ~3.5M rows (825ms) because
the subquery had no WHERE clause. The new query filters the subquery
to only count children for the specific IDs being queried, reducing
execution time significantly by leveraging the existing parentId index.

Changes:
- Add getEventTypesWithChildrenCount helper using raw SQL in WorkflowRepository
- Add findByIdsIncludeChildrenCount method to EventTypeRepository
- Add enrichWorkflowsWithChildrenCount to enrich workflow data with counts
- Update all workflow fetching methods to use the optimized query
- Update eventTypeRepository findById and findByIdForOrgAdmin methods

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@keithwillcode keithwillcode self-assigned this Jan 27, 2026
keithwillcode and others added 2 commits January 27, 2026 17:35
…alls

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
The optimization for eventTypeRepository.findById and findByIdForOrgAdmin
caused type errors because the tRPC types expect _count.children to be
present in the workflow.activeOn.eventType structure.

The main performance optimization remains in WorkflowRepository where
the raw SQL query is used to efficiently fetch children counts for
multiple workflows.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants