mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-11-28 13:09:10 -05:00
address database upgrade slowness (#107)
* give a rule of thumb for update time in the documentation * log the SQLite3 version, which can affect performance * do the vacuum in non-WAL mode, to correctly set the page size and to avoid very slow behavior on older SQLite3 versions. Larger page sizes are generally faster (including subsequent vacuum operations). This won't help much for the first vacuum after this change, but it will help afterward. * likewise, set the page size properly on "moonfire-nvr init".
This commit is contained in:
@@ -51,9 +51,15 @@ pub fn run(args: &Args) -> Result<(), Error> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Use WAL mode (which is the most efficient way to preserve database integrity) with a large
|
||||
// page size (so reading large recording_playback rows doesn't require as many seeks). Changing
|
||||
// the page size requires doing a vacuum in non-WAL mode. This will be cheap on an empty
|
||||
// database. https://www.sqlite.org/pragma.html#pragma_page_size
|
||||
conn.execute_batch(r#"
|
||||
pragma journal_mode = wal;
|
||||
pragma journal_mode = delete;
|
||||
pragma page_size = 16384;
|
||||
vacuum;
|
||||
pragma journal_mode = wal;
|
||||
"#)?;
|
||||
db::init(&mut conn)?;
|
||||
info!("Database initialized.");
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
use db::dir;
|
||||
use failure::{Error, Fail};
|
||||
use log::info;
|
||||
use nix::fcntl::FlockArg;
|
||||
use rusqlite;
|
||||
use std::path::Path;
|
||||
@@ -43,7 +44,7 @@ pub mod sql;
|
||||
pub mod ts;
|
||||
pub mod upgrade;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
enum OpenMode {
|
||||
ReadOnly,
|
||||
ReadWrite,
|
||||
@@ -71,8 +72,11 @@ fn open_dir(db_dir: &Path, mode: OpenMode) -> Result<dir::Fd, Error> {
|
||||
/// The returned `dir::Fd` holds the lock and should be kept open as long as the `Connection` is.
|
||||
fn open_conn(db_dir: &Path, mode: OpenMode) -> Result<(dir::Fd, rusqlite::Connection), Error> {
|
||||
let dir = open_dir(db_dir, mode)?;
|
||||
let db_path = db_dir.join("db");
|
||||
info!("Opening {} in {:?} mode with SQLite version {}",
|
||||
db_path.display(), mode, rusqlite::version());
|
||||
let conn = rusqlite::Connection::open_with_flags(
|
||||
db_dir.join("db"),
|
||||
db_path,
|
||||
match mode {
|
||||
OpenMode::ReadOnly => rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
|
||||
OpenMode::ReadWrite => rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE,
|
||||
|
||||
Reference in New Issue
Block a user