Skip to content

perf: sort fewer rows when grouping notifications - #473

Open
SanderMuller wants to merge 1 commit into
dev-null-sh:productionfrom
SanderMuller:perf/notification-grouping-sorts
Open

SanderMuller wants to merge 1 commit into
dev-null-sh:productionfrom
SanderMuller:perf/notification-grouping-sorts

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

The inbox and the unread badge, which every page shows, both group a user's notifications with window functions. This makes that grouping cheaper in three ways:

  • The windows over the whole history move into their own CTE. The per-topic windows after them then share one sort, where they used to sort every row twice.
  • The badge counts the distinct groups that have an unread notification, instead of filtering ranked groups on group_rank and unread_count.
  • The grouping reads only the seven columns it needs, so its sorts no longer carry whole rows. The inbox and the group popover join the rest of each notification afterwards. The inbox only does that for the rows with group_rank = 1.

What the inbox shows stays the same. On the stress database, the inbox and badge queries return the same rows as on production. The existing tests cover the group popover.

I measured it on the StressSeeder database, for an account with 38,583 notifications (20,250 unread). Each query ran 15 times with EXPLAIN ANALYZE, alternating with the production version:

Query production median This PR median Change, median of the pairs
Inbox page 432 ms 349 ms −18%
Unread badge 263 ms 226 ms −15%

The machine was under load during the run, so the absolute times run high. The two versions ran back to back, so the ratio holds.

#407 changes lines next to these, so it may need a small rebase. #308 moves the inbox query into a new method, which would need the same join after a rebase.

🤖 Generated with Claude Code

Move the whole-history windows into their own CTE, count the
badge as distinct groups with an unread notification, and group
narrow rows, joining whole notifications after group_rank = 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

1 participant