22--
33-- $materialize drops and recreates the tables — indexes are lost.
44-- Re-run this script after every $materialize call.
5- --
6- -- Perf benchmark (100k patients, POC :7888, 12 measures cohort SQL, warm cache):
7- -- without new indexes: 11.98s total
8- -- with new indexes: 9.91s total (1.21x speedup)
9- -- biggest win: CMS1154 1.88s → 0.54s (3.5x) from sof.patient_flat (id) alone
105
11- -- patient_id lookups (one per measure cohort)
6+ -- patient_id / id lookups
127CREATE INDEX IF NOT EXISTS idx_sof_patient_id ON sof .patient_flat (id);
138CREATE INDEX IF NOT EXISTS idx_sof_encounter_patient ON sof .encounter_flat (patient_id);
149CREATE INDEX IF NOT EXISTS idx_sof_condition_patient ON sof .condition_flat (patient_id);
@@ -25,12 +20,12 @@ CREATE INDEX IF NOT EXISTS idx_sof_condition_code ON sof.condition_flat(code_sys
2520CREATE INDEX IF NOT EXISTS idx_sof_procedure_code ON sof .procedure_flat (code_system, code);
2621CREATE INDEX IF NOT EXISTS idx_sof_observation_code ON sof .observation_flat (code_system, code);
2722CREATE INDEX IF NOT EXISTS idx_sof_observation_value ON sof .observation_flat (value_system, value_code);
28- CREATE INDEX IF NOT EXISTS idx_sof_servicerequest_code ON sof .servicerequest_flat (code_system, code);
29- CREATE INDEX IF NOT EXISTS idx_sof_medicationrequest_med ON sof .medicationrequest_flat (med_system, med_code);
30- CREATE INDEX IF NOT EXISTS idx_sof_devicerequest_code ON sof .devicerequest_flat (code_system, code);
3123
32- -- Composite covering (patient+code in one access path — encounter & condition are hottest)
33- CREATE INDEX IF NOT EXISTS idx_sof_encounter_patient_type ON sof .encounter_flat (patient_id, type_system, type_code);
24+ -- Composite covering — verified via EXPLAIN ANALYZE that the planner picks
25+ -- this for cms130/cms131/cms143/cms149/cms155 condition lookups (Bitmap Heap
26+ -- Scan over patient_id + code_system + code). Encounter equivalent was tried
27+ -- and dropped: planner never selected it and its presence caused a Hash→Merge
28+ -- join flip in cms153 that regressed that measure.
3429CREATE INDEX IF NOT EXISTS idx_sof_condition_patient_code ON sof .condition_flat (patient_id, code_system, code);
3530
3631-- ANALYZE so planner has fresh row estimates immediately after $materialize.
0 commit comments