perf: remove redundant COUNT query and use bulk insert for CSV imports

This commit is contained in:
Peter Stockings
2026-03-15 00:24:26 +11:00
parent 25aa7de043
commit 25d1774e53
2 changed files with 6 additions and 8 deletions

View File

@@ -288,12 +288,9 @@ def prepare_graph_data(readings):
def calculate_progress_badges(user_id, user_tz):
"""Generate badges based on user activity and milestones using optimized queries."""
total_readings = Reading.query.filter_by(user_id=user_id).count()
if total_readings == 0:
return []
# Fetch only timestamps (highly optimized compared to fetching full objects)
# Fetch only timestamps (index-only scan on the composite index)
timestamps = db.session.query(Reading.timestamp).filter(Reading.user_id == user_id).order_by(Reading.timestamp.desc()).all()
total_readings = len(timestamps)
return _compute_badges(total_readings, timestamps, user_tz)
def _compute_badges(total_readings, timestamps, user_tz, now_local=None):