From 2371d99e9b12a7351dc2286b8f7ff83b0abb4051 Mon Sep 17 00:00:00 2001 From: filaebot Date: Sat, 14 Nov 2026 12:26:27 -0500 Subject: [PATCH] Compute has_unread from unread_count in channel and DM lists 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. --- api/src/routes/channels.ts | 67 +++++++++++++++----------------------- api/src/routes/dms.ts | 28 ++-------------- 2 files changed, 29 insertions(+), 66 deletions(-) diff --git a/api/src/routes/channels.ts b/api/src/routes/channels.ts index 2c1f835..f4e1375 100644 --- a/api/src/routes/channels.ts +++ b/api/src/routes/channels.ts @@ -42,49 +42,34 @@ async function notifyBotViaRecentChannels(env: Env, botUserId: string, excludeCh } export async function handleListChannels(env: Env, user: User): Promise { + // has_unread is derived from unread_count in the outer query. It used to be + // two extra MAX(m.id) subqueries per channel with the same filters as the + // COUNT; MAX(id) can't use idx_messages_channel's ordering, so each channel's + // history was scanned three times per request instead of once. "A qualifying + // message newer than the read cursor exists" is exactly unread_count > 0. const channels = await env.DB.prepare(` - SELECT c.*, - cm.notifications, - cr.last_read_message_id, - CASE cm.notifications - WHEN 'none' THEN 0 - WHEN 'mentions' THEN CASE WHEN ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - AND EXISTS (SELECT 1 FROM message_mentions mm WHERE mm.message_id = m.id AND mm.user_id = ?) - ) IS NOT NULL AND ( - cr.last_read_message_id IS NULL OR cr.last_read_message_id < ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - AND EXISTS (SELECT 1 FROM message_mentions mm WHERE mm.message_id = m.id AND mm.user_id = ?) - ) - ) THEN 1 ELSE 0 END - ELSE CASE WHEN ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') + SELECT x.*, CASE WHEN x.unread_count > 0 THEN 1 ELSE 0 END as has_unread + FROM ( + SELECT c.*, + cm.notifications, + cr.last_read_message_id, + CASE cm.notifications + WHEN 'none' THEN 0 + WHEN 'mentions' THEN (SELECT COUNT(*) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') + AND (cr.last_read_message_id IS NULL OR m.id > cr.last_read_message_id) AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - ) IS NOT NULL AND ( - cr.last_read_message_id IS NULL OR cr.last_read_message_id < ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - ) - ) THEN 1 ELSE 0 END - END as has_unread, - CASE cm.notifications - WHEN 'none' THEN 0 - WHEN 'mentions' THEN (SELECT COUNT(*) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND (cr.last_read_message_id IS NULL OR m.id > cr.last_read_message_id) - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - AND EXISTS (SELECT 1 FROM message_mentions mm WHERE mm.message_id = m.id AND mm.user_id = ?)) - ELSE (SELECT COUNT(*) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND (cr.last_read_message_id IS NULL OR m.id > cr.last_read_message_id) - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id)) - END as unread_count - FROM channels c - JOIN channel_members cm ON c.id = cm.channel_id - LEFT JOIN channel_reads cr ON c.id = cr.channel_id AND cr.user_id = ? - WHERE cm.user_id = ? AND (c.is_dm = 0 OR c.is_dm IS NULL) AND cm.left_at IS NULL AND c.archived_at IS NULL - ORDER BY c.name - `).bind(user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id).all(); + AND EXISTS (SELECT 1 FROM message_mentions mm WHERE mm.message_id = m.id AND mm.user_id = ?)) + ELSE (SELECT COUNT(*) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') + AND (cr.last_read_message_id IS NULL OR m.id > cr.last_read_message_id) + AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id)) + END as unread_count + FROM channels c + JOIN channel_members cm ON c.id = cm.channel_id + LEFT JOIN channel_reads cr ON c.id = cr.channel_id AND cr.user_id = ? + WHERE cm.user_id = ? AND (c.is_dm = 0 OR c.is_dm IS NULL) AND cm.left_at IS NULL AND c.archived_at IS NULL + ) x + ORDER BY x.name + `).bind(user.id, user.id, user.id, user.id, user.id).all(); return jsonResponse(channels.results); } diff --git a/api/src/routes/dms.ts b/api/src/routes/dms.ts index 609a420..7b5f8cb 100644 --- a/api/src/routes/dms.ts +++ b/api/src/routes/dms.ts @@ -145,29 +145,6 @@ export async function handleListDMs(env: Env, user: User): Promise { cm.notifications, cr.last_read_message_id, u.id as partner_id, u.username as partner_username, u.display_name as partner_display_name, u.name_color as partner_name_color, u.role as partner_role, u.bot_capabilities_json as partner_bot_capabilities_json, - CASE cm.notifications - WHEN 'none' THEN 0 - WHEN 'mentions' THEN CASE WHEN ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - AND EXISTS (SELECT 1 FROM message_mentions mm WHERE mm.message_id = m.id AND mm.user_id = ?) - ) IS NOT NULL AND ( - cr.last_read_message_id IS NULL OR cr.last_read_message_id < ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - AND EXISTS (SELECT 1 FROM message_mentions mm WHERE mm.message_id = m.id AND mm.user_id = ?) - ) - ) THEN 1 ELSE 0 END - ELSE CASE WHEN ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - ) IS NOT NULL AND ( - cr.last_read_message_id IS NULL OR cr.last_read_message_id < ( - SELECT MAX(m.id) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') - AND NOT EXISTS (SELECT 1 FROM message_reads mr WHERE mr.user_id = cm.user_id AND mr.message_id = m.id) - ) - ) THEN 1 ELSE 0 END - END as has_unread, CASE cm.notifications WHEN 'none' THEN 0 WHEN 'mentions' THEN (SELECT COUNT(*) FROM messages m WHERE m.channel_id = c.id AND m.deleted_at IS NULL AND m.user_id != ? AND m.message_type IN ('human', 'response') @@ -187,7 +164,7 @@ export async function handleListDMs(env: Env, user: User): Promise { LEFT JOIN channel_reads cr ON c.id = cr.channel_id AND cr.user_id = ? WHERE c.is_dm = 1 AND cm.hidden_at IS NULL ORDER BY cm.position IS NULL, cm.position ASC, last_message_at DESC, c.created_at DESC - `).bind(user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id, user.id).all(); + `).bind(user.id, user.id, user.id, user.id, user.id, user.id).all(); const results = dms.results.map((dm: any) => ({ id: dm.id, @@ -198,7 +175,8 @@ export async function handleListDMs(env: Env, user: User): Promise { position: dm.position, last_read_message_id: dm.last_read_message_id ?? null, notifications: dm.notifications, - has_unread: dm.has_unread, + // Derived rather than computed in SQL: any unread message means unread_count > 0. + has_unread: dm.unread_count > 0 ? 1 : 0, unread_count: dm.unread_count, last_message_content: dm.last_message_content, last_message_at: dm.last_message_at,