Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop departure query optimizations #289

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions finders/dbfinder/stop_time_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func StopDeparturesSelect(spairs []FVPair, where *model.StopTimeFilter) sq.Selec
"gtfs_trips.id AS trip_id",
"gtfs_trips.feed_version_id",
"sts.stop_id",
"sts.arrival_time + gtfs_trips.journey_pattern_offset + coalesce(-trip_stop_sequence.first_departure_time + freq.freq_start, 0) AS arrival_time",
"sts.departure_time + gtfs_trips.journey_pattern_offset + coalesce(-trip_stop_sequence.first_departure_time + freq.freq_start, 0) AS departure_time",
"sts.arrival_time_freq AS arrival_time",
"sts.departure_time_freq AS departure_time",
"sts.stop_sequence",
"sts.shape_dist_traveled",
"sts.pickup_type",
Expand All @@ -91,26 +91,6 @@ func StopDeparturesSelect(spairs []FVPair, where *model.StopTimeFilter) sq.Selec
"sts.continuous_drop_off",
).
From("gtfs_trips").
Join("feed_versions on feed_versions.id = gtfs_trips.feed_version_id").
Join("current_feeds on current_feeds.id = feed_versions.feed_id").
Join("gtfs_trips t2 ON t2.trip_id::text = gtfs_trips.journey_pattern_id AND gtfs_trips.feed_version_id = t2.feed_version_id").
Join("gtfs_stop_times sts ON sts.trip_id = t2.id and sts.feed_version_id = t2.feed_version_id").
JoinClause(`join lateral (
select
min(sts2.departure_time) first_departure_time,
min(sts2.stop_sequence),
max(sts2.stop_sequence) max
from gtfs_stop_times sts2
where
sts2.trip_id = t2.id
AND sts2.feed_version_id = t2.feed_version_id
) trip_stop_sequence on true`).
JoinClause(`left join lateral (
select
generate_series(start_time, end_time, headway_secs) freq_start
from gtfs_frequencies
where gtfs_frequencies.trip_id = gtfs_trips.id
) freq on true`).
JoinClause(`join (
SELECT
id
Expand Down Expand Up @@ -154,18 +134,49 @@ func StopDeparturesSelect(spairs []FVPair, where *model.StopTimeFilter) sq.Selec
fvids,
serviceDate,
fvids).
Join("gtfs_trips base_trip ON base_trip.trip_id::text = gtfs_trips.journey_pattern_id AND gtfs_trips.feed_version_id = base_trip.feed_version_id").
Join("feed_versions on feed_versions.id = gtfs_trips.feed_version_id").
Join("current_feeds on current_feeds.id = feed_versions.feed_id").
JoinClause(`left join lateral (
select
generate_series(start_time, end_time, headway_secs) freq_start
from gtfs_frequencies
where gtfs_frequencies.trip_id = gtfs_trips.id
) freq on true`).
JoinClause(`join lateral (
select
min(sts2.departure_time) first_departure_time,
min(sts2.stop_sequence) stop_sequence_min,
max(sts2.stop_sequence) stop_sequence_max
from gtfs_stop_times sts2
where sts2.trip_id = base_trip.id and sts2.feed_version_id = base_trip.feed_version_id
) trip_stop_sequence on true`).
JoinClause(`join lateral (
select
sts.*,
sts.arrival_time + gtfs_trips.journey_pattern_offset + coalesce(
- trip_stop_sequence.first_departure_time + freq.freq_start,
0
) AS arrival_time_freq,
sts.departure_time + gtfs_trips.journey_pattern_offset + coalesce(
- trip_stop_sequence.first_departure_time + freq.freq_start,
0
) AS departure_time_freq
from gtfs_stop_times sts
where sts.trip_id = base_trip.id and sts.feed_version_id = base_trip.feed_version_id
) sts on true`).
Where(
In("sts.stop_id", sids),
In("sts.feed_version_id", fvids),
).
OrderBy("sts.departure_time + gtfs_trips.journey_pattern_offset + coalesce(-trip_stop_sequence.first_departure_time + freq.freq_start, 0)", "sts.trip_id") // base + offset
OrderBy("sts.departure_time_freq", "sts.trip_id") // base + offset

if where != nil {
if where.ExcludeFirst != nil && *where.ExcludeFirst {
q = q.Where("sts.stop_sequence > trip_stop_sequence.min")
q = q.Where("sts.stop_sequence > trip_stop_sequence.stop_sequence_min")
}
if where.ExcludeLast != nil && *where.ExcludeLast {
q = q.Where("sts.stop_sequence < trip_stop_sequence.max")
q = q.Where("sts.stop_sequence < trip_stop_sequence.stop_sequence_max")
}
if len(where.RouteOnestopIds) > 0 {
if where.AllowPreviousRouteOnestopIds != nil && *where.AllowPreviousRouteOnestopIds {
Expand Down Expand Up @@ -200,10 +211,10 @@ func StopDeparturesSelect(spairs []FVPair, where *model.StopTimeFilter) sq.Selec
where.EndTime = ptr(where.End.Int())
}
if where.StartTime != nil {
q = q.Where(sq.GtOrEq{"sts.departure_time + gtfs_trips.journey_pattern_offset + coalesce(-trip_stop_sequence.first_departure_time + freq.freq_start, 0)": *where.StartTime})
q = q.Where(sq.GtOrEq{"sts.departure_time_freq": *where.StartTime})
}
if where.EndTime != nil {
q = q.Where(sq.LtOrEq{"sts.departure_time + gtfs_trips.journey_pattern_offset + coalesce(-trip_stop_sequence.first_departure_time + freq.freq_start, 0)": *where.EndTime})
q = q.Where(sq.LtOrEq{"sts.departure_time_freq": *where.EndTime})
}
}
return q
Expand Down
Loading