Merge pull request 'fix(hub): BIT-30040 stop ROW_NUMBER from materializing mega-incident partitions' (#13) from fix/BIT-30040-recent-errors-mega-incident-fastpath into master
This commit was merged in pull request #13.
This commit is contained in:
commit
3da9e47873
1 file changed
+21
-21
@@ -178,12 +178,18 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply):
|
||||
let params: Array<string | number>;
|
||||
|
||||
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];
|
||||
|
||||
Reference in new issue
Block a user