Skip to content

speed up slow stats aggregations#613

Open
Warbs816 wants to merge 1 commit into
Yooooomi:masterfrom
Warbs816:perf/stats-queries
Open

speed up slow stats aggregations#613
Warbs816 wants to merge 1 commit into
Yooooomi:masterfrom
Warbs816:perf/stats-queries

Conversation

@Warbs816
Copy link
Copy Markdown

Rewrites four slow stats aggregations in apps/server/src/database/queries/stats.ts:

  • getLongestListeningSession — replaces the O(N²) $reduce+$concatArrays sessionizer with a $setWindowFields+$shift pass (cumulative-sum session id).
  • albumDateRatio, featRatio, getBestArtistsPer — drop the per-play tracks lookup and read albumId / artistIds / primaryArtistId straight off infos. albumDateRatio additionally pre-aggregates by (albumId, time bucket) before the albums lookup.

Output shapes and numerical results unchanged.

Measured locally on Mongo 8.3 (one user, ~85K plays, all-time year split):

Query Before After
getLongestListeningSession 82 s 1.6 s
albumDateRatio 7.0 s 1.8 s
featRatio 4.1 s 1.1 s
getBestArtistsPer 4.1 s 0.9 s

Requires MongoDB ≥ 5.0 (for $setWindowFields/$shift).

@Warbs816 Warbs816 marked this pull request as ready for review May 24, 2026 11:49
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