From 4f606615ebfdc8dabc568deadeec30c1ee35cd2a Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Fri, 16 Dec 2022 08:16:49 -0600 Subject: [PATCH] perf: indexing improvement --- src/schema.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/schema.rs b/src/schema.rs index 9407ca1..db45ae4 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -20,7 +20,7 @@ pragma mmap_size = 536870912; -- 512MB of mmap "##; /// Latest database version -pub const DB_VERSION: usize = 8; +pub const DB_VERSION: usize = 9; /// Schema definition const INIT_SQL: &str = formatcp!( @@ -49,6 +49,7 @@ content TEXT NOT NULL -- serialized json of event object -- Event Indexes CREATE UNIQUE INDEX IF NOT EXISTS event_hash_index ON event(event_hash); CREATE INDEX IF NOT EXISTS author_index ON event(author); +CREATE INDEX IF NOT EXISTS created_at_index ON event(created_at); CREATE INDEX IF NOT EXISTS delegated_by_index ON event(delegated_by); CREATE INDEX IF NOT EXISTS event_composite_index ON event(kind,created_at); @@ -396,3 +397,23 @@ PRAGMA user_version = 8; } Ok(8) } + +fn mig_8_to_9(conn: &mut PooledConnection) -> Result { + info!("database schema needs update from 8->9"); + // Those old indexes were actually helpful... + let upgrade_sql = r##" +CREATE INDEX IF NOT EXISTS created_at_index ON event(created_at); +CREATE INDEX IF NOT EXISTS event_composite_index ON event(kind,created_at); +PRAGMA user_version = 8; +"##; + match conn.execute_batch(upgrade_sql) { + Ok(()) => { + info!("database schema upgraded v8 -> v9"); + } + Err(err) => { + error!("update failed: {}", err); + panic!("database could not be upgraded"); + } + } + Ok(8) +}