Improve INSERT-per-second performance of SQLite
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Improving INSERT-per-second performance in SQLite requires optimizing various factors such as transaction management, write settings, and indexing. Here are key strategies to significantly boost SQLite insert performance:
1. Use Transactions Effectively
SQLite commits changes to disk after every INSERT by default, which is slow. Instead, group multiple inserts into a single transaction.
Single Transaction for Multiple Inserts:
- Why?: Committing after every insert incurs disk I/O overhead. Grouping inserts reduces the number of commits.
2. Use PRAGMA synchronous to Control Write Behavior
The synchronous setting controls SQLite's syncing with the disk. Lowering it improves performance but at the cost of some durability.
Set Synchronous to OFF or NORMAL:
OFF: SQLite doesn’t wait for writes to be confirmed by the disk.NORMAL: SQLite syncs less aggressively, improving performance while still offering reasonable safety.
3. Use PRAGMA journal_mode to Optimize Journaling
SQLite uses a journal to ensure database integrity. The journal mode impacts performance during writes.
Set Journal Mode to MEMORY or WAL:
WAL(Write-Ahead Logging): Allows concurrent reads and improves write performance.MEMORY: Speeds up writes by keeping the journal in memory but loses durability after crashes.
WAL mode is generally recommended for better INSERT performance and concurrency.
4. Use PRAGMA cache_size to Increase Cache
Increasing the cache size reduces disk I/O by caching pages in memory.
Set Cache Size:
- Adjust the cache size based on your available system memory.
5. Use Prepared Statements
Prepared statements reduce query parsing overhead when inserting multiple records.
Example Using Python:
- Why?: Prepared statements compile the query once and reuse it, saving time.
6. Disable Indexes During Bulk Inserts
Indexes slow down inserts because SQLite has to update indexes for every row. Disable indexes temporarily during bulk inserts and recreate them afterward.
Steps:
- Drop the index.
- Perform bulk inserts.
- Recreate the index.
Example:
7. Avoid Unnecessary Constraints
- Avoid using
FOREIGN KEYconstraints if they are not strictly necessary. - If constraints are needed, group inserts into transactions to minimize constraint checks.
8. Use Bulk Insert with INSERT ... VALUES
Instead of individual INSERT statements, use bulk inserts:
Example:
This reduces query overhead.
9. Optimize File System and Disk Access
- Use an SSD instead of an HDD for faster disk writes.
- Use tmpfs (on Linux) or RAM disks to store the SQLite database in memory for temporary operations.
10. Measure Performance
Use SQLite's built-in timing feature to analyze query performance:
Summary Table of Optimizations
| Optimization | Command/Action | Impact |
| Use Transactions | BEGIN TRANSACTION; ... COMMIT; | Reduces commit overhead. |
| Synchronous OFF/NORMAL | PRAGMA synchronous = OFF; | Speeds up disk writes. |
| WAL Journal Mode | PRAGMA journal_mode = WAL; | Improves write concurrency. |
| Increase Cache Size | PRAGMA cache_size = 10000; | Reduces disk I/O. |
| Use Prepared Statements | Use executemany() in Python. | Reduces query parsing overhead. |
| Disable Indexes Temporarily | Drop/recreate indexes for bulk inserts. | Speeds up bulk inserts. |
| Use Bulk INSERT Syntax | INSERT INTO table VALUES (...), (...); | Reduces query overhead. |
| Store DB in Memory (if possible) | Use :memory: or RAM disk. | Avoids disk latency. |
By applying these optimizations, you can significantly improve INSERT-per-second performance in SQLite, especially during bulk data operations. 🚀

