My C application uses SQLite 3.36. I am trying to move journal mode from OFF to WAL. But insertion in WAL takes 4 times longer. In the empty table, If insertion of N records taking 7 secs in Journal Mode OFF, in WAL insertion of N records takes ~27 secs. I was expecting faster insertion in WAL mode.
My application contains the structure of the record in an array/list and insertion is done inside a loop. In existing code sqlite3_prepare_v2 is inside the loop.
Below is the sample code -
int get_stmt_id() {
//Based on application logic return id of the SQL statement.
}
sqlite3_stmt prepare_statment(int stmtid){
//1. Search stmtid in the prepared statement array.If found return stmt.
//2. If not found prepare a statement and add it to the prepared statement array. Return newly prepared stmt.
}
int insert_nodes_to_table(Node* node){
...
...
stmtid = get_stmt_id();
stmt = prepare_statment(stmtid);
sqlite3_prepare_v2(..,...,stmt,....);
sqlite3_bind_int(.....);
..
..
sqlite3_bind_text(....);
..
...
status = sqlite3_step(stmt);
...
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
}
sqlite3_exec(db_write, "BEGIN TRANSACTION", NULL,NULL, NULL)
for (node = head; node != NULL; node = inode->next) {
insert_nodes_to_table(node);
....
....
}
sqlite3_exec(db_write, "END TRANSACTION", NULL,NULL, NULL)
Please help me understand, Is the prepared statement inside loop causing the slow insertion? If yes, why are insertions fast in journal mode OFF/Normal and not in WAL mode with same code ?