| title | SQLite PRAGMAs and Tuning |
|---|---|
| description | Safe performance tuning using SQLite runtime settings |
| tags | sqlite, pragma, tuning, cache, memory |
PRAGMAs change behavior globally for a connection or database file. Benchmark before and after under representative load.
PRAGMA cache_size = -32768; -- about 32 MB cache (negative = KB)
PRAGMA temp_store = MEMORY; -- keep temp objects in memory when feasible
PRAGMA mmap_size = 268435456; -- 256 MB mapped I/O if platform supports it- Larger cache helps read-heavy workloads, but increases memory use.
temp_store=MEMORYcan improve sorts/joins but may increase RSS.mmap_sizecan reduce syscall overhead on capable filesystems.
PRAGMA foreign_keys = ON;
PRAGMA trusted_schema = OFF;- Always enable foreign keys unless there is a specific documented reason not to.
trusted_schema=OFFreduces risk from malicious schema-level SQL in untrusted files.
PRAGMA analysis_limit = 2000;
PRAGMA optimize;PRAGMA optimizemay run targetedANALYZEwork when statistics are stale or missing.- Use explicit
ANALYZEafter large data shifts; usePRAGMA optimizeas periodic maintenance.
synchronous=OFFis risky for most production apps.- Revisit tuning after schema/index changes; bottlenecks move.