Skip to content

fix(events): replace repetitions groupHash_hashed index with groupHashAndId - #692

Merged
neSpecc merged 4 commits into
masterfrom
fix/repetitions-group-hash-id-index
Nov 20, 2026
Merged

neSpecc merged 4 commits into
masterfrom
fix/repetitions-group-hash-id-index

Conversation

@neSpecc

@neSpecc neSpecc commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Problem: slow query LatestRepetitions

How to reproduce:

  1. open EventOveriew
  2. go to Repetitions tab
  3. list loading is too slow

Fixes: https://garage.hawk.so/project/5ee3bb8531ad69816c67e035/event/6aff16f9960f464d87f7f097/6aff173fb78a9e93012b84e2/overview

image image

Reason

query LatestRepetitions (Event.repetitionsPortion -> EventsFactory.getEventRepetitions) queries repetitions:<projectId> with:

.find({ groupHash, _id: { $lte: cursor } }) // or { groupHash }
.sort({ _id: -1 })
.limit(limit + 1)

Because repetitions:<projectId> only had { groupHash: 'hashed' } (groupHash_hashed) and { groupHash: 1, release: 1 } (groupHashAndRelease), MongoDB had no index covering both groupHash equality and _id: -1 ordering/range. As a result, queries fell back to either scanning _id backwards across unrelated events or fetching all repetitions for a groupHash and performing a blocking in-memory sort — taking 4–20s on large collections.

Solution

  • Replaced groupHash_hashed with a compound B-tree index { groupHash: 1, _id: -1 } (groupHashAndId) on repetitions:<projectId> in createProject (src/resolvers/project.js).
  • Added migration migrations/20261119000000-replace-repetition-group-hash-index.js for existing repetitions:* collections: it creates groupHashAndId first (so groupHash queries remain indexed with zero downtime) and then drops groupHash_hashed.
  • Updated test/resolvers/project.test.ts to verify groupHashAndId is created and groupHash_hashed is not.

Why groupHash_hashed can be safely removed

groupHash is the leading prefix of the new { groupHash: 1, _id: -1 } B-tree index, so MongoDB can use groupHashAndId for any exact groupHash lookup (e.g., deleteMany({ groupHash }) in removeEvent) while also supporting _id sorting and cursor range scans (_id: { $lte: cursor }) — which a hashed index cannot do. Nothing in the codebase hints groupHash_hashed (while groupHashAndRelease is preserved for release-validator).

@neSpecc
neSpecc requested a review from Reversean November 19, 2026 21:22

@Kuchizu Kuchizu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heavy migration for a single-node DB, let's run it off-peak.

@neSpecc
neSpecc merged commit ae2dcdf into master Nov 20, 2026
7 checks passed
@neSpecc
neSpecc deleted the fix/repetitions-group-hash-id-index branch November 20, 2026 18:52
neSpecc added a commit that referenced this pull request Nov 20, 2026
…hAndId (#692) (#693)

* fix(events): replace repetitions groupHash_hashed index with groupHashAndId

* Bump version up to 1.7.1

* fix(migrations): simplify IndexNotFound error check

* chore(migrations): add progress logging for repetitions index migration

---------

Co-authored-by: Peter <specc.dev@gmail.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
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.

4 participants