perf: remove redundant COUNT query and use bulk insert for CSV imports
This commit is contained in:
@@ -17,16 +17,17 @@ def manage_data():
|
|||||||
try:
|
try:
|
||||||
csv_data = csv.reader(StringIO(file.read().decode('utf-8')))
|
csv_data = csv.reader(StringIO(file.read().decode('utf-8')))
|
||||||
next(csv_data) # Skip the header row
|
next(csv_data) # Skip the header row
|
||||||
|
readings_to_add = []
|
||||||
for row in csv_data:
|
for row in csv_data:
|
||||||
timestamp, systolic, diastolic, heart_rate = row
|
timestamp, systolic, diastolic, heart_rate = row
|
||||||
reading = Reading(
|
readings_to_add.append(Reading(
|
||||||
user_id=current_user.id,
|
user_id=current_user.id,
|
||||||
timestamp=datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S'),
|
timestamp=datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S'),
|
||||||
systolic=int(systolic),
|
systolic=int(systolic),
|
||||||
diastolic=int(diastolic),
|
diastolic=int(diastolic),
|
||||||
heart_rate=int(heart_rate),
|
heart_rate=int(heart_rate),
|
||||||
)
|
))
|
||||||
db.session.add(reading)
|
db.session.bulk_save_objects(readings_to_add)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Data imported successfully!', 'success')
|
flash('Data imported successfully!', 'success')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -288,12 +288,9 @@ def prepare_graph_data(readings):
|
|||||||
|
|
||||||
def calculate_progress_badges(user_id, user_tz):
|
def calculate_progress_badges(user_id, user_tz):
|
||||||
"""Generate badges based on user activity and milestones using optimized queries."""
|
"""Generate badges based on user activity and milestones using optimized queries."""
|
||||||
total_readings = Reading.query.filter_by(user_id=user_id).count()
|
# Fetch only timestamps (index-only scan on the composite index)
|
||||||
if total_readings == 0:
|
|
||||||
return []
|
|
||||||
|
|
||||||
# Fetch only timestamps (highly optimized compared to fetching full objects)
|
|
||||||
timestamps = db.session.query(Reading.timestamp).filter(Reading.user_id == user_id).order_by(Reading.timestamp.desc()).all()
|
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)
|
return _compute_badges(total_readings, timestamps, user_tz)
|
||||||
|
|
||||||
def _compute_badges(total_readings, timestamps, user_tz, now_local=None):
|
def _compute_badges(total_readings, timestamps, user_tz, now_local=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user