From e2b67d20fb1fd7acd4370f371a5db077c03b5718 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Tue, 7 Jul 2026 10:51:49 -0300 Subject: [PATCH] refactor: Remove `GROUP BY c.id` from chatlist queries Not clear why `GROUP BY` was needed -- a chat can't appear in the selected rows multiple times because we do JOIN on the last message in the chat, there can't be multiple last messages. --- src/chatlist.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/chatlist.rs b/src/chatlist.rs index d954b3043..17f20fcc3 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -132,9 +132,7 @@ ORDER BY timestamp DESC, id DESC LIMIT 1)" ) }; } - // - `GROUP BY` is needed several messages may have the same - // timestamp - // - the list starts with the newest chats + // The list starts with the newest chats. // // The query shows messages from blocked contacts in // groups. Otherwise it would be hard to follow conversations. @@ -149,7 +147,6 @@ ORDER BY timestamp DESC, id DESC LIMIT 1)" WHERE c.id>9 AND c.blocked!=1 AND c.id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=? AND add_timestamp >= remove_timestamp) - GROUP BY c.id ORDER BY c.archived=? DESC, IFNULL(NULLIF(m.timestamp,0),c.created_timestamp) DESC, m.id DESC"), (query_contact_id, ChatVisibility::Pinned), process_row, @@ -174,7 +171,6 @@ ORDER BY timestamp DESC, id DESC LIMIT 1)" WHERE c.id>9 AND c.blocked!=1 AND c.archived=1 - GROUP BY c.id ORDER BY IFNULL(NULLIF(m.timestamp,0),c.created_timestamp) DESC, m.id DESC" ), (), @@ -206,7 +202,6 @@ ORDER BY timestamp DESC, id DESC LIMIT 1)" AND c.blocked!=1 AND IFNULL(c.name_normalized,c.name) LIKE ? AND (NOT ? OR EXISTS (SELECT 1 FROM msgs m WHERE m.chat_id = c.id AND m.state == ? AND hidden=0)) - GROUP BY c.id ORDER BY IFNULL(NULLIF(m.timestamp,0),c.created_timestamp) DESC, m.id DESC"), (skip_id, str_like_cmd, only_unread, MessageState::InFresh), process_row, @@ -250,7 +245,6 @@ ORDER BY timestamp DESC, id DESC LIMIT 1)" AND c.blocked=0 AND NOT c.archived=? AND (c.type!=? OR c.id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=? AND add_timestamp >= remove_timestamp)) - GROUP BY c.id ORDER BY c.id=? DESC, c.archived=? DESC, IFNULL(NULLIF(m.timestamp,0),c.created_timestamp) DESC, m.id DESC"), ( skip_id, ChatVisibility::Archived, @@ -271,7 +265,6 @@ ORDER BY timestamp DESC, id DESC LIMIT 1)" WHERE c.id>9 AND c.id!=? AND (c.blocked=0 OR c.blocked=2) AND NOT c.archived=? - GROUP BY c.id ORDER BY c.id=0 DESC, c.archived=? DESC, IFNULL(NULLIF(m.timestamp,0),c.created_timestamp) DESC, m.id DESC"), (skip_id, ChatVisibility::Archived, ChatVisibility::Pinned), process_row,