Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve a bottleneck on sqlite3?

Tags:

sqlite

I'm working on a project written in C that generates almost 350k sequences to be persisted on a sqlite database. For each sequence I have to insert (or ignore) a string in a table and update a row in other table.

I tried this "guide" but couldn't reach more than 30k operations per second.

I'm using transactions of 1M operations each (inserts and updates) and PRAGMA synchronous=OFF

What options do I have to solve this bottleneck?

like image 207
Pedro Alves Avatar asked Sep 06 '25 03:09

Pedro Alves


1 Answers

Actually, SQLite will easily do 50,000 or more INSERT statements per second on an average desktop computer. But it will only do a few dozen transactions per second. Transaction speed is limited by the rotational speed of your disk drive. A transaction normally requires two complete rotations of the disk platter, which on a 7200RPM disk drive limits you to about 60 transactions per second.

Transaction speed is limited by disk drive speed because (by default) SQLite actually waits until the data really is safely stored on the disk surface before the transaction is complete. That way, if you suddenly lose power or if your OS crashes, your data is still safe. For details, read about atomic commit in SQLite..

By default, each INSERT statement is its own transaction. But if you surround multiple INSERT statements with BEGIN...COMMIT then all the inserts are grouped into a single transaction. The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced.

Another option is to run PRAGMA synchronous=OFF. This command will cause SQLite to not wait on data to reach the disk surface, which will make write operations appear to be much faster. But if you lose power in the middle of a transaction, your database file might go corrupt.

Please check out this FAQ, it explains the insert bottleneck issue among others.

like image 83
medina Avatar answered Sep 07 '25 23:09

medina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!