From fddbf321bc22a1ec3b3397e8f24f9942d6cbc051 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sun, 15 Jan 2023 10:52:49 -0600 Subject: [PATCH] perf: add indexes and force their use (authors) --- src/db.rs | 15 +++++++++++++-- src/schema.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index 5cdfd92..580c026 100644 --- a/src/db.rs +++ b/src/db.rs @@ -524,8 +524,19 @@ fn override_index(f: &ReqFilter) -> Option { } } // if there is an author, it is much better to force the authors index. - if let Some(ks) = &f.authors { - return Some("author_index".into()); + if let Some(_) = &f.authors { + if f.since.is_none() && f.until.is_none() { + if f.kinds.is_none() { + // with no use of kinds/created_at, just author + return Some("author_index".into()); + } else { + // prefer author_kind if there are kinds + return Some("author_kind_index".into()); + } + } else { + // finally, prefer author_created_at if time is provided + return Some("author_created_at_index".into()); + } } None } diff --git a/src/schema.rs b/src/schema.rs index b41523d..f4b6990 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -20,7 +20,7 @@ pragma mmap_size = 17179869184; -- cap mmap at 16GB "##; /// Latest database version -pub const DB_VERSION: usize = 14; +pub const DB_VERSION: usize = 15; /// Schema definition const INIT_SQL: &str = formatcp!( @@ -56,6 +56,8 @@ 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); CREATE INDEX IF NOT EXISTS kind_author_index ON event(kind,author); CREATE INDEX IF NOT EXISTS kind_created_at_index ON event(kind,created_at); +CREATE INDEX IF NOT EXISTS author_created_at_index ON event(author,created_at); +CREATE INDEX IF NOT EXISTS author_kind_index ON event(author,kind); -- Tag Table -- Tag values are stored as either a BLOB (if they come in as a @@ -158,19 +160,15 @@ pub fn upgrade_db(conn: &mut PooledConnection) -> Result<()> { if curr_version == 1 { curr_version = mig_1_to_2(conn)?; } - if curr_version == 2 { curr_version = mig_2_to_3(conn)?; } - if curr_version == 3 { curr_version = mig_3_to_4(conn)?; } - if curr_version == 4 { curr_version = mig_4_to_5(conn)?; } - if curr_version == 5 { curr_version = mig_5_to_6(conn)?; } @@ -198,6 +196,9 @@ pub fn upgrade_db(conn: &mut PooledConnection) -> Result<()> { if curr_version == 13 { curr_version = mig_13_to_14(conn)?; } + if curr_version == 14 { + curr_version = mig_14_to_15(conn)?; + } if curr_version == DB_VERSION { info!( @@ -619,3 +620,23 @@ PRAGMA user_version = 14; } Ok(14) } + +fn mig_14_to_15(conn: &mut PooledConnection) -> Result { + info!("database schema needs update from 14->15"); + let upgrade_sql = r##" +CREATE INDEX IF NOT EXISTS author_created_at_index ON event(author,created_at); +CREATE INDEX IF NOT EXISTS author_kind_index ON event(author,kind); +pragma optimize; +PRAGMA user_version = 15; +"##; + match conn.execute_batch(upgrade_sql) { + Ok(()) => { + info!("database schema upgraded v14 -> v15"); + } + Err(err) => { + error!("update failed: {}", err); + panic!("database could not be upgraded"); + } + } + Ok(15) +}