sqlite optimizations: transactions, journal_mode, synchronous.

This commit is contained in:
Enno Rehling 2017-11-11 21:46:16 +01:00
parent 79b397e07f
commit 48a58fc907
1 changed files with 31 additions and 1 deletions

View File

@ -16,12 +16,20 @@ static sqlite3 *g_db;
static sqlite3_stmt * g_stmt_insert; static sqlite3_stmt * g_stmt_insert;
static sqlite3_stmt * g_stmt_select; static sqlite3_stmt * g_stmt_select;
static int g_order_batchsize;
static int g_order_tx_size;
order_data *db_load_order(int id) order_data *db_load_order(int id)
{ {
if (id > 0) { if (id > 0) {
order_data * od = NULL; order_data * od = NULL;
int err; int err;
if (g_order_tx_size > 0) {
g_order_tx_size = 0;
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
err = sqlite3_reset(g_stmt_select); err = sqlite3_reset(g_stmt_select);
assert(err == SQLITE_OK); assert(err == SQLITE_OK);
err = sqlite3_bind_int(g_stmt_select, 1, id); err = sqlite3_bind_int(g_stmt_select, 1, id);
@ -48,6 +56,14 @@ int db_save_order(order_data *od)
if (od->_str) { if (od->_str) {
int err; int err;
sqlite3_int64 id; sqlite3_int64 id;
if (g_order_batchsize > 0) {
if (g_order_tx_size == 0) {
err = sqlite3_exec(g_db, "BEGIN TRANSACTION", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
}
err = sqlite3_reset(g_stmt_insert); err = sqlite3_reset(g_stmt_insert);
assert(err == SQLITE_OK); assert(err == SQLITE_OK);
err = sqlite3_bind_text(g_stmt_insert, 1, od->_str, -1, SQLITE_STATIC); err = sqlite3_bind_text(g_stmt_insert, 1, od->_str, -1, SQLITE_STATIC);
@ -56,6 +72,15 @@ int db_save_order(order_data *od)
assert(err == SQLITE_DONE); assert(err == SQLITE_DONE);
id = sqlite3_last_insert_rowid(g_db); id = sqlite3_last_insert_rowid(g_db);
assert(id <= INT_MAX); assert(id <= INT_MAX);
if (g_order_batchsize > 0) {
if (++g_order_tx_size >= g_order_batchsize) {
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
g_order_tx_size = 0;
}
}
return (int)id; return (int)id;
} }
return 0; return 0;
@ -66,12 +91,17 @@ void db_open(void)
int err; int err;
const char *dbname; const char *dbname;
dbname = config_get("config.dbname"); g_order_batchsize = config_get_int("config.db_batch", 100);
dbname = config_get("config.db_name");
if (!dbname) { if (!dbname) {
dbname = ""; dbname = "";
} }
err = sqlite3_open(dbname, &g_db); err = sqlite3_open(dbname, &g_db);
assert(err == SQLITE_OK); assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA journal_mode=OFF", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA synchronous=OFF", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, data TEXT NOT NULL)", NULL, NULL, NULL); err = sqlite3_exec(g_db, "CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, data TEXT NOT NULL)", NULL, NULL, NULL);
assert(err == SQLITE_OK); assert(err == SQLITE_OK);
err = sqlite3_prepare_v2(g_db, "INSERT INTO orders (data) VALUES (?)", -1, &g_stmt_insert, NULL); err = sqlite3_prepare_v2(g_db, "INSERT INTO orders (data) VALUES (?)", -1, &g_stmt_insert, NULL);