Skip to content

Mutations table: read copy number for the table's genes only; compute its rows once - #502

Merged
alisman merged 1 commit into
masterfrom
mutation-table-query-cost
Nov 23, 2026
Merged

alisman merged 1 commit into
masterfrom
mutation-table-query-cost

Conversation

@alisman

@alisman alisman commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The Mutations tab got 5× slower between 140c923 and b12e240. Its first draw went from 3.5s to 16.7s on coadread (KRAS) and from 4.4s to 24.8s on msk_chord (TP53). This PR removes most of that slowdown and leaves the table's contents unchanged.

Cause

Two parts of the table's row statement (mutationRowSource) do most of the extra reading:

  1. The copy-number lookup reads every gene. Since 96a8c05 (Mutation table: the portal's Mutation Type labels and its remaining columns (G) #245), cn<i> reads every gene's discrete CNA call for the table's samples. On msk_chord that is about 12.7M rows of genetic_alteration_derived per statement. Its joins only ever match the queried gene (cn.g = gene) and the fusion rows' site 1 genes (cn.g = v.gene1). It now reads only those genes.
  2. The mutation and fusion rows are recomputed at every use. The statement refers to m<i> and v<i> 21 times, and ClickHouse recomputes a CTE at each reference. Where the connection supports enable_materialized_cte (materializedCteSupported, the check the other views already use), they are now AS MATERIALIZED. Each row statement (page, counts, values, rank) ends with the setting through a new RowSource.settings. Runs from recorded answers (the snapshots) keep the plain statements.

Replaying the captured msk_chord TP53 counts statement:

variant rows read ClickHouse time
b12e240 35.8M 4.0–4.6s
copy-number lookup narrowed 23.1M 1.8–3.7s
rows computed once 16.1M 4.0–4.5s
both (this PR) 3.5M 0.75–1.2s

Result

Browser benchmark with production builds, cold loads, until the page is fully drawn. Medians of 3 loads for coadread and 6 for msk_chord:

140c923 b12e240 this PR
coadread KRAS 3.3s 16.5s 4.2s
msk_chord TP53 3.6s 18.5s 7.3s

msk_chord is still about 2× its time on 140c923. The rest comes from two things this PR doesn't touch:

Verification

  • Same tables from the API. /api/analysis returned identical mutations tables from b12e240 and this branch: rows, totals and Copy # values. Cases:
    • msk_chord: TP53, ALK, TMPRSS2 (including its 315 fusion rows) and KRAS.
    • coadread: KRAS and APC.
    • msk_impact_2017: EGFR.
  • Statement text tests. The table's page, counts and values statements are now in test/support/view-mcte-cases.ts. The test checks that exactly m0 and v0 are materialized and that the setting is appended. These tests fail without the change.
  • Fixture rows. Under F1 in view-materialized-fixture, materialized and plain statements return the same rows, for 3 genes × 2 cohorts.
  • New fixture check for the gene condition. F3 compares every row with and without the new copy-number condition, for EGFR, KRAS and TP53 (73 of 76 rows have a Copy # call) and ALK fusions. The fixture has no CNA rows for a fusion partner's gene, so that join is covered only by the warehouse comparison above (TMPRSS2).
  • Mutations snapshot. I re-recorded it. Its only SQL change is the copy-number condition, and the results file is unchanged.
  • Test and lint runs.
    • npm test has two failures that are not from this change:
      • F1 in view-materialized-fixture fails with no fixture database. It fails the same way on master.
      • The TODO-marked parity cases fail on master too.
    • npx tsc --noEmit, eslint and knip are clean.
    • I did not run scripts/gate.sh.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q3ezTZuofAGZDqqTqBUcTu


Get evidence
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

… its rows once

The Mutations tab's first draw went from 3.5s to 16.7s (coadread KRAS) and 4.4s to 24.8s
(msk_chord TP53) between 140c923 and b12e240. Two things in the table's statement cause it:

- The copy-number lookup (cn<i>) has read every gene's discrete CNA call for the table's
  samples since 96a8c05, about 12.7M rows of genetic_alteration_derived per statement on
  msk_chord. It now reads only the queried gene and the fusion rows' site 1 genes, the only
  keys its two joins (cn.g = gene, cn.g = v.gene1) ever match.
- The mutation and fusion rows (m<i>, v<i>) are referenced 21 times in the statement, and
  ClickHouse inlines a CTE at each reference. Where the connection runs
  enable_materialized_cte (materializedCteSupported, as the other views already ask), they are
  now AS MATERIALIZED and every row statement (page, counts, values, rank) ends with the
  setting, via a new RowSource.settings. Recorded-answer runs (snapshots) stay plain.

The rows are unchanged. Verified:
- Replaying the captured msk_chord TP53 counts statement: 35.8M rows read and 4.0-4.6s before,
  3.5M and 0.75-1.2s with both changes.
- /api/analysis mutations tables from b12e240 and this branch are identical (rows, totals,
  Copy # values) for msk_chord TP53/ALK/TMPRSS2 (315 fusion rows)/KRAS, coadread KRAS/APC and
  msk_impact_2017 EGFR.
- Browser benchmark, cold, fully drawn: coadread KRAS 16.5s -> 4.2s (baseline 3.3s);
  msk_chord TP53 18.5s -> 7.3s (baseline 3.6s).
- Tests: the mutation table's page, counts and values statements join the materialized case
  table (text: exactly m0 and v0 materialized and the setting appended; fixture: the same rows
  as the plain statement, 3 genes x 2 cohorts). A new fixture check (F3) compares every row
  with and without the gene condition, including ALK fusion rows. The mutations snapshot is
  re-recorded; only its copy-number condition changed, its results are byte-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q3ezTZuofAGZDqqTqBUcTu
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cbiolab-genie Ready Ready Preview Nov 23, 2026 4:08pm UTC
cbiolab-new Ready Ready Preview Nov 23, 2026 4:08pm UTC
cohort-lab Ready Ready Preview Nov 23, 2026 4:08pm UTC

Request Review

@alisman
alisman added this pull request to the merge queue Nov 23, 2026
Merged via the queue into master with commit 328f8b1 Nov 23, 2026
13 checks passed
@alisman
alisman deleted the mutation-table-query-cost branch November 23, 2026 17:29

This branch was successfully deployed

1 active deployment
Preview – cohort-lab — b0c6e383 Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant