LanceDB Optimize
Compact and prune all LanceDB tables to merge accumulated fragment files,
reduce open file descriptors, and improve query performance.
When To Use
- Periodic maintenance (recommended weekly or after heavy write activity).
- After "Too many open files" (os error 24) errors.
- After bulk imports, batch embeds, or large cleanup operations.
- Before backups to minimize storage footprint.
Database Roots
All paths are relative to DB_ROOT (default: /mnt/wsl/data4tb/static-flow-data).
| DB |
Path |
Tables |
| Content |
$DB_ROOT/lancedb |
api_behavior_events, article_request_ai_run_chunks, article_request_ai_runs, article_requests, article_views, articles, images, interactive_assets, interactive_page_locales, interactive_pages, llm_gateway_keys, llm_gateway_usage_events, llm_gateway_runtime_config, taxonomies |
| Comments |
$DB_ROOT/lancedb-comments |
comment_ai_run_chunks, comment_ai_runs, comment_audit_logs, comment_published, comment_tasks |
| Music |
$DB_ROOT/lancedb-music |
music_comments, music_plays, music_wish_ai_run_chunks, music_wish_ai_runs, music_wishes, songs |
Preconditions
- Resolve CLI in this order:
./target/release/sf-cli
./target/debug/sf-cli
../target/release/sf-cli
sf-cli from PATH
- Verify CLI works:
<cli> --help
- Build if needed:
cargo build -p sf-cli --release
- If the checkout is newer than the chosen binary, rebuild before use.
- Do not prefer legacy
./bin/sf-cli snapshots for storage-format-sensitive writes.
- Verify DB paths exist.
Execution Workflow
Step 1: Pre-check — Count Fragments
For each DB root and each table, inspect fragment counts via Lance metadata
instead of counting files under data/:
<cli> db --db-path <db_path> audit-storage --table <table>
Report a summary table of fragment counts. Tables with <= 3 fragments can be
skipped (already compact).
Step 2: Optimize All Tables
For each table that needs compaction:
<cli> db --db-path <db_path> optimize <table> --all --prune-now
--all: full optimization (compact fragments + rebuild indexes)
--prune-now: immediately remove old versions (older_than=0, delete_unverified=true)
- On the current StaticFlow fork, blob v2 tables such as
songs, images,
and interactive_assets are expected to compact normally.
Run tables within the same DB sequentially (they share the same lock).
Different DBs can run in parallel.
Step 3: Post-check — Verify
- Re-check fragment counts with
audit-storage.
- Verify row counts match pre-optimization counts:
<cli> db --db-path <db_path> count-rows <table>
- Report before/after comparison.
Selective Optimization
To optimize only specific DBs or tables:
# Single table
<cli> db --db-path /mnt/wsl/data4tb/static-flow-data/lancedb optimize api_behavior_events --all --prune-now
# All tables in one DB
for t in <table_list>; do
<cli> db --db-path <db_path> optimize "$t" --all --prune-now
done
Safety Notes
- Optimization is non-destructive: it merges fragments and removes old versions,
but never deletes current data.
- The backend should ideally not be writing heavily during optimization to avoid
lock contention. Light writes (normal API traffic) are fine.
- If optimization fails mid-way, the table remains valid — just not fully compacted.
Re-run to complete.
1---2name: lancedb-optimize3description: >-4 Optimize (compact + prune) all StaticFlow LanceDB tables across content,5 comments, and music databases to merge fragment files and reclaim storage.6metadata:7 source: https://skillmd.com/skills/zero-yx/lancedb-optimize8---910# LanceDB Optimize1112Compact and prune all LanceDB tables to merge accumulated fragment files,13reduce open file descriptors, and improve query performance.1415## When To Use161. Periodic maintenance (recommended weekly or after heavy write activity).172. After "Too many open files" (os error 24) errors.183. After bulk imports, batch embeds, or large cleanup operations.194. Before backups to minimize storage footprint.2021## Database Roots2223All paths are relative to `DB_ROOT` (default: `/mnt/wsl/data4tb/static-flow-data`).2425| DB | Path | Tables |26|----|------|--------|27| Content | `$DB_ROOT/lancedb` | `api_behavior_events`, `article_request_ai_run_chunks`, `article_request_ai_runs`, `article_requests`, `article_views`, `articles`, `images`, `interactive_assets`, `interactive_page_locales`, `interactive_pages`, `llm_gateway_keys`, `llm_gateway_usage_events`, `llm_gateway_runtime_config`, `taxonomies` |28| Comments | `$DB_ROOT/lancedb-comments` | `comment_ai_run_chunks`, `comment_ai_runs`, `comment_audit_logs`, `comment_published`, `comment_tasks` |29| Music | `$DB_ROOT/lancedb-music` | `music_comments`, `music_plays`, `music_wish_ai_run_chunks`, `music_wish_ai_runs`, `music_wishes`, `songs` |3031## Preconditions321. Resolve CLI in this order:33 - `./target/release/sf-cli`34 - `./target/debug/sf-cli`35 - `../target/release/sf-cli`36 - `sf-cli` from `PATH`372. Verify CLI works: `<cli> --help`38 - Build if needed: `cargo build -p sf-cli --release`393. If the checkout is newer than the chosen binary, rebuild before use.404. Do not prefer legacy `./bin/sf-cli` snapshots for storage-format-sensitive writes.415. Verify DB paths exist.4243## Execution Workflow4445### Step 1: Pre-check — Count Fragments4647For each DB root and each table, inspect fragment counts via Lance metadata48instead of counting files under `data/`:4950```bash51<cli> db --db-path <db_path> audit-storage --table <table>52```5354Report a summary table of fragment counts. Tables with <= 3 fragments can be55skipped (already compact).5657### Step 2: Optimize All Tables5859For each table that needs compaction:6061```bash62<cli> db --db-path <db_path> optimize <table> --all --prune-now63```6465- `--all`: full optimization (compact fragments + rebuild indexes)66- `--prune-now`: immediately remove old versions (older_than=0, delete_unverified=true)67- On the current StaticFlow fork, blob v2 tables such as `songs`, `images`,68 and `interactive_assets` are expected to compact normally.6970Run tables within the same DB sequentially (they share the same lock).71Different DBs can run in parallel.7273### Step 3: Post-check — Verify74751. Re-check fragment counts with `audit-storage`.762. Verify row counts match pre-optimization counts:77 ```bash78 <cli> db --db-path <db_path> count-rows <table>79 ```803. Report before/after comparison.8182## Selective Optimization8384To optimize only specific DBs or tables:8586```bash87# Single table88<cli> db --db-path /mnt/wsl/data4tb/static-flow-data/lancedb optimize api_behavior_events --all --prune-now8990# All tables in one DB91for t in <table_list>; do92 <cli> db --db-path <db_path> optimize "$t" --all --prune-now93done94```9596## Safety Notes97- Optimization is non-destructive: it merges fragments and removes old versions,98 but never deletes current data.99- The backend should ideally not be writing heavily during optimization to avoid100 lock contention. Light writes (normal API traffic) are fine.101- If optimization fails mid-way, the table remains valid — just not fully compacted.102 Re-run to complete.