From 6c57003d172fdf2bd5595075701cf0d2e85b32e4 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 18 Feb 2025 16:48:17 +0100 Subject: [PATCH] feat(sqlite) Add an index on `events.event_id` and `.room_id`. This patch adds an index on `events.event_id` and on `events.room_id` so that queries on this column are faster. It mostly happens for the `Deduplicator`, which runs for every backwards pagination or sync. This patch also updates the query in `filter_duplicated_events` to sort event by their `chunk_id` and `position` so that the results are constant, it helps when testing. --- .../005_events_index_on_event_id.sql | 2 ++ crates/matrix-sdk-sqlite/src/event_cache_store.rs | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 crates/matrix-sdk-sqlite/migrations/event_cache_store/005_events_index_on_event_id.sql diff --git a/crates/matrix-sdk-sqlite/migrations/event_cache_store/005_events_index_on_event_id.sql b/crates/matrix-sdk-sqlite/migrations/event_cache_store/005_events_index_on_event_id.sql new file mode 100644 index 000000000..871ce97a4 --- /dev/null +++ b/crates/matrix-sdk-sqlite/migrations/event_cache_store/005_events_index_on_event_id.sql @@ -0,0 +1,2 @@ +-- Create a unique index on `events.event_id` and `events.room_id` . +CREATE UNIQUE INDEX "linked_chunks_event_id_and_room_id" ON events (event_id, room_id); diff --git a/crates/matrix-sdk-sqlite/src/event_cache_store.rs b/crates/matrix-sdk-sqlite/src/event_cache_store.rs index 87e2a3f94..73498e107 100644 --- a/crates/matrix-sdk-sqlite/src/event_cache_store.rs +++ b/crates/matrix-sdk-sqlite/src/event_cache_store.rs @@ -62,7 +62,7 @@ mod keys { /// This is used to figure whether the SQLite database requires a migration. /// Every new SQL migration should imply a bump of this number, and changes in /// the [`run_migrations`] function. -const DATABASE_VERSION: u8 = 4; +const DATABASE_VERSION: u8 = 5; /// The string used to identify a chunk of type events, in the `type` field in /// the database. @@ -339,6 +339,16 @@ async fn run_migrations(conn: &SqliteAsyncConn, version: u8) -> Result<()> { .await?; } + if version < 5 { + conn.with_transaction(|txn| { + txn.execute_batch(include_str!( + "../migrations/event_cache_store/005_events_index_on_event_id.sql" + ))?; + txn.set_db_version(5) + }) + .await?; + } + Ok(()) } @@ -787,7 +797,7 @@ impl EventCacheStore for SqliteEventCacheStore { .with_transaction(move |txn| -> Result<_> { txn.chunk_large_query_over(events, None, move |txn, events| { let query = format!( - "SELECT event_id FROM events WHERE room_id = ? AND event_id IN ({})", + "SELECT event_id FROM events WHERE room_id = ? AND event_id IN ({}) ORDER BY chunk_id ASC, position ASC", repeat_vars(events.len()), ); let parameters = params_from_iter(