Problem #
Measured on a live home directory cache, path strings are heavily duplicated across tables and B-tree indexes:
directoriesindexes = 222.6 MiB:idx_directories_depth ON directories(depth, path): 95.8 MiB (duplicates every fullpathstring a second time)sqlite_autoindex_directories_1(path TEXT PRIMARY KEY): 65.3 MiBidx_directories_parent ON directories(parent_path): 52.0 MiB (plus 36.4 MiB for theparent_pathcolumn itself)idx_directories_name ON directories(name): 9.6 MiB (only used byfind_bloat_hintsfor 8 known folder names)
top_filesrepeats full directory paths 4 times per row (~1.47 GiB before filtering):path(287 MiB) +dir_path(244 MiB) +sqlite_autoindex_top_files_1(496 MiB) +idx_top_files_dir(402 MiB), pluscategorystored as text (21.8 MiB) instead of a 1-byte integer enum tag.
Estimated Savings #
- ~200+ MiB on
directories+ indexes, and ~5× smaller per-row footprint ontop_files.
Proposed Fix #
- Add
id INTEGER PRIMARY KEY(rowidalias) todirectoriesand replaceparent_path TEXTwithparent_id INTEGERindexed byidx_directories_parent(parent_id)(shrinksparent_path+ index from ~88 MiB to ~6 MiB). - Drop
idx_directories_depth(depth, path)(−95.8 MiB): fetch depth-bounded trees inget_treeusing aWITH RECURSIVECTE over the integeridx_directories_parent(parent_id)index (fast, exact, zero duplicated path strings). - Replace
idx_directories_name(−9.5 MiB) with a partial index covering only bloat folder names:CREATE INDEX idx_directories_bloat ON directories(name, subtree_physical_bytes DESC) WHERE name IN ('target', 'node_modules', '.venv', 'DerivedData', 'Caches', '.cache', 'ollama', 'huggingface'); - In
top_files, replacepath TEXT PRIMARY KEYanddir_path TEXTwithid INTEGER PRIMARY KEY,dir_id INTEGER NOT NULL,name TEXT NOT NULL, andcategory INTEGER NOT NULL(reconstructing fullpathvia join todirectoriesat query time). - Verify all cache unit tests, proptests, and e2e tests pass after schema migration.