Skip to content

Commit 6e7ebdc

Browse files
committed
Optimize complete plan query
- Pre-fetch the answers, questions, and sections as part of the initial plans query (but only when we are in fact fetching all of the questions and answers). - This speeds up the mean request time by 25%
1 parent a370249 commit 6e7ebdc

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

app/controllers/api/v2/plans_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def index
2626
raise Pundit::NotAuthorizedError unless @scopes.include?('read')
2727

2828
@plans = PlansPolicy::Scope.new(@resource_owner).resolve
29+
@plans = @plans.includes(answers: { question: :section }) if @complete
2930
@items = paginate_response(results: @plans)
3031
render '/api/v2/plans/index', status: :ok
3132
end

app/presenters/api/v2/plan_presenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def plan_costs(plan:)
6060

6161
# Fetch all questions and answers from a plan, regardless of theme
6262
def fetch_all_q_and_a
63-
answers = @plan.answers.includes(question: [:section])
63+
answers = @plan.answers
6464
return [] unless answers.present?
6565

6666
answers.filter_map do |answer|

0 commit comments

Comments
 (0)