core: improve indexes (#6095)

* core: improve indexes

* update plans

* queries

* postgres migration

* optimize query

* lint

* plans

* add indexes

* schema

* plans

* postgres schema

---------

Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
This commit is contained in:
spaced4ndy
2025-07-24 22:36:08 +00:00
committed by GitHub
parent 7b7926a73e
commit d69511bcad
11 changed files with 153 additions and 58 deletions
@@ -12,6 +12,7 @@ import Simplex.Chat.Store.Postgres.Migrations.M20250526_short_links
import Simplex.Chat.Store.Postgres.Migrations.M20250702_contact_requests_remove_cascade_delete
import Simplex.Chat.Store.Postgres.Migrations.M20250704_groups_conn_link_prepared_connection
import Simplex.Chat.Store.Postgres.Migrations.M20250709_profile_short_descr
import Simplex.Chat.Store.Postgres.Migrations.M20250721_indexes
import Simplex.Messaging.Agent.Store.Shared (Migration (..))
schemaMigrations :: [(String, Text, Maybe Text)]
@@ -23,7 +24,8 @@ schemaMigrations =
("20250526_short_links", m20250526_short_links, Just down_m20250526_short_links),
("20250702_contact_requests_remove_cascade_delete", m20250702_contact_requests_remove_cascade_delete, Just down_m20250702_contact_requests_remove_cascade_delete),
("20250704_groups_conn_link_prepared_connection", m20250704_groups_conn_link_prepared_connection, Just down_m20250704_groups_conn_link_prepared_connection),
("20250709_profile_short_descr", m20250709_profile_short_descr, Just down_m20250709_profile_short_descr)
("20250709_profile_short_descr", m20250709_profile_short_descr, Just down_m20250709_profile_short_descr),
("20250721_indexes", m20250721_indexes, Just down_m20250721_indexes)
]
-- | The list of migrations in ascending order by date
@@ -0,0 +1,37 @@
{-# LANGUAGE QuasiQuotes #-}
module Simplex.Chat.Store.Postgres.Migrations.M20250721_indexes where
import Data.Text (Text)
import qualified Data.Text as T
import Text.RawString.QQ (r)
m20250721_indexes :: Text
m20250721_indexes =
T.pack
[r|
DROP INDEX idx_contact_requests_xcontact_id;
CREATE INDEX idx_contact_requests_xcontact_id ON contact_requests(user_id, xcontact_id);
CREATE INDEX idx_chat_items_group_scope_stats_all ON chat_items (
user_id,
group_id,
group_scope_tag,
group_scope_group_member_id,
item_status,
chat_item_id,
user_mention
);
|]
down_m20250721_indexes :: Text
down_m20250721_indexes =
T.pack
[r|
DROP INDEX idx_contact_requests_xcontact_id;
CREATE INDEX idx_contact_requests_xcontact_id ON contact_requests(xcontact_id);
DROP INDEX idx_chat_items_group_scope_stats_all;
|]
@@ -1624,6 +1624,10 @@ CREATE INDEX idx_chat_items_group_scope_item_ts ON test_chat_schema.chat_items U
CREATE INDEX idx_chat_items_group_scope_stats_all ON test_chat_schema.chat_items USING btree (user_id, group_id, group_scope_tag, group_scope_group_member_id, item_status, chat_item_id, user_mention);
CREATE UNIQUE INDEX idx_chat_items_group_shared_msg_id ON test_chat_schema.chat_items USING btree (user_id, group_id, group_member_id, shared_msg_id);
@@ -1784,7 +1788,7 @@ CREATE INDEX idx_contact_requests_user_contact_link_id ON test_chat_schema.conta
CREATE INDEX idx_contact_requests_xcontact_id ON test_chat_schema.contact_requests USING btree (xcontact_id);
CREATE INDEX idx_contact_requests_xcontact_id ON test_chat_schema.contact_requests USING btree (user_id, xcontact_id);