Repository navigation
Compute has_unread from unread_count in channel and DM lists - #63
Merged
Merged
Conversation
The list queries computed has_unread with two MAX(m.id) subqueries per channel on top of the unread COUNT, all with the same filters. MAX(id) can't use idx_messages_channel, so every request scanned each channel's history three times. This query was the top D1 reader on Dan's tenant (52% of query time, ~107K rows read per call). A qualifying message newer than the read cursor exists exactly when unread_count > 0, so the response is unchanged. Wire contract unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
GET /channelsandGET /dmscomputedhas_unreadwith twoMAX(m.id)subqueries per channel, on top of theunread_countCOUNT subquery. All three used the same filters. This PR deriveshas_unreadasunread_count > 0and drops the two MAX subqueries.Why
handleListChannelswas the top D1 reader on the dan tenant: 52% of query time, 6,243 calls and 671M rows read in 24h, about 107K rows per call. The MAX subqueries can't useidx_messages_channel, so every request scanned each channel's history three times.Behavior
No change. A message that qualifies and is newer than the read cursor exists exactly when
unread_count > 0. The response shape and wire contract are unchanged.Verify
Related
/channels+/dmsrefresh from every 30s to every 10 min. That was roughly 45% of the calls.messages(channel_id, id) WHERE deleted_at IS NULL. It should wait until we've measured the query cost after this lands.🤖 Generated with Claude Code