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:
@@ -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
|
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.
|
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 sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app.models import Notification
|
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:
|
if not ids:
|
||||||
return 0
|
return 0
|
||||||
for nid in ids:
|
# One bulk DELETE rather than a get+delete per row (the ids are already in hand).
|
||||||
db.delete(db.get(Notification, nid))
|
db.execute(delete(Notification).where(Notification.id.in_(ids)))
|
||||||
db.commit()
|
db.commit()
|
||||||
return len(ids)
|
return len(ids)
|
||||||
|
|||||||
Reference in New Issue
Block a user