Compaction
Compaction rebuilds collection storage to remove fragmentation and then rebuilds indexes.
You typically run compaction when:
- the database has been running for a long time with lots of updates/deletes
- you want to reclaim disk space
- you want to "defragment" collections after heavy write workloads
db.compactCollection(name)
- TypeScript
- JavaScript
compactCollection(name: string): Promise<void>
compactCollection(name)
- TypeScript
- JavaScript
import { LioranManager } from "@liorandb/core";
const manager = new LioranManager({ rootPath: "./.liorandb" });
const db = await manager.db("app");
await db.compactCollection("users");
await manager.close();
import { LioranManager } from "@liorandb/core";
const manager = new LioranManager({ rootPath: "./.liorandb" });
const db = await manager.db("app");
await db.compactCollection("users");
await manager.close();
Sandbox output (example)
(no output)
db.compactAll()
- TypeScript
- JavaScript
compactAll(): Promise<void>
compactAll()
- TypeScript
- JavaScript
await db.compactAll();
await db.compactAll();
collection.compact()
- TypeScript
- JavaScript
compact(): Promise<void>
compact()
- TypeScript
- JavaScript
const users = db.collection("users");
await users.compact();
const users = db.collection("users");
await users.compact();
What compaction does (high level)
- crash recovery checks
- snapshot rebuild to a temporary directory
- atomic directory swap
- reopen DB
- rebuild indexes