From 488bc77f9abe8537b48c199eb6dbdadb1cf85ce6 Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Thu, 1 Aug 2024 16:29:25 +0200 Subject: [PATCH 1/2] feat(store): cache sql statements --- crates/store/src/db/sql.rs | 47 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/crates/store/src/db/sql.rs b/crates/store/src/db/sql.rs index 77afe7b87..39dcc6aa0 100644 --- a/crates/store/src/db/sql.rs +++ b/crates/store/src/db/sql.rs @@ -33,7 +33,7 @@ use crate::{ /// /// A vector with accounts, or an error. pub fn select_accounts(conn: &mut Connection) -> Result> { - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT account_id, @@ -61,8 +61,8 @@ pub fn select_accounts(conn: &mut Connection) -> Result> { /// /// The vector with the account id and corresponding hash, or an error. pub fn select_account_hashes(conn: &mut Connection) -> Result> { - let mut stmt = - conn.prepare("SELECT account_id, account_hash FROM accounts ORDER BY block_num ASC;")?; + let mut stmt = conn + .prepare_cached("SELECT account_id, account_hash FROM accounts ORDER BY block_num ASC;")?; let mut rows = stmt.query([])?; let mut result = Vec::new(); @@ -91,7 +91,7 @@ pub fn select_accounts_by_block_range( ) -> Result> { let account_ids: Vec = account_ids.iter().copied().map(u64_to_value).collect(); - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT account_id, @@ -124,7 +124,7 @@ pub fn select_accounts_by_block_range( /// /// The latest account details, or an error. pub fn select_account(conn: &mut Connection, account_id: AccountId) -> Result { - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT account_id, @@ -159,7 +159,7 @@ pub fn select_account_deltas( block_start: BlockNumber, block_end: BlockNumber, ) -> Result> { - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT delta @@ -196,14 +196,14 @@ pub fn upsert_accounts( accounts: &[BlockAccountUpdate], block_num: BlockNumber, ) -> Result { - let mut upsert_stmt = transaction.prepare( + let mut upsert_stmt = transaction.prepare_cached( "INSERT OR REPLACE INTO accounts (account_id, account_hash, block_num, details) VALUES (?1, ?2, ?3, ?4);", )?; - let mut insert_delta_stmt = transaction.prepare( + let mut insert_delta_stmt = transaction.prepare_cached( "INSERT INTO account_deltas (account_id, block_num, delta) VALUES (?1, ?2, ?3);", )?; let mut select_details_stmt = - transaction.prepare("SELECT details FROM accounts WHERE account_id = ?1;")?; + transaction.prepare_cached("SELECT details FROM accounts WHERE account_id = ?1;")?; let mut count = 0; for update in accounts.iter() { @@ -274,7 +274,7 @@ pub fn insert_nullifiers_for_block( nullifiers: &[Nullifier], block_num: BlockNumber, ) -> Result { - let mut stmt = transaction.prepare( + let mut stmt = transaction.prepare_cached( "INSERT INTO nullifiers (nullifier, nullifier_prefix, block_num) VALUES (?1, ?2, ?3);", )?; @@ -293,7 +293,7 @@ pub fn insert_nullifiers_for_block( /// A vector with nullifiers and the block height at which they were created, or an error. pub fn select_nullifiers(conn: &mut Connection) -> Result> { let mut stmt = - conn.prepare("SELECT nullifier, block_num FROM nullifiers ORDER BY block_num ASC;")?; + conn.prepare_cached("SELECT nullifier, block_num FROM nullifiers ORDER BY block_num ASC;")?; let mut rows = stmt.query([])?; let mut result = vec![]; @@ -325,7 +325,7 @@ pub fn select_nullifiers_by_block_range( let nullifier_prefixes: Vec = nullifier_prefixes.iter().copied().map(u32_to_value).collect(); - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT nullifier, @@ -374,7 +374,7 @@ pub fn select_nullifiers_by_prefix( let nullifier_prefixes: Vec = nullifier_prefixes.iter().copied().map(u32_to_value).collect(); - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT nullifier, @@ -410,7 +410,7 @@ pub fn select_nullifiers_by_prefix( /// /// A vector with notes, or an error. pub fn select_notes(conn: &mut Connection) -> Result> { - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT block_num, @@ -473,7 +473,7 @@ pub fn select_notes(conn: &mut Connection) -> Result> { /// The [Transaction] object is not consumed. It's up to the caller to commit or rollback the /// transaction. pub fn insert_notes(transaction: &Transaction, notes: &[NoteRecord]) -> Result { - let mut stmt = transaction.prepare( + let mut stmt = transaction.prepare_cached( " INSERT INTO notes @@ -536,7 +536,7 @@ pub fn select_notes_since_block_by_tag_and_sender( let tags: Vec = tags.iter().copied().map(u32_to_value).collect(); let account_ids: Vec = account_ids.iter().copied().map(u64_to_value).collect(); - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT block_num, @@ -613,7 +613,7 @@ pub fn select_notes_since_block_by_tag_and_sender( pub fn select_notes_by_id(conn: &mut Connection, note_ids: &[NoteId]) -> Result> { let note_ids: Vec = note_ids.iter().map(|id| id.to_bytes().into()).collect(); - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT block_num, @@ -680,7 +680,7 @@ pub fn select_notes_by_id(conn: &mut Connection, note_ids: &[NoteId]) -> Result< /// transaction. pub fn insert_block_header(transaction: &Transaction, block_header: &BlockHeader) -> Result { let mut stmt = transaction - .prepare("INSERT INTO block_headers (block_num, block_header) VALUES (?1, ?2);")?; + .prepare_cached("INSERT INTO block_headers (block_num, block_header) VALUES (?1, ?2);")?; Ok(stmt.execute(params![block_header.block_num(), block_header.to_bytes()])?) } @@ -697,11 +697,12 @@ pub fn select_block_header_by_block_num( let mut stmt; let mut rows = match block_number { Some(block_number) => { - stmt = conn.prepare("SELECT block_header FROM block_headers WHERE block_num = ?1")?; + stmt = + conn.prepare_cached("SELECT block_header FROM block_headers WHERE block_num = ?1")?; stmt.query([block_number])? }, None => { - stmt = conn.prepare( + stmt = conn.prepare_cached( "SELECT block_header FROM block_headers ORDER BY block_num DESC LIMIT 1", )?; stmt.query([])? @@ -724,7 +725,7 @@ pub fn select_block_header_by_block_num( /// A vector of [BlockHeader] or an error. pub fn select_block_headers(conn: &mut Connection) -> Result> { let mut stmt = - conn.prepare("SELECT block_header FROM block_headers ORDER BY block_num ASC;")?; + conn.prepare_cached("SELECT block_header FROM block_headers ORDER BY block_num ASC;")?; let mut rows = stmt.query([])?; let mut result = vec![]; while let Some(row) = rows.next()? { @@ -754,7 +755,7 @@ pub fn insert_transactions( block_num: BlockNumber, accounts: &[BlockAccountUpdate], ) -> Result { - let mut stmt = transaction.prepare( + let mut stmt = transaction.prepare_cached( "INSERT INTO transactions (transaction_id, account_id, block_num) VALUES (?1, ?2, ?3);", )?; let mut count = 0; @@ -785,7 +786,7 @@ pub fn select_transactions_by_accounts_and_block_range( ) -> Result> { let account_ids: Vec = account_ids.iter().copied().map(u64_to_value).collect(); - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( " SELECT account_id, From eeb086f77416ade558682f4f199e54a702224a25 Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Thu, 1 Aug 2024 16:49:14 +0200 Subject: [PATCH 2/2] feat(store): increase statement cache size to 32 --- CHANGELOG.md | 1 + crates/store/src/db/mod.rs | 7 ++++++- crates/store/src/lib.rs | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a9f57113..fa9233d60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Added `GetAccountStateDelta` endpoint (#418). - Added `CheckNullifiersByPrefix` endpoint (#419). - Support multiple inflight transactions on the same account (#407). +- Cache sql statements (#427). ### Fixes diff --git a/crates/store/src/db/mod.rs b/crates/store/src/db/mod.rs index ac9b5ef1e..d35ebc6e0 100644 --- a/crates/store/src/db/mod.rs +++ b/crates/store/src/db/mod.rs @@ -29,7 +29,7 @@ use crate::{ errors::{DatabaseError, DatabaseSetupError, GenesisError, StateSyncError}, genesis::GenesisState, types::{AccountId, BlockNumber}, - COMPONENT, + COMPONENT, SQL_STATEMENT_CACHE_CAPACITY, }; mod migrations; @@ -118,6 +118,11 @@ impl Db { // queries we want to run array::load_module(conn)?; + // Increase the statement cache size. + conn.set_prepared_statement_cache_capacity( + SQL_STATEMENT_CACHE_CAPACITY, + ); + // Enable the WAL mode. This allows concurrent reads while the // transaction is being written, this is required for proper // synchronization of the servers in-memory and on-disk representations diff --git a/crates/store/src/lib.rs b/crates/store/src/lib.rs index dd9a3e605..95767833e 100644 --- a/crates/store/src/lib.rs +++ b/crates/store/src/lib.rs @@ -11,3 +11,6 @@ pub mod types; // CONSTANTS // ================================================================================================= pub const COMPONENT: &str = "miden-store"; + +/// Number of sql statements that each connection will cache. +const SQL_STATEMENT_CACHE_CAPACITY: usize = 32;