From 4d532e618719bb6fc60a975adafdc53b67ae257b Mon Sep 17 00:00:00 2001 From: Ayush Pandey Date: Wed, 8 Jul 2026 00:09:48 +0530 Subject: [PATCH 1/2] perf: optimize unread notification count query --- backend/app/services/notification_service.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/app/services/notification_service.py b/backend/app/services/notification_service.py index f36910c8..d836c0ae 100644 --- a/backend/app/services/notification_service.py +++ b/backend/app/services/notification_service.py @@ -3,7 +3,7 @@ import uuid from datetime import datetime -from sqlalchemy import select +from sqlalchemy import func, select from sqlalchemy.orm import Session from app.models.notification import Notification @@ -91,12 +91,12 @@ def unread_count( recipient_id: uuid.UUID, ) -> int: - stmt = select(Notification).where( + stmt = select(func.count()).select_from(Notification).where( Notification.recipient_id == recipient_id, Notification.is_read.is_(False), ) - return len(list(db.scalars(stmt))) + return db.scalar(stmt) or 0 @staticmethod def mark_as_read( From 61e77833a1a982f834b0a481572bf8599cfc52b4 Mon Sep 17 00:00:00 2001 From: Ayush Pandey Date: Wed, 8 Jul 2026 18:31:34 +0530 Subject: [PATCH 2/2] style: format notification service with black --- backend/app/services/notification_service.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/app/services/notification_service.py b/backend/app/services/notification_service.py index d836c0ae..0d15fe62 100644 --- a/backend/app/services/notification_service.py +++ b/backend/app/services/notification_service.py @@ -91,9 +91,13 @@ def unread_count( recipient_id: uuid.UUID, ) -> int: - stmt = select(func.count()).select_from(Notification).where( - Notification.recipient_id == recipient_id, - Notification.is_read.is_(False), + stmt = ( + select(func.count()) + .select_from(Notification) + .where( + Notification.recipient_id == recipient_id, + Notification.is_read.is_(False), + ) ) return db.scalar(stmt) or 0