Skip to content

Commit c3f8d44

Browse files
committed
Avoid preloading patient sessions
Some sessions can have lots of patient sessions (community clinics can sometimes have upwards of 50,000 patients in) and preloading all of this can be slow and require a large amount of memory. Often at this scale, it seems to be more performant to not avoid N+1 queries, particularly depending on the page that is trying to be loaded.
1 parent 92310d2 commit c3f8d44

File tree

7 files changed

+14
-32
lines changed

7 files changed

+14
-32
lines changed

app/components/app_session_actions_component.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ class AppSessionActionsComponent < ViewComponent::Base
66
<%= govuk_summary_list(rows:) %>
77
ERB
88

9-
def initialize(session, patient_sessions:)
9+
def initialize(session)
1010
super
1111

1212
@session = session
13-
@patient_sessions = patient_sessions
1413
end
1514

1615
def render?
@@ -19,7 +18,9 @@ def render?
1918

2019
private
2120

22-
attr_reader :session, :patient_sessions
21+
attr_reader :session
22+
23+
delegate :patient_sessions, to: :session
2324

2425
def rows
2526
@rows ||= [

app/components/app_session_details_summary_component.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# frozen_string_literal: true
22

33
class AppSessionDetailsSummaryComponent < ViewComponent::Base
4-
def initialize(session, patient_sessions:)
4+
def initialize(session)
55
super
66

77
@session = session
8-
@patient_sessions = patient_sessions
98
end
109

1110
def call
@@ -14,7 +13,9 @@ def call
1413

1514
private
1615

17-
attr_reader :session, :patient_sessions
16+
attr_reader :session
17+
18+
delegate :patient_sessions, to: :session
1819

1920
def cohort_row
2021
count = patient_sessions.count

app/controllers/programmes_controller.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@ def sessions
3232
policy_scope(Session)
3333
.has_programme(@programme)
3434
.for_current_academic_year
35-
.eager_load(:location)
36-
.preload(
37-
:session_dates,
38-
patient_sessions: [
39-
:gillick_assessments,
40-
:session_attendances,
41-
{
42-
patient: %i[consent_statuses triage_statuses vaccination_records]
43-
}
44-
]
45-
)
35+
.includes(:location, :session_dates)
4636
.order("locations.name")
4737
end
4838

app/controllers/sessions_controller.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ def completed
2929

3030
def show
3131
respond_to do |format|
32-
format.html do
33-
@patient_sessions =
34-
@session
35-
.patient_sessions
36-
.includes(:patient)
37-
.in_programmes(@session.programmes)
38-
39-
render layout: "full"
40-
end
32+
format.html { render layout: "full" }
4133

4234
format.xlsx do
4335
filename =

app/views/sessions/show.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727

2828
<h3 class="nhsuk-heading-s nhsuk-u-margin-bottom-2">Session details</h3>
2929

30-
<%= render AppSessionDetailsSummaryComponent.new(@session, patient_sessions: @patient_sessions) %>
30+
<%= render AppSessionDetailsSummaryComponent.new(@session) %>
3131

3232
<% if @session.unscheduled? %>
3333
<% if policy(@session).edit? %>
3434
<%= govuk_button_link_to "Schedule sessions", edit_session_path(@session), class: "app-button--secondary" %>
3535
<% end %>
3636
<% else %>
37-
<%= render AppSessionActionsComponent.new(@session, patient_sessions: @patient_sessions) %>
37+
<%= render AppSessionActionsComponent.new(@session) %>
3838

3939
<div class="app-button-group">
4040
<% if policy(@session).edit? %>

spec/components/app_session_actions_component_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
describe AppSessionActionsComponent do
44
subject { render_inline(component) }
55

6-
let(:component) { described_class.new(session, patient_sessions:) }
6+
let(:component) { described_class.new(session) }
77

88
let(:programmes) { [create(:programme, :hpv)] }
99
let(:session) { create(:session, programmes:) }
10-
let(:patient_sessions) { session.patient_sessions }
1110

1211
before do
1312
create(

spec/components/app_session_details_summary_component_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
describe AppSessionDetailsSummaryComponent do
44
subject { render_inline(component) }
55

6-
let(:component) { described_class.new(session, patient_sessions:) }
6+
let(:component) { described_class.new(session) }
77

88
let(:programme) { create(:programme, :hpv) }
99
let(:session) { create(:session, programmes: [programme]) }
10-
let(:patient_sessions) { session.patient_sessions }
1110

1211
it { should have_text("Cohort\nNo children") }
1312
it { should have_text("Consent refused\nNo children") }

0 commit comments

Comments
 (0)