BUG: /folders/all-folders takes over a second when folders contain both images and videos

Đang mở Phù hợp với người mới
#1,708 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 6 ngày

@Saurabhyadav0 đang làm issue này rồi.

Từ ngày 13/11/2026.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, sqlite
Lĩnh vực
backend, databases

Hướng nghiên cứu

Truy vấn chậm nằm trong db_get_all_folder_details() tại backend/app/database/folders.py, thực hiện LEFT JOIN bảng images và videos vào folders rồi đếm bằng COUNT(DISTINCT). Trước tiên, hãy đọc ba bình luận để kiểm tra xem đã có ai nhận việc này chưa. Viết lại hàm để đếm từng bảng trong một subquery GROUP BY folder_id riêng, rồi nối các tổng số đó vào folders. Hoàn thành khi phản hồi của /folders/all-folders không thay đổi với thư mục rỗng, chỉ có ảnh, chỉ có video và hỗn hợp, và một test mới trong các test thư mục hiện có khẳng định chính xác các số đếm đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug enhancement possible-duplicate
Is there an existing issue for this?
  • I have searched the existing issues
What happened?

db_get_all_folder_details() in backend/app/database/folders.py left-joins both images and videos onto folders on the same folder_id and uses COUNT(DISTINCT ...) to keep the counts right. The counts are correct, but SQLite still builds images × videos rows per folder before counting, so the cost grows with the product rather than the sum.

FROM folders f
LEFT JOIN images i ON f.folder_id = i.folder_id
LEFT JOIN videos v ON f.folder_id = v.folder_id
GROUP BY f.folder_id

While the folder list in Settings is open and a folder with AI tagging is still indexing, the frontend polls this endpoint every second (frontend/src/hooks/useFolderOperations.tsx), so on a large mixed library each poll takes over a second.

GET /folders/all-folders, median of 5, on a database built with the project's db_create_* functions:

Library Now
20 folders × 2,500 images, no videos 66 ms
20 folders × 2,500 images × 10 videos 144 ms
20 folders × 2,500 images × 100 videos 1,192 ms
1 folder × 10,000 images × 500 videos 1,184 ms

Counting each table in its own GROUP BY folder_id subquery and joining the totals onto folders returns a byte-identical response in 3–20 ms for all of the above (also checked against empty, image-only and video-only folders). An index on folder_id alone doesn't help, since the row multiplication is the cost — with indexes added the current query actually got slower.

The query plan confirms the multiplication:

SCAN f ... | SEARCH i ... LEFT-JOIN | SEARCH v ... LEFT-JOIN
| USE TEMP B-TREE FOR count(DISTINCT) | USE TEMP B-TREE FOR count(DISTINCT)

Related: #876 reports the Settings folder list being slow in general. It predates this join (added in fab7ef08, 2026-07-24), so it isn't caused by it, but fixing this removes one source of that slowness. #1404 point 1 is what added video_count (the second join) — the counts it asked for are correct; this is about the cost only.

I'd like to work on this: rewrite the query to count each table in its own grouped subquery, and add a test with a folder holding several images and several videos (plus empty / image-only / video-only) asserting the exact counts are unchanged.

Record
  • I agree to follow this project's Code of Conduct
Ngôn ngữ chính
Python
Star
313
Fork
755
Merge trung bình
6 ngày 36 phút
Pull request đã merge (30 ngày)
8

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của AOSSIE-Org/PictoPy

Tất cả issue của AOSSIE-Org/PictoPy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.