From afd145c979f200b0396cf2e7c032bbc8212d2f11 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:03:40 +0400 Subject: [PATCH] core: improve chat list performance (indexes) (#3728) --- simplex-chat.cabal | 1 + .../Chat/Migrations/M20240122_indexes.hs | 26 +++++++++++++++++++ src/Simplex/Chat/Migrations/chat_schema.sql | 25 ++++++++++++++++++ src/Simplex/Chat/Store/Messages.hs | 9 ++++--- src/Simplex/Chat/Store/Migrations.hs | 4 ++- 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/Simplex/Chat/Migrations/M20240122_indexes.hs diff --git a/simplex-chat.cabal b/simplex-chat.cabal index 97ee2a4fdd..cebe1a11f6 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -132,6 +132,7 @@ library Simplex.Chat.Migrations.M20240102_note_folders Simplex.Chat.Migrations.M20240104_members_profile_update Simplex.Chat.Migrations.M20240115_block_member_for_all + Simplex.Chat.Migrations.M20240122_indexes Simplex.Chat.Mobile Simplex.Chat.Mobile.File Simplex.Chat.Mobile.Shared diff --git a/src/Simplex/Chat/Migrations/M20240122_indexes.hs b/src/Simplex/Chat/Migrations/M20240122_indexes.hs new file mode 100644 index 0000000000..7b708f8bbe --- /dev/null +++ b/src/Simplex/Chat/Migrations/M20240122_indexes.hs @@ -0,0 +1,26 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Chat.Migrations.M20240122_indexes where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20240122_indexes :: Query +m20240122_indexes = + [sql| +CREATE INDEX idx_chat_items_contacts_created_at on chat_items (user_id, contact_id, created_at); +CREATE INDEX idx_chat_items_contacts_item_status on chat_items (user_id, contact_id, item_status); +CREATE INDEX idx_chat_items_groups_item_status on chat_items (user_id, group_id, item_status); +CREATE INDEX idx_chat_items_notes_created_at on chat_items (user_id, note_folder_id, created_at); +CREATE INDEX idx_chat_items_notes_item_status on chat_items (user_id, note_folder_id, item_status); +|] + +down_m20240122_indexes :: Query +down_m20240122_indexes = + [sql| +DROP INDEX idx_chat_items_contacts_created_at; +DROP INDEX idx_chat_items_contacts_item_status; +DROP INDEX idx_chat_items_groups_item_status; +DROP INDEX idx_chat_items_notes_created_at; +DROP INDEX idx_chat_items_notes_item_status; +|] diff --git a/src/Simplex/Chat/Migrations/chat_schema.sql b/src/Simplex/Chat/Migrations/chat_schema.sql index 3a7f28e334..efed6d168a 100644 --- a/src/Simplex/Chat/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Migrations/chat_schema.sql @@ -829,3 +829,28 @@ CREATE INDEX idx_msg_deliveries_agent_msg_id ON "msg_deliveries"( CREATE INDEX chat_items_note_folder_id ON chat_items(note_folder_id); CREATE INDEX files_note_folder_id ON files(note_folder_id); CREATE INDEX note_folders_user_id ON note_folders(user_id); +CREATE INDEX idx_chat_items_contacts_created_at on chat_items( + user_id, + contact_id, + created_at +); +CREATE INDEX idx_chat_items_contacts_item_status on chat_items( + user_id, + contact_id, + item_status +); +CREATE INDEX idx_chat_items_groups_item_status on chat_items( + user_id, + group_id, + item_status +); +CREATE INDEX idx_chat_items_notes_created_at on chat_items( + user_id, + note_folder_id, + created_at +); +CREATE INDEX idx_chat_items_notes_item_status on chat_items( + user_id, + note_folder_id, + item_status +); diff --git a/src/Simplex/Chat/Store/Messages.hs b/src/Simplex/Chat/Store/Messages.hs index 31373c17b8..891e9887e6 100644 --- a/src/Simplex/Chat/Store/Messages.hs +++ b/src/Simplex/Chat/Store/Messages.hs @@ -543,12 +543,13 @@ findDirectChatPreviews_ db User {userId} pagination clq = LEFT JOIN ( SELECT contact_id, chat_item_id, MAX(created_at) FROM chat_items + WHERE user_id = :user_id AND contact_id IS NOT NULL GROUP BY contact_id ) LastItems ON LastItems.contact_id = ct.contact_id LEFT JOIN ( SELECT contact_id, COUNT(1) AS UnreadCount, MIN(chat_item_id) AS MinUnread FROM chat_items - WHERE item_status = :rcv_new + WHERE user_id = :user_id AND contact_id IS NOT NULL AND item_status = :rcv_new GROUP BY contact_id ) ChatStats ON ChatStats.contact_id = ct.contact_id |] @@ -638,12 +639,13 @@ findGroupChatPreviews_ db User {userId} pagination clq = LEFT JOIN ( SELECT group_id, chat_item_id, MAX(item_ts) FROM chat_items + WHERE user_id = :user_id AND group_id IS NOT NULL GROUP BY group_id ) LastItems ON LastItems.group_id = g.group_id LEFT JOIN ( SELECT group_id, COUNT(1) AS UnreadCount, MIN(chat_item_id) AS MinUnread FROM chat_items - WHERE item_status = :rcv_new + WHERE user_id = :user_id AND group_id IS NOT NULL AND item_status = :rcv_new GROUP BY group_id ) ChatStats ON ChatStats.group_id = g.group_id |] @@ -733,12 +735,13 @@ findLocalChatPreviews_ db User {userId} pagination clq = LEFT JOIN ( SELECT note_folder_id, chat_item_id, MAX(created_at) FROM chat_items + WHERE user_id = :user_id AND note_folder_id IS NOT NULL GROUP BY note_folder_id ) LastItems ON LastItems.note_folder_id = nf.note_folder_id LEFT JOIN ( SELECT note_folder_id, COUNT(1) AS UnreadCount, MIN(chat_item_id) AS MinUnread FROM chat_items - WHERE item_status = :rcv_new + WHERE user_id = :user_id AND note_folder_id IS NOT NULL AND item_status = :rcv_new GROUP BY note_folder_id ) ChatStats ON ChatStats.note_folder_id = nf.note_folder_id |] diff --git a/src/Simplex/Chat/Store/Migrations.hs b/src/Simplex/Chat/Store/Migrations.hs index f9513dcc2a..2f5e61a5e6 100644 --- a/src/Simplex/Chat/Store/Migrations.hs +++ b/src/Simplex/Chat/Store/Migrations.hs @@ -97,6 +97,7 @@ import Simplex.Chat.Migrations.M20231215_recreate_msg_deliveries import Simplex.Chat.Migrations.M20240102_note_folders import Simplex.Chat.Migrations.M20240104_members_profile_update import Simplex.Chat.Migrations.M20240115_block_member_for_all +import Simplex.Chat.Migrations.M20240122_indexes import Simplex.Messaging.Agent.Store.SQLite.Migrations (Migration (..)) schemaMigrations :: [(String, Query, Maybe Query)] @@ -193,7 +194,8 @@ schemaMigrations = ("20231215_recreate_msg_deliveries", m20231215_recreate_msg_deliveries, Just down_m20231215_recreate_msg_deliveries), ("20240102_note_folders", m20240102_note_folders, Just down_m20240102_note_folders), ("20240104_members_profile_update", m20240104_members_profile_update, Just down_m20240104_members_profile_update), - ("20240115_block_member_for_all", m20240115_block_member_for_all, Just down_m20240115_block_member_for_all) + ("20240115_block_member_for_all", m20240115_block_member_for_all, Just down_m20240115_block_member_for_all), + ("20240122_indexes", m20240122_indexes, Just down_m20240122_indexes) ] -- | The list of migrations in ascending order by date