core: improve chat list performance (indexes) (#3728)
This commit is contained in:
parent
97fbf2b7fe
commit
afd145c979
@ -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
|
||||
|
26
src/Simplex/Chat/Migrations/M20240122_indexes.hs
Normal file
26
src/Simplex/Chat/Migrations/M20240122_indexes.hs
Normal file
@ -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;
|
||||
|]
|
@ -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
|
||||
);
|
||||
|
@ -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
|
||||
|]
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user