Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 22, 2025

The workflow visualization endpoint was making individual WorkflowStep.find calls inside a loop when building link data, causing N+1 queries.

Changes

  • Build hash lookup table from preloaded steps using index_by(&:id) instead of querying database for each target step
  • Add nil check to skip target steps not in current workflow (handles potential data integrity issues)

Before

steps.each do |step|
  step.workflow_step_options.each do |option|
    target_step = DiscourseWorkflow::WorkflowStep.find(option.target_step_id)
    links << { source: step.name, target: target_step.name, ... }
  end
end

After

steps_by_id = steps.index_by(&:id)

steps.each do |step|
  step.workflow_step_options.each do |option|
    target_step = steps_by_id[option.target_step_id]
    next unless target_step
    links << { source: step.name, target: target_step.name, ... }
  end
end

Reduces database queries from O(n) to O(1) for link generation, where n is the number of workflow step options.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 22, 2025 16:02
Co-authored-by: merefield <35533304+merefield@users.noreply.github.com>
Co-authored-by: merefield <35533304+merefield@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on performance improvements from PR #9 Fix N+1 query in workflow visualization controller Nov 22, 2025
Copilot finished work on behalf of merefield November 22, 2025 16:06
Copilot AI requested a review from merefield November 22, 2025 16:06
@merefield merefield marked this pull request as ready for review November 22, 2025 16:16
@merefield merefield changed the base branch from eliminate_n_plus_1 to main November 22, 2025 16:17
@merefield merefield merged commit a647128 into main Nov 22, 2025
4 checks passed
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