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(