From 09a68510f2189bf1568b019c4ebf972fe1a931df Mon Sep 17 00:00:00 2001 From: Avi Weinstock Date: Mon, 28 Jun 2021 15:50:58 -0400 Subject: [PATCH] Use SQL transactions in world_block_statistics to try to speed it up. --- world/examples/world_block_statistics.rs | 43 +++++++++++++++--------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/world/examples/world_block_statistics.rs b/world/examples/world_block_statistics.rs index af5fed39db..dbd4d2948c 100644 --- a/world/examples/world_block_statistics.rs +++ b/world/examples/world_block_statistics.rs @@ -8,7 +8,7 @@ use rayon::{ iter::{IntoParallelIterator, ParallelIterator}, ThreadPoolBuilder, }; -use rusqlite::{Connection, ToSql, NO_PARAMS}; +use rusqlite::{Connection, ToSql, Transaction, TransactionBehavior, NO_PARAMS}; use std::{ collections::{HashMap, HashSet}, error::Error, @@ -134,22 +134,24 @@ fn main() -> Result<(), Box> { } }); }); - #[rustfmt::skip] - let mut insert_block = conn.prepare(" - REPLACE INTO block (xcoord, ycoord, kind, r, g, b, quantity) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) - ")?; - #[rustfmt::skip] - let mut insert_sprite = conn.prepare(" - REPLACE INTO sprite (xcoord, ycoord, kind, quantity) - VALUES (?1, ?2, ?3, ?4) - ")?; - #[rustfmt::skip] - let mut insert_chunk = conn.prepare(" - REPLACE INTO chunk (xcoord, ycoord, height, start_time, end_time) - VALUES (?1, ?2, ?3, ?4, ?5) - ")?; + let mut tx = Transaction::new_unchecked(&conn, TransactionBehavior::Deferred)?; + let mut i = 0; while let Ok((x, y, height, start_time, end_time, block_counts, sprite_counts)) = rx.recv() { + #[rustfmt::skip] + let mut insert_block = tx.prepare_cached(" + REPLACE INTO block (xcoord, ycoord, kind, r, g, b, quantity) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) + ")?; + #[rustfmt::skip] + let mut insert_sprite = tx.prepare_cached(" + REPLACE INTO sprite (xcoord, ycoord, kind, quantity) + VALUES (?1, ?2, ?3, ?4) + ")?; + #[rustfmt::skip] + let mut insert_chunk = tx.prepare_cached(" + REPLACE INTO chunk (xcoord, ycoord, height, start_time, end_time) + VALUES (?1, ?2, ?3, ?4, ?5) + ")?; println!("Inserting results for chunk at ({}, {})", x, y); for ((kind, color), count) in block_counts.iter() { insert_block.execute(&[ @@ -168,6 +170,15 @@ fn main() -> Result<(), Box> { let start_time = start_time.duration_since(UNIX_EPOCH)?.as_secs_f64(); let end_time = end_time.duration_since(UNIX_EPOCH)?.as_secs_f64(); insert_chunk.execute(&[&x as &dyn ToSql, &y, &height, &start_time, &end_time])?; + if i % 32 == 0 { + println!("Committing last 32 chunks"); + drop(insert_block); + drop(insert_sprite); + drop(insert_chunk); + tx.commit()?; + tx = Transaction::new_unchecked(&conn, TransactionBehavior::Deferred)?; + } + i += 1; } Ok(()) }