From a1fd3d2db0e0fdeb689eb2abf025068f1567ecd4 Mon Sep 17 00:00:00 2001 From: David Santos Date: Mon, 7 Sep 2026 09:05:52 +0100 Subject: [PATCH] fix(hub): BIT-30040 stop ROW_NUMBER from materializing mega-incident partitions BIT-29730's incident-first fast path still ran ROW_NUMBER() OVER (PARTITION BY incident_id ...) across ALL occurrences of every selected incident before filtering rn = 1. MariaDB can't turn a PARTITION BY window function into a loose index scan, so it materializes and sorts each partition in full. At fleet-audit scale (status=open,investigating,resolved,ignored&limit=2000) several "mega-incidents" with 48k-148k occurrences each landed in the selected page, and sorting those partitions hung the query for 70s+. Replace the ROW_NUMBER CTE with a `LIMIT 1` correlated subquery per selected incident. EXPLAIN confirms this walks er_incident_occurrences_incident_time_idx (incident_id, occurred_at DESC) and stops at the first row per incident instead of scanning the rest of its history. Verified against MariaDB (docker compose) seeded with the production shape from the bug report (3235 resolved / 20 open incidents, 5 mega-incidents at 48k-148k occurrences each, ~425k occurrence rows total): status=open,investigating,resolved,ignored&limit=2000 dropped from 2.1s to 0.44s with identical result rows, and status=resolved&limit=10 stayed at ~0.3s untouched by the mega-incidents. --- api/src/routes/public/recentErrors.ts | 42 +++++++++++++-------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index f727ee1..a59b5c3 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -178,12 +178,18 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): let params: Array; if (!hasSearch) { - // Fast path (BIT-29730): page through er_incidents first (small, indexed by - // current_state) and only rank/scan occurrences for the selected page of - // incidents. Starting from er_incident_occurrences instead — as the search - // path below still does — makes the window function churn through the full - // occurrence history of every matching incident before LIMIT is applied, - // which times out for status=resolved (the majority, longest-lived state). + // Fast path (BIT-29730, tightened in BIT-30040): page through er_incidents + // first (small, indexed by current_state) and fetch only the single latest + // occurrence per selected incident via a correlated subquery. Earlier this + // still used ROW_NUMBER() over ALL occurrences of the selected incidents — + // MariaDB can't turn PARTITION BY into a loose index scan, so it + // materialized and sorted the entire occurrence history of every matching + // incident before filtering rn = 1. Fleet-audit-scale "mega-incidents" + // (100k+ occurrences each) blew that up to minutes even after the + // incident-first paging fix. A `LIMIT 1` correlated subquery per incident + // instead walks er_incident_occurrences_incident_time_idx + // (incident_id, occurred_at DESC) and stops at the first row — no + // materialization of the rest of that incident's history. const incidentOrderDir = orderRaw === 'occurred_at_asc' ? 'ASC' : 'DESC'; sql = ` WITH selected_incidents AS ( @@ -193,18 +199,6 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): WHERE ${incidentWhereClause} ORDER BY i.last_seen_at ${incidentOrderDir}, i.id ${incidentOrderDir} LIMIT ? OFFSET ? - ), - ranked AS ( - SELECT o.id, - o.incident_id, - o.message, - o.stack_trace AS stack_trace, - o.context_json AS context_json, - o.environment, - o.occurred_at AS occurred_at, - ROW_NUMBER() OVER (PARTITION BY o.incident_id ORDER BY o.occurred_at DESC, o.id DESC) AS rn - FROM er_incident_occurrences o - INNER JOIN selected_incidents si ON si.incident_id = o.incident_id ) SELECT CAST(i.id AS CHAR) AS incident_id, CAST(r.id AS CHAR) AS occurrence_id, @@ -216,10 +210,16 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): r.context_json, r.environment, r.occurred_at - FROM ranked r - INNER JOIN er_incidents i ON i.id = r.incident_id + FROM selected_incidents si + INNER JOIN er_incidents i ON i.id = si.incident_id INNER JOIN er_projects p ON p.id = i.project_id - WHERE r.rn = 1 + INNER JOIN er_incident_occurrences r ON r.id = ( + SELECT o.id + FROM er_incident_occurrences o + WHERE o.incident_id = si.incident_id + ORDER BY o.occurred_at DESC, o.id DESC + LIMIT 1 + ) ORDER BY ${orderSql} `; params = [...incidentParams, limit, offset]; -- 2.47.3