-- Add composite indexes for search performance optimization -- This migration addresses N+1 query issues and enables database-level sorting -- Covering index for date-sorted queries (supports ORDER BY + pagination) -- Enables efficient date-based sorting without loading all files into memory CREATE INDEX IF NOT EXISTS idx_image_exif_date_path ON image_exif(date_taken DESC, file_path); -- Optimize batch tag count queries with GROUP BY -- Reduces N individual queries to a single batch query CREATE INDEX IF NOT EXISTS idx_tagged_photo_count ON tagged_photo(photo_name, tag_id); -- Update query planner statistics to optimize query execution ANALYZE;