Repository navigation
Index block_trip_entry by block_id #1688
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26688ff
Index block_trip_entry by block_id
aaronbrethorst 68abc72
Explain the real query in the block_id index test
aaronbrethorst 8333821
Check the block_id index plan on the RABA feed
aaronbrethorst 5c1e5ae
Name both block_id queries in the index comment
aaronbrethorst a5b2bda
Merge branch 'main' into block-trip-entry-block-id-index
burma-shave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package gtfsdb | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // GetBlocksForBlockTripIndexIDs joins block_trip_entry to itself on block_id. Without | ||
| // an index there, SQLite scans every block entry for every route trip, which made | ||
| // trips-for-route take seconds on NJ Transit's 33k-trip feed. | ||
| func TestBlockTripEntryBlockIDJoinUsesIndex(t *testing.T) { | ||
| client := newTestClientWithRABA(t) | ||
| ctx := t.Context() | ||
|
|
||
| var routeID, serviceID string | ||
| var indexID int64 | ||
| require.NoError(t, client.DB.QueryRowContext(ctx, ` | ||
| SELECT trips.route_id, bte.service_id, bte.block_trip_index_id | ||
| FROM block_trip_entry bte JOIN trips ON trips.id = bte.trip_id | ||
| WHERE bte.block_id IS NOT NULL LIMIT 1`).Scan(&routeID, &serviceID, &indexID)) | ||
|
|
||
| recorder := &queryRecorder{DBTX: client.DB} | ||
| _, err := New(recorder).GetBlocksForBlockTripIndexIDs(ctx, GetBlocksForBlockTripIndexIDsParams{ | ||
| FromTime: sql.NullInt64{Int64: 0, Valid: true}, | ||
| ToTime: sql.NullInt64{Int64: int64(24 * time.Hour), Valid: true}, | ||
| RouteID: routeID, | ||
| IndexIds: []int64{indexID}, | ||
| ActiveServiceIds: []string{serviceID}, | ||
| RouteServiceIds: []string{serviceID}, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // The planner may start the self-join from either side, so accept the index | ||
| // on either alias. | ||
| plan := strings.Join(explainRecordedQuery(t, client, recorder), "\n") | ||
| assert.Regexp(t, `SEARCH \w+ USING (COVERING )?INDEX idx_block_trip_entry_block_id \(block_id=\?`, plan) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package gtfsdb | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // queryRecorder captures the SQL and arguments a generated query method sends, | ||
| // so a test can explain the real query rather than a copy of it. | ||
| type queryRecorder struct { | ||
| DBTX | ||
| query string | ||
| args []any | ||
| } | ||
|
|
||
| func (db *queryRecorder) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) { | ||
| db.query, db.args = query, args | ||
| return db.DBTX.QueryContext(ctx, query, args...) | ||
| } | ||
|
|
||
| // explainRecordedQuery returns the detail column of each EXPLAIN QUERY PLAN row | ||
| // for the query the recorder last captured. | ||
| func explainRecordedQuery(t *testing.T, client *Client, recorder *queryRecorder) []string { | ||
| t.Helper() | ||
| rows, err := client.DB.QueryContext(t.Context(), "EXPLAIN QUERY PLAN "+recorder.query, recorder.args...) | ||
| require.NoError(t, err) | ||
| defer func() { _ = rows.Close() }() | ||
|
|
||
| var details []string | ||
| for rows.Next() { | ||
| var id, parent, unused int | ||
| var detail string | ||
| require.NoError(t, rows.Scan(&id, &parent, &unused, &detail)) | ||
| details = append(details, detail) | ||
| } | ||
| require.NoError(t, rows.Err()) | ||
| return details | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two optional follow-ups, not blockers:
block_idalso filter onservice_id:GetBlocksForBlockTripIndexIDs(route_bte.service_id/active_bte.service_id IN ...) andGetBlockTripIndexIDsForBlocks(bte.block_id IN ... AND bte.service_id IN ...). An index on(block_id, service_id)would do that filtering inside the index rather than reading each table row. The comment above also only names the first query, so the second one's dependence on this index isn't written down.ANALYZE/PRAGMA optimizeafter a static import, so SQLite picks join orders without statistics. On a feed where one service_id covers most trips, as on NJ Transit, the plan can still start fromactive_bteviaidx_block_trip_entry_service_idand read most active entries before narrowing to the route. RunningPRAGMA optimizeonce after each import would help every multi-join query, not just this one. That probably belongs in a separate issue.Generated by Claude Code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5c1e5ae fixes the comment to name both queries. On RABA without statistics,
GetBlockTripIndexIDsForBlocksactually plans throughidx_block_trip_entry_service_idtoday, so whether(block_id, service_id)wins depends on planner stats. I'd doPRAGMA optimizeafter import as a follow-up first, then revisit the composite index.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed as #1971 (PRAGMA optimize first, then the composite index).