perf: remove redundant COUNT query and use bulk insert for CSV imports
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user