Litestone guide / Advanced
Performance
Litestone is built for SQLite's one-writer, many-reader shape, and most tuning is done for you. The levers left are indexes, how much of a row you ask for, and what you let the planner see.
model Note {
id Int
kind String
archivedAt DateTime?
([kind], where: archivedAt == null) // a partial index over live notes
}
const { sql } = db.$rawDbs.main.query(`SELECT sql FROM sqlite_master WHERE name = 'idx_note_kind'`).get()
if (!/WHERE "archivedAt" IS NULL/.test(sql)) throw new Error(`expected a partial index, got ${sql}`)
await db.note.create({ data: { kind: 'k3' }, select: false })
const slim = await db.note.findMany({ where: { kind: 'k3', archivedAt: null }, select: { id: true } })
if (slim.length !== 1 || 'kind' in slim[0]) throw new Error('expected the narrow read')
A partial index is used when the query states its predicate, so filter by the question the index was built for.
What else it can do
@@softDeletemodels get partial indexes over live rows automaticallyfindUniqueby primary key and a barefindManyrun on precomputed statementsselect: falseon a write skipsRETURNINGand the row parse- A partial
selectfetches only the columns it names orderBy: { name: { dir: 'asc', nulls: 'last' } }places nulls without an in-memory sortANALYZEruns after every migration, and by hand after a bulk load- Reads use a separate
query_onlyconnection, so they never block a write readOnly: trueopens a client that cannot write, andaccesssets it per database$backuptakes a consistent hot snapshot, withvacuum: trueto compact itlitestone doctorflags unindexed foreign keys, large unindexed tables and stale statistics
In depth: Performance § Hot paths, § Partial indexes, § select: false, § Read-only clients, § $backup, § Doctor