perf: Add eager loading for comment attachments in task view

- Add selectinload(Comment.attachments) to task comments query
- Prevent N+1 queries when displaying comments with attachments
- Improve performance for pages with multiple comments

This optimization reduces database queries from N+1 to a single
query for all attachments, significantly improving page load times.
This commit is contained in:
Dries Peeters
2026-01-04 06:23:48 +01:00
parent 31066e7f8d
commit fed49cea5b
+3 -1
View File
@@ -268,7 +268,9 @@ def view_task(task_id):
joinedload(Comment.author), # Eagerly load author for all comments
# Load replies with their authors - selectinload loads all direct replies in one query
# This prevents N+1 queries when accessing comment.replies in the template
selectinload(Comment.replies).joinedload(Comment.author)
selectinload(Comment.replies).joinedload(Comment.author),
# Eagerly load attachments to prevent N+1 queries
selectinload(Comment.attachments)
)
.order_by(Comment.created_at.asc())
.all()