Skip to content

SQLite

Pragmas, strict types, and other settings for running SQLite in production.

SQLite defaults are tuned for maximum compatibility, not maximum performance or correctness. A handful of pragmas and one DDL keyword close most of the gap.

Quick-start checklist

When you create a new database, run this once:

PRAGMA journal_mode = WAL;
PRAGMA page_size    = 8192;
PRAGMA auto_vacuum  = INCREMENTAL;

On every new connection:

PRAGMA foreign_keys = ON;
PRAGMA synchronous  = NORMAL;
PRAGMA busy_timeout = 5000;
PRAGMA cache_size   = -64000;
PRAGMA temp_store   = MEMORY;
PRAGMA mmap_size    = 134217728;

One-time setup

These pragmas are written into the database file header and apply automatically to every future connection.

journal_mode = WAL

The default rollback journal serializes all writes: a writer blocks every reader while it holds the journal. WAL (Write-Ahead Log) inverts this: readers and writers operate on separate snapshots and never block each other. For nearly every workload this is the right choice.

WAL also improves write throughput because the OS can flush the WAL file sequentially rather than seeking across the main database file.

page_size = 8192

SQLite’s default page is 4 KB. Larger pages mean shallower B-trees (fewer reads to reach a leaf) and more efficient sequential scans. 8 KB is a good default for most workloads; 16 KB is worth considering for analytics-heavy databases with large blobs or wide rows.

This must be set before any tables exist. On an existing database the pragma is silently ignored until you run VACUUM.

auto_vacuum = INCREMENTAL

When rows are deleted, SQLite marks pages as free but does not shrink the file by default (NONE). INCREMENTAL mode tracks free pages and lets you reclaim them on your schedule by calling PRAGMA incremental_vacuum(N), e.g. from a nightly maintenance job. This keeps the file size manageable without the pause of a full VACUUM.

Per-connection setup

These pragmas are not stored in the file. Set them at the start of every new connection, in your database initialization code, not in a migration.

foreign_keys = ON

SQLite ships with FK enforcement off for backwards compatibility with old databases that declared foreign keys but never expected them to be checked. There is no reason to leave this off in new code.

synchronous = NORMAL

FULL (the default) calls fsync after every write. NORMAL skips the final sync on WAL checkpoints; safe because a crash can lose the most-recent checkpoint but can never corrupt the database. In practice NORMAL + WAL is the right setting for the vast majority of applications.

busy_timeout = 5000

Without this, any attempt to write while another connection holds the write lock returns SQLITE_BUSY immediately. Setting a timeout tells SQLite to retry for up to N milliseconds before giving up, which is almost always what you want under light concurrency.

cache_size = -64000

Negative values are in kibibytes. -64000 gives each connection a 64 MB page cache. More cache means fewer disk reads for frequently accessed pages. Tune based on available RAM and the number of concurrent connections.

temp_store = MEMORY

Temporary tables, indexes, and sort buffers created during query execution default to a temp file. Moving them to memory avoids the file I/O entirely for short-lived working data.

mmap_size = 134217728

Memory-maps the first 128 MB of the database file. Reads against mapped pages go directly through the OS page cache without a read() syscall or a copy into SQLite’s own buffer. On read-heavy workloads this can meaningfully reduce CPU time.

Set it to 0 to disable if the database is write-heavy or larger than available address space.

Strict tables

By default, SQLite uses type affinity: every column accepts any value, and SQLite tries to coerce it to the declared type. This means INSERT INTO users (age) VALUES ('not a number') succeeds silently. The STRICT keyword (SQLite 3.37+, released 2021) turns this off.

CREATE TABLE users (
  id       INTEGER NOT NULL PRIMARY KEY,
  username TEXT    NOT NULL,
  score    REAL,
  metadata ANY     -- flexible column: accepts any SQLite value type
) STRICT;

A STRICT table only accepts six column types:

Type Accepted Coerced Rejected
INTEGER / INT Integers Real with no fractional part → integer TEXT, BLOB
REAL Reals Integer → real TEXT, BLOB
TEXT Text, NULL Integer/real → text string BLOB
BLOB Blobs, NULL n/a TEXT, INTEGER, REAL
ANY Everything n/a n/a

Declaring any other type (e.g. VARCHAR(255), BOOLEAN, DATETIME) is a syntax error in a STRICT table, forcing you to pick a real storage type rather than a name borrowed from another database.

Use ANY deliberately. It opts a single column out of type checking entirely; use it for JSON blobs or heterogeneous metadata, not as a default.

STRICT is per-table, not per-database. You can add it to new tables in an existing schema without touching old ones.

Use STRICT on every new table. The only reason not to is if you’re importing data from an external source that doesn’t conform to your declared types; even then, fix the data, not the table.

Type to search…

↑↓ navigate↵ selectEsc close