perf: one grouped query for latest per-flat application, not N
_wohnungen_context was calling last_application_for_flat(uid, f.id)
inside the flats loop — at 100 flats that's 101 queries per poll, and
with running applications the page polls every 3s. New
db.latest_applications_by_flat(user_id) returns {flat_id: row} via a
single grouped self-join.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ebb11178e7
commit
77098c82df
2 changed files with 22 additions and 2 deletions
|
|
@ -400,6 +400,9 @@ def _wohnungen_context(user) -> dict:
|
||||||
age_cutoff = None
|
age_cutoff = None
|
||||||
if max_age_hours:
|
if max_age_hours:
|
||||||
age_cutoff = datetime.now(timezone.utc) - timedelta(hours=int(max_age_hours))
|
age_cutoff = datetime.now(timezone.utc) - timedelta(hours=int(max_age_hours))
|
||||||
|
# One query for this user's latest application per flat, instead of a
|
||||||
|
# per-flat query inside the loop.
|
||||||
|
latest_apps = db.latest_applications_by_flat(uid)
|
||||||
flats_view = []
|
flats_view = []
|
||||||
for f in flats:
|
for f in flats:
|
||||||
if f["id"] in rejected:
|
if f["id"] in rejected:
|
||||||
|
|
@ -417,8 +420,7 @@ def _wohnungen_context(user) -> dict:
|
||||||
"wbs": f["wbs"],
|
"wbs": f["wbs"],
|
||||||
}, filters):
|
}, filters):
|
||||||
continue
|
continue
|
||||||
last = db.last_application_for_flat(uid, f["id"])
|
flats_view.append({"row": f, "last": latest_apps.get(f["id"])})
|
||||||
flats_view.append({"row": f, "last": last})
|
|
||||||
|
|
||||||
rejected_view = db.rejected_flats(uid)
|
rejected_view = db.rejected_flats(uid)
|
||||||
enrichment_counts = db.enrichment_counts()
|
enrichment_counts = db.enrichment_counts()
|
||||||
|
|
|
||||||
18
web/db.py
18
web/db.py
|
|
@ -675,6 +675,24 @@ def last_application_for_flat(user_id: int, flat_id: str) -> Optional[sqlite3.Ro
|
||||||
).fetchone()
|
).fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def latest_applications_by_flat(user_id: int) -> dict:
|
||||||
|
"""Return a dict {flat_id: latest-application-row} for all flats the user
|
||||||
|
has interacted with. One query instead of N."""
|
||||||
|
rows = _get_conn().execute(
|
||||||
|
"""SELECT a.*
|
||||||
|
FROM applications a
|
||||||
|
JOIN (
|
||||||
|
SELECT flat_id, MAX(started_at) AS maxstart
|
||||||
|
FROM applications
|
||||||
|
WHERE user_id = ?
|
||||||
|
GROUP BY flat_id
|
||||||
|
) m ON m.flat_id = a.flat_id AND m.maxstart = a.started_at
|
||||||
|
WHERE a.user_id = ?""",
|
||||||
|
(user_id, user_id),
|
||||||
|
).fetchall()
|
||||||
|
return {r["flat_id"]: r for r in rows}
|
||||||
|
|
||||||
|
|
||||||
def has_running_application(user_id: int) -> bool:
|
def has_running_application(user_id: int) -> bool:
|
||||||
row = _get_conn().execute(
|
row = _get_conn().execute(
|
||||||
"SELECT 1 FROM applications WHERE user_id = ? AND finished_at IS NULL LIMIT 1",
|
"SELECT 1 FROM applications WHERE user_id = ? AND finished_at IS NULL LIMIT 1",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue