From ce64c91d5a6e4cbc6cfae6b4a5b312224931f10c Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 10 May 2023 15:17:26 +0400 Subject: [PATCH] add indexes for reading pending files (#751) --- simplexmq.cabal | 1 + .../Agent/Store/SQLite/Migrations.hs | 4 +++- ...20230510_files_pending_replicas_indexes.hs | 22 +++++++++++++++++++ .../Store/SQLite/Migrations/agent_schema.sql | 11 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20230510_files_pending_replicas_indexes.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index 8afbf6e16..378338cf2 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -80,6 +80,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230223_files Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230320_retry_state Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230401_snd_files + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230510_files_pending_replicas_indexes Simplex.Messaging.Agent.TAsyncs Simplex.Messaging.Agent.TRcvQueues Simplex.Messaging.Client diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs index 14419e85f..a06230441 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs @@ -58,6 +58,7 @@ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230217_server_key_hash import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230223_files import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230320_retry_state import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230401_snd_files +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230510_files_pending_replicas_indexes import Simplex.Messaging.Encoding.String import Simplex.Messaging.Parsers (dropPrefix, sumTypeJSON) import Simplex.Messaging.Transport.Client (TransportHost) @@ -82,7 +83,8 @@ schemaMigrations = ("m20230217_server_key_hash", m20230217_server_key_hash, Nothing), ("m20230223_files", m20230223_files, Just down_m20230223_files), ("m20230320_retry_state", m20230320_retry_state, Just down_m20230320_retry_state), - ("m20230401_snd_files", m20230401_snd_files, Just down_m20230401_snd_files) + ("m20230401_snd_files", m20230401_snd_files, Just down_m20230401_snd_files), + ("m20230510_files_pending_replicas_indexes", m20230510_files_pending_replicas_indexes, Just down_m20230510_files_pending_replicas_indexes) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20230510_files_pending_replicas_indexes.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20230510_files_pending_replicas_indexes.hs new file mode 100644 index 000000000..6c7b93cbc --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20230510_files_pending_replicas_indexes.hs @@ -0,0 +1,22 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20230510_files_pending_replicas_indexes where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20230510_files_pending_replicas_indexes :: Query +m20230510_files_pending_replicas_indexes = + [sql| +CREATE INDEX idx_rcv_file_chunk_replicas_pending ON rcv_file_chunk_replicas(received, replica_number); +CREATE INDEX idx_snd_file_chunk_replicas_pending ON snd_file_chunk_replicas(replica_status, replica_number); +CREATE INDEX idx_deleted_snd_chunk_replicas_pending ON deleted_snd_chunk_replicas(created_at); +|] + +down_m20230510_files_pending_replicas_indexes :: Query +down_m20230510_files_pending_replicas_indexes = + [sql| +DROP INDEX idx_deleted_snd_chunk_replicas_pending; +DROP INDEX idx_snd_file_chunk_replicas_pending; +DROP INDEX idx_rcv_file_chunk_replicas_pending; +|] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 9caa8eb21..18829f716 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -421,3 +421,14 @@ CREATE INDEX idx_deleted_snd_chunk_replicas_user_id ON deleted_snd_chunk_replica CREATE INDEX idx_deleted_snd_chunk_replicas_xftp_server_id ON deleted_snd_chunk_replicas( xftp_server_id ); +CREATE INDEX idx_rcv_file_chunk_replicas_pending ON rcv_file_chunk_replicas( + received, + replica_number +); +CREATE INDEX idx_snd_file_chunk_replicas_pending ON snd_file_chunk_replicas( + replica_status, + replica_number +); +CREATE INDEX idx_deleted_snd_chunk_replicas_pending ON deleted_snd_chunk_replicas( + created_at +);