Optimized SQL query that fetches thumbnails for the album view