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.
This commit is contained in:
Ivan Enderlin
2025-02-18 16:48:17 +01:00
parent 2eb2ae7959
commit 6c57003d17
2 changed files with 14 additions and 2 deletions

View File

@@ -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);

View File

@@ -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(