Skip to content

feat(sqlite) Add an index on events.event_id and .room_id #4685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 12 additions & 2 deletions crates/matrix-sdk-sqlite/src/event_cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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(
Expand Down
Loading