Repository navigation
perf: optimize AlertHistory queries (HDX-3706) - #1928
Conversation
Limit the $match scan to `limit * interval` instead of the full 30-day
TTL window. Add $sort by createdAt before $group to enable the streaming
group optimization using the existing { alert: 1, createdAt: -1 } index.
Made-with: Cursor
…lertHistories
Add a new compound index on { alert: 1, group: 1, createdAt: -1 } to
directly support the $sort + $group/$first pattern used by
getPreviousAlertHistories. Update the $sort stage to match the new index.
The existing { alert: 1, createdAt: -1 } index is kept for
getRecentAlertHistories.
Made-with: Cursor
Limit the $match scan to the last 7 days instead of the full 30-day TTL window. The check task runs every minute so the latest entry is normally ~1 minute old; 7 days provides ample margin for extended system outages. Made-with: Cursor
Use Date.now()-relative timestamps instead of hardcoded 2024 dates so test data falls within the new interval-based lookback window. Add the required interval parameter to all getRecentAlertHistories calls. Made-with: Cursor
🦋 Changeset detectedLatest commit: 59135a8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Made-with: Cursor
E2E Test Results✅ All tests passed • 90 passed • 3 skipped • 1017s
Tests ran across 4 shards in parallel. |
PR Review✅ The implementation is correct and the performance optimizations are sound. No critical bugs or security issues found. One minor concern worth acknowledging:
Everything else (sort ordering, streaming group optimization alignment, |
- **`getRecentAlertHistories`**: Add interval-based `createdAt` lower bound to `$match` (scans only `limit * interval` instead of the full 30-day TTL window) and insert `$sort: { createdAt: -1 }` before `$group` to enable the streaming group optimization using the existing `{ alert: 1, createdAt: -1 }` index. For a 1-minute alert with limit 20, this reduces the scan from ~43k docs to ~20 docs (~2,160x).
- **`getPreviousAlertHistories`**: Add a new `{ alert: 1, group: 1, createdAt: -1 }` compound index and align the `$sort` stage to match it, so the `$group`/`$first` pattern can leverage the index directly. Also add a 7-day `createdAt` lower bound to the `$match` stage (~4x scan reduction vs. 30-day TTL).
- Update tests to use `Date.now()`-relative timestamps and pass the new `interval` parameter.
Summary
getRecentAlertHistories: Add interval-basedcreatedAtlower bound to$match(scans onlylimit * intervalinstead of the full 30-day TTL window) and insert$sort: { createdAt: -1 }before$groupto enable the streaming group optimization using the existing{ alert: 1, createdAt: -1 }index. For a 1-minute alert with limit 20, this reduces the scan from ~43k docs to ~20 docs (~2,160x).getPreviousAlertHistories: Add a new{ alert: 1, group: 1, createdAt: -1 }compound index and align the$sortstage to match it, so the$group/$firstpattern can leverage the index directly. Also add a 7-daycreatedAtlower bound to the$matchstage (~4x scan reduction vs. 30-day TTL).Date.now()-relative timestamps and pass the newintervalparameter.Test plan
alertHistory.test.tsintegration tests pass (9/9)checkAlerts.test.tsintegration tests (existing tests use dates within the 7-day window relative to theirnowparameter, so no changes needed)Made with Cursor