Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): cache sql statements #427

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion crates/store/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
errors::{DatabaseError, DatabaseSetupError, GenesisError, StateSyncError},
genesis::GenesisState,
types::{AccountId, BlockNumber},
COMPONENT,
COMPONENT, SQL_STATEMENT_CACHE_CAPACITY,
};

mod migrations;
Expand Down Expand Up @@ -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
Expand Down
47 changes: 24 additions & 23 deletions crates/store/src/db/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
///
/// A vector with accounts, or an error.
pub fn select_accounts(conn: &mut Connection) -> Result<Vec<AccountInfo>> {
let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
account_id,
Expand Down Expand Up @@ -61,8 +61,8 @@ pub fn select_accounts(conn: &mut Connection) -> Result<Vec<AccountInfo>> {
///
/// The vector with the account id and corresponding hash, or an error.
pub fn select_account_hashes(conn: &mut Connection) -> Result<Vec<(AccountId, RpoDigest)>> {
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();
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn select_accounts_by_block_range(
) -> Result<Vec<AccountSummary>> {
let account_ids: Vec<Value> = account_ids.iter().copied().map(u64_to_value).collect();

let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
account_id,
Expand Down Expand Up @@ -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<AccountInfo> {
let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
account_id,
Expand Down Expand Up @@ -159,7 +159,7 @@ pub fn select_account_deltas(
block_start: BlockNumber,
block_end: BlockNumber,
) -> Result<Vec<AccountDelta>> {
let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
delta
Expand Down Expand Up @@ -196,14 +196,14 @@ pub fn upsert_accounts(
accounts: &[BlockAccountUpdate],
block_num: BlockNumber,
) -> Result<usize> {
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() {
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn insert_nullifiers_for_block(
nullifiers: &[Nullifier],
block_num: BlockNumber,
) -> Result<usize> {
let mut stmt = transaction.prepare(
let mut stmt = transaction.prepare_cached(
"INSERT INTO nullifiers (nullifier, nullifier_prefix, block_num) VALUES (?1, ?2, ?3);",
)?;

Expand All @@ -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<Vec<(Nullifier, BlockNumber)>> {
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![];
Expand Down Expand Up @@ -325,7 +325,7 @@ pub fn select_nullifiers_by_block_range(
let nullifier_prefixes: Vec<Value> =
nullifier_prefixes.iter().copied().map(u32_to_value).collect();

let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
nullifier,
Expand Down Expand Up @@ -374,7 +374,7 @@ pub fn select_nullifiers_by_prefix(
let nullifier_prefixes: Vec<Value> =
nullifier_prefixes.iter().copied().map(u32_to_value).collect();

let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
nullifier,
Expand Down Expand Up @@ -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<Vec<NoteRecord>> {
let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
block_num,
Expand Down Expand Up @@ -473,7 +473,7 @@ pub fn select_notes(conn: &mut Connection) -> Result<Vec<NoteRecord>> {
/// 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<usize> {
let mut stmt = transaction.prepare(
let mut stmt = transaction.prepare_cached(
"
INSERT INTO
notes
Expand Down Expand Up @@ -536,7 +536,7 @@ pub fn select_notes_since_block_by_tag_and_sender(
let tags: Vec<Value> = tags.iter().copied().map(u32_to_value).collect();
let account_ids: Vec<Value> = account_ids.iter().copied().map(u64_to_value).collect();

let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
block_num,
Expand Down Expand Up @@ -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<Vec<NoteRecord>> {
let note_ids: Vec<Value> = note_ids.iter().map(|id| id.to_bytes().into()).collect();

let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
block_num,
Expand Down Expand Up @@ -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<usize> {
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()])?)
}

Expand All @@ -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([])?
Expand All @@ -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<Vec<BlockHeader>> {
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()? {
Expand Down Expand Up @@ -754,7 +755,7 @@ pub fn insert_transactions(
block_num: BlockNumber,
accounts: &[BlockAccountUpdate],
) -> Result<usize> {
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;
Expand Down Expand Up @@ -785,7 +786,7 @@ pub fn select_transactions_by_accounts_and_block_range(
) -> Result<Vec<TransactionSummary>> {
let account_ids: Vec<Value> = account_ids.iter().copied().map(u64_to_value).collect();

let mut stmt = conn.prepare(
let mut stmt = conn.prepare_cached(
"
SELECT
account_id,
Expand Down
3 changes: 3 additions & 0 deletions crates/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading