Skip to main content

Where Clinical Measure Time Actually Goes: eCQM Scaling to 5,000 Patients

· 8 min read

A clinical quality measure is a good stress test for a query engine. You take an HL7 CQL library — hundreds of definitions, value sets, temporal intervals, polymorphic FHIR fields — compile it to one population query, and run it over an entire patient cohort at once. Nothing is cached between measures, and nothing is pre-flattened: the engine reads raw FHIR JSON.

We wanted a number for "how does this behave as the cohort grows", and more importantly where the time goes. So we built a ladder — the same measures at 100, 1,000 and 5,000 patients — with a query profile captured at every rung, run identically on both DuckDB engine lines.

The short version of what the profiles say: a measure's cost is set by its access pattern — which resource types it touches, how many times, and where the per-row predicates run — not by the size of its CQL or the number of terminology calls it makes. And you cannot see any of that by reading the SQL; you have to profile the operators.

Method, and one thing we deliberately did not count​

The headline metric is execution time only. CQL parsing, library translation, population-SQL generation and result conversion are measured and reported — but separately, as compile diagnostics. They never enter a speedup or a milliseconds-per-patient figure. Those phases are one-time per library version; a production deployment pays them at deploy, not at query.

  • Engine: FHIR4DS compiles each measure's CQL into a single population SQL query over an in-memory DuckDB table of FHIR resources. Every number below was run identically on both DuckDB 1.5.6 and 2.0.0.
  • Timing: median of three executions of the same pre-compiled SQL. DuckDB re-plans on every execution, so planning is inside the number; compilation is not.
  • Cohorts: nested deterministic prefixes of one Synthea generation (100 / 1,000 / 5,000 patients, 6.3 million resources at the top rung), measurement period 2026 calendar year, one fresh process per rung, corpus loaded from disk once per rung.
  • Measures: five, spanning the complexity range — CMS68 (documentation of current medications), CMS22 (BP screening follow-up), CMS2 (depression screening, the largest generated SQL), CMS130 (colorectal cancer screening), CMS133 (cataracts, ~110 distinct Condition value-set retrieves).
  • Baseline: the clinical-reasoning engine (Java, HAPI-based) evaluated per patient, linearly extrapolated from its per-patient cost. We did not run it at these sizes. That matters, and it is the first caveat below.

The ladder​

Execution time on DuckDB 1.5.6, median of three runs. Each cell shows the total and (per-patient) time:

Measure1001,0005,000
CMS6870 ms (0.70)117 ms (0.12)474 ms (0.09)
CMS22407 ms (4.07)1,206 ms (1.21)4,630 ms (0.93)
CMS2460 ms (4.60)734 ms (0.73)2,773 ms (0.55)
CMS1302,219 ms (22.2)12,733 ms (12.7)57,357 ms (11.5)
CMS1332,924 ms (29.2)10,421 ms (10.4)46,428 ms (9.3)

Two things to read off that table.

Per-patient cost falls as the cohort grows. CMS68 goes from 0.70 ms/patient at 100 patients to 0.09 ms/patient at 5,000; every measure shows the same direction. A columnar engine amortizes the fixed cost of planning, CTE materialization and the patient roster over more rows — the opposite of a per-patient engine, whose cost is flat by construction. Against the extrapolated Java baseline at 5,000 patients, that is roughly 7,600× (CMS68), 214× (CMS2), 57× (CMS130) and 26× (CMS22).

Shape matters more than size. CMS130 and CMS133 are 10–100× more expensive per patient than CMS68 and CMS2 — not because their CQL is bigger, but because of what they touch. The profile explains why.

Peak process memory at the 5,000-patient rung: 18.6 GB (the corpus itself is 6.3 million resources; rungs 100 and 1,000 stay under 5.4 GB).

Where the time actually goes​

Static analysis of the generated SQL is a poor predictor. A structural score (scan sites, SELECT DISTINCT, json_extract, date casts, joins, derived tables) ranks the measures roughly correctly but badly understates the magnitudes — the missing term is how many rows each predicate runs over, and per-row predicate cost, which only a profile shows.

The EXPLAIN ANALYZE profiles at the 5,000-patient rung split the measures into two clean families:

MeasureDominant costShare
CMS130expression evaluation99.6%
CMS2expression evaluation81.5%
CMS22expression evaluation86.9%
CMS133resource scans96.2%
CMS68resource scans87.7%

"Expression evaluation" is the fhirpath / value-set / interval predicates that DuckDB pushes into scan filters, evaluated over every row of a resource-type partition before selectivity. In one CMS68 operator, a filter costing 3× its own scan emits zero rows — the predicate is recomputing derived temporal bounds per row to reject rows a precomputed column would have rejected for free.

Two intuitive hypotheses the profiles kill, worth recording because both were wrong when we started:

"The value-set and interval calls are the expensive part." CMS2 contains far more value-set membership, coding-match and interval calls than CMS130. It is also 20× cheaper per patient. The calls are not the cost — where they run is. In CMS2 they sit behind selective filters over small candidate sets; in CMS130 the same kind of predicate runs inside a scan filter over millions of rows.

"Deduplicating the full JSON payload is a major win." Every retrieve CTE deduplicates on the whole JSON payload; that dedup accounts for under 1% of operator time on the predicate-heavy measures. Real, but nowhere near the top of the list.

The transferable lesson: before optimizing anything, find out which family your measure is in. Scan-dominated measures want fewer, shared scans; predicate- dominated measures want the derived values the predicates recompute hoisted into columns — the translator already does this for period_start/period_end, and the same treatment for temporal bounds is the highest-value next optimization.

Two engines, one SQL​

Because both DuckDB lines ship the same generated SQL, the ladder also gives a clean engine comparison at 5,000 patients:

Measure1.5.62.0.0Delta
CMS68474 ms234 ms−50.6%
CMS13057,357 ms5,463 ms−90.5%
CMS13346,428 ms5,329 ms−88.5%
CMS224,630 ms4,977 ms+7.5%
CMS22,773 ms2,574 ms−7.2%

DuckDB 2.0.0 is the fastest engine on four of the five measures at this rung and within noise on the fifth. Getting there required one planner setting. Out of the box, 2.0 regressed CMS2 catastrophically (~40 s vs 2.8 s): the cost was in planning, not execution. CMS2's generated SQL carries ~64 correlated scalar-subquery lookups, and the 2.0 planner's delimiter-to-CTE conversion scales exponentially in the number of such references — a 60-lookup query plans in ~8 s, an 80-lookup one in ~35 s, while the same sweep with delim_join_as_cte=false stays in single-digit seconds (we filed the repro upstream). Because fhir4ds SQL already uses explicit materialized CTEs, the setting is a strict win across every measure and rung we measured — no regression, identical results — so register() now applies it automatically on DuckDB 2.x connections. The 2.0 numbers above are with that tuning.

Read these numbers with four caveats​

We would rather publish the caveats than have someone discover them.

  1. The Java baseline is extrapolated, not measured. It is the reference engine's per-patient cost multiplied by cohort size. Per-patient evaluation does scale roughly linearly, but this is an extrapolation and is labelled as one in the source data — and its per-patient cost itself grows with data volume in practice, so the extrapolation flatters the baseline.
  2. Population counts in this benchmark are raw per-definition truth values, with no CQM attribution masking applied. On synthetic data the numerator can exceed the denominator. They are sanity signals that the query ran, not reported measure rates — accuracy is validated separately against the official HL7 test bundles (47/47 QI-Core 2025 eCQMs and 73/73 US-Core 2025 DQMs). CMS133 has empty populations on this cohort (Synthea generates no cataracts-surgery patients) while still executing its full 110-retrieve query — it measures machinery cost, not clinical yield.
  3. Operator shares under parallelism are not wall-clock seconds. Per-operator timings are summed across worker threads; the shares show relative cost distribution. Wall time is the measured execution figure.
  4. One machine, one corpus. Absolute numbers will differ elsewhere; the cost structure — which family a measure falls into, and what dominates its profile — is the transferable finding.

The full report — every rung, every operator bucket, both engines, the structural scoring method and the compile diagnostics — is in benchmarks/scaling/SCALING.md in the repository, regenerated by benchmarks/scaling/make_report.py from the raw run data.

The short version for anyone building on a SQL-native clinical engine: the standards are not what makes a measure expensive. Access patterns are. Profile the operators, find out which family each measure belongs to, and optimize for that family — the expensive part is never what the SQL looks like.