Skip to content

Commit

Permalink
Add new indexes on reporting_patient_states (#5349)
Browse files Browse the repository at this point in the history
**Story card:**
[sc-11600](https://app.shortcut.com/simpledotorg/story/11600/optimize-queries-used-by-dhis2-exporter)

The current queries for our monthly DHIS2 exports are slow. We want to
add a couple of indexes to the reporting_patient_states table to improve
this.

This PR adds a migration to create four composite indexes:
- on month_date and assigned_facility_id
- on month_date and assigned_facility_region_id
- on month_date and registration_facility_id
- on month_date and registration_facility_region_id

We're accounting for both the facility and the facility region because
both these fields are used throughout the codebase and we don't want one
query to pick up the correct index and not another.
  • Loading branch information
qptr authored Dec 14, 2023
1 parent 64ccd7c commit d3d129e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddIndexesToReportingPatientStates < ActiveRecord::Migration[6.1]
# Cannot create index concurrently inside a transaction block
self.disable_ddl_transaction = true

def change
add_index :reporting_patient_states, %i[month_date assigned_facility_id], name: "patient_states_month_date_assigned_facility", algorithm: :concurrently
add_index :reporting_patient_states, %i[month_date assigned_facility_region_id], name: "patient_states_month_date_assigned_facility_region", algorithm: :concurrently
add_index :reporting_patient_states, %i[month_date registration_facility_id], name: "patient_states_month_date_registration_facility", algorithm: :concurrently
add_index :reporting_patient_states, %i[month_date registration_facility_region_id], name: "patient_states_month_date_registration_facility_region", algorithm: :concurrently
end
end
31 changes: 30 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7140,13 +7140,41 @@ CREATE INDEX patient_states_assigned_state ON public.reporting_patient_states US
CREATE INDEX patient_states_care_state ON public.reporting_patient_states USING btree (hypertension, htn_care_state, htn_treatment_outcome_in_last_3_months);


--
-- Name: patient_states_month_date_assigned_facility; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX patient_states_month_date_assigned_facility ON public.reporting_patient_states USING btree (month_date, assigned_facility_id);


--
-- Name: patient_states_month_date_assigned_facility_region; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX patient_states_month_date_assigned_facility_region ON public.reporting_patient_states USING btree (month_date, assigned_facility_region_id);


--
-- Name: patient_states_month_date_patient_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX patient_states_month_date_patient_id ON public.reporting_patient_states USING btree (month_date, patient_id);


--
-- Name: patient_states_month_date_registration_facility; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX patient_states_month_date_registration_facility ON public.reporting_patient_states USING btree (month_date, registration_facility_id);


--
-- Name: patient_states_month_date_registration_facility_region; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX patient_states_month_date_registration_facility_region ON public.reporting_patient_states USING btree (month_date, registration_facility_region_id);


--
-- Name: patient_visits_patient_id_month_date; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -7644,6 +7672,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230614171507'),
('20230713065237'),
('20230713065420'),
('20230713135154');
('20230713135154'),
('20231208091419');


0 comments on commit d3d129e

Please sign in to comment.