- Put inserts/updates in a transaction.
- For older versions of SQLite – Consider a less paranoid journal mode (
pragma journal_mode). There is
NORMAL, and then there is
OFF, which can significantly increase insert speed if you’re not too worried about the database possibly getting corrupted if the OS crashes. If your application crashes the data should be fine. Note that in newer versions, the
OFF/MEMORYsettings are not safe for application level crashes.
- Playing with page sizes makes a difference as well (
PRAGMA page_size). Having larger page sizes can make reads and writes go a bit faster as larger pages are held in memory. Note that more memory will be used for your database.
- If you have indices, consider calling
CREATE INDEXafter doing all your inserts. This is significantly faster than creating the index and then doing your inserts.
- You have to be quite careful if you have concurrent access to SQLite, as the whole database is locked when writes are done, and although multiple readers are possible, writes will be locked out. This has been improved somewhat with the addition of a WAL in newer SQLite versions.
- Take advantage of saving space…smaller databases go faster. For instance, if you have key value pairs, try making the key an
INTEGER PRIMARY KEYif possible, which will replace the implied unique row number column in the table.
- If you are using multiple threads, you can try using the shared page cache, which will allow loaded pages to be shared between threads, which can avoid expensive I/O calls.
- Don’t use
I’ve also asked similar questions here and here.