From c48e45686dc82fcf3105f7d9098c75a13410af7b Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Wed, 14 Dec 2022 22:27:32 -0600 Subject: [PATCH] perf: schema updates for better event indexing --- src/schema.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/schema.rs b/src/schema.rs index 20e446c..9407ca1 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 = 7; +pub const DB_VERSION: usize = 8; /// Schema definition const INIT_SQL: &str = formatcp!( @@ -48,11 +48,9 @@ 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 created_at_index ON event(created_at); CREATE INDEX IF NOT EXISTS author_index ON event(author); CREATE INDEX IF NOT EXISTS delegated_by_index ON event(delegated_by); -CREATE INDEX IF NOT EXISTS kind_index ON event(kind); -CREATE INDEX IF NOT EXISTS kind_created_index ON event(kind,created_at); +CREATE INDEX IF NOT EXISTS event_composite_index ON event(kind,created_at); -- Tag Table -- Tag values are stored as either a BLOB (if they come in as a @@ -158,6 +156,9 @@ pub fn upgrade_db(conn: &mut PooledConnection) -> Result<()> { if curr_version == 6 { curr_version = mig_6_to_7(conn)?; } + if curr_version == 7 { + curr_version = mig_7_to_8(conn)?; + } if curr_version == DB_VERSION { info!( "All migration scripts completed successfully. Welcome to v{}.", @@ -374,3 +375,24 @@ PRAGMA user_version = 7; } Ok(7) } + +fn mig_7_to_8(conn: &mut PooledConnection) -> Result { + info!("database schema needs update from 7->8"); + // Remove redundant indexes, and add a better multi-column index. + let upgrade_sql = r##" +DROP INDEX IF EXISTS created_at_index; +DROP INDEX IF EXISTS kind_index; +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 v7 -> v8"); + } + Err(err) => { + error!("update failed: {}", err); + panic!("database could not be upgraded"); + } + } + Ok(8) +}