perf: indexes, query rewrites, caching — fix slow load at 30k+ photos
- Expression index on strftime('%m-%d', date_taken) makes Memories instant
- Composite indexes: (deleted, date_taken), (deleted, starred), (deleted, media_type),
(deleted, place_city), (deleted, camera_model), (collection_id, photo_id)
- Albums: replace window-function queries with GROUP BY + MAX(id) CTE — avoids
materialising 30k rows × 3 times just to pick cover photos
- Library stats + Albums: 30s/60s in-memory cache, invalidated on any write
- Videos: cursor-based pagination (100/page) with IntersectionObserver infinite scroll
- Fix build error: mixed ?? and || operators in MemoriesView track count display
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
6f4e90a1c8
commit
bc2eb7307f
5 files changed
+180
-58
No files matched your search
@@ -126,6 +126,19 @@ export function openDb(dbPath) {
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_place_city ON photos(place_city)');
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_media_type ON photos(media_type)');
|
||||
|
||||
// Performance indexes for 30k+ libraries
|
||||
// Composite covering index for the main library query (deleted + date_taken for sorted pagination)
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_deleted_date ON photos(deleted, date_taken DESC, id DESC)');
|
||||
// Composite for stats queries
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_deleted_starred ON photos(deleted, starred)');
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_deleted_type ON photos(deleted, media_type)');
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_deleted_city ON photos(deleted, place_city)');
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photos_deleted_camera ON photos(deleted, camera_model)');
|
||||
// Expression index for Memories ("on this day") — makes strftime('%m-%d') queries instant
|
||||
db.exec("CREATE INDEX IF NOT EXISTS idx_photos_month_day ON photos(strftime('%m-%d', date_taken)) WHERE date_taken IS NOT NULL");
|
||||
// Collections join performance
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_photo_collections_col ON photo_collections(collection_id, photo_id)');
|
||||
|
||||
// Seed default settings
|
||||
const seedSetting = db.prepare('INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)');
|
||||
for (const [k, v] of Object.entries(SETTING_DEFAULTS)) seedSetting.run(k, v);
|
||||
|
||||
Reference in new issue
Block a user