perf(notifications): bulk-delete trimmed read notifications (C-4.1)

trim_read looped a db.get()+db.delete() per id (N+1). The ids are already in hand, so
one DELETE ... WHERE id IN (...) replaces the loop.
This commit is contained in:
2026-07-21 21:47:26 +02:00
parent dd99206ed2
commit 3dfa35f396
+3 -3
View File
@@ -5,7 +5,7 @@ survive reloads/devices and are produced server-side. First producer is the main
job. Unread rows never auto-expire; read rows are trimmed past a soft per-user cap so the
table can't grow without bound for a heavy user.
"""
from sqlalchemy import select
from sqlalchemy import delete, select
from sqlalchemy.orm import Session
from app.models import Notification
@@ -52,7 +52,7 @@ def trim_read(db: Session, user_id: int, cap: int = READ_SOFT_CAP) -> int:
)
if not ids:
return 0
for nid in ids:
db.delete(db.get(Notification, nid))
# One bulk DELETE rather than a get+delete per row (the ids are already in hand).
db.execute(delete(Notification).where(Notification.id.in_(ids)))
db.commit()
return len(ids)