You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a marathon runner, I'd like to see the time I've spent running in each training zone for my training block (18 weeks).
As a marathon runner, I might be training for multiple races at once (like a half marathon before my marathon) with different paces. I'd like to see a pace breakdown for each race.
Option 1: Precalculate Summaries
This option minimizes PostgreSQL calculation overhead by precalculating summaries for each race goal.
Database Schema
race_goals (existing table)
CREATETABLErace_goals (
id SERIALPRIMARY KEY,
user_id INTEGERNOT NULL,
race_date DATENOT NULL,
target_time INTERVAL,
distance NUMERIC(8, 2), -- in meters
name VARCHAR(255)
);
Insert multiple records into pace_zones, one for each zone (typically 5-6 zones)
When a new activity is uploaded:
Calculate the time and distance spent in each zone based on the trackpoints and the user's current race goal(s)
Insert records into activity_pace_zone_summary for each relevant zone
Pros
Efficient querying of pace zone data
Quick retrieval of zone data without recalculation
Supports multiple race goals and custom zone definitions
Cons
Requires storage of precalculated summaries
Less flexible if zone definitions change frequently
Option 2: Trackpoint Segments
This option stores the difference between each trackpoint in a separate table, allowing PostgreSQL to perform real-time calculations for time spent in each pace zone.
Database Schema
trackpoint_segments
CREATETABLEtrackpoint_segments (
id SERIALPRIMARY KEY,
activity_id INTEGERNOT NULL,
segment_start TIMESTAMP WITH TIME ZONENOT NULL,
segment_duration INTERVAL NOT NULL,
segment_distance NUMERIC(8, 4) NOT NULL,
segment_pace INTERVAL NOT NULL,
FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE
);
pace_zones (same as Option 1)
Implementation Considerations
Indexing: Create indexes on activity_id, segment_start, and segment_pace for performance.
Partitioning: For large datasets, consider partitioning the trackpoint_segments table by activity_id or date ranges.
Materialized Views: Use materialized views for common queries to improve performance.
Query Optimization: Carefully optimize SQL queries for zone calculations.
Pros
Flexibility: Zones can be redefined without recalculating summaries.
Accuracy: Calculations are always based on the most granular data available.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Pace Zones Implementation Options
User Stories
Option 1: Precalculate Summaries
This option minimizes PostgreSQL calculation overhead by precalculating summaries for each race goal.
Database Schema
Implementation
When a user sets up a new race goal:
race_goalspace_zones, one for each zone (typically 5-6 zones)When a new activity is uploaded:
activity_pace_zone_summaryfor each relevant zonePros
Cons
Option 2: Trackpoint Segments
This option stores the difference between each trackpoint in a separate table, allowing PostgreSQL to perform real-time calculations for time spent in each pace zone.
Database Schema
Implementation Considerations
activity_id,segment_start, andsegment_pacefor performance.trackpoint_segmentstable byactivity_idor date ranges.Pros
Cons
Conclusion
Both options have their merits and drawbacks. The choice between them depends on factors such as:
Consider implementing a hybrid approach or starting with one option and being prepared to transition if needed as your application scales.
Beta Was this translation helpful? Give feedback.
All reactions