Skip to content

Commit

Permalink
Add index of start time so that queries with a LIMIT clause can avoid…
Browse files Browse the repository at this point in the history
… a full table scan (for #202)
  • Loading branch information
ddworken committed Aug 11, 2024
1 parent 5290f6d commit ca90ab6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2996,7 +2996,7 @@ func BenchmarkQuery(b *testing.B) {
// Benchmarked code:
b.StartTimer()
ctx := hctx.MakeContext()
_, err := lib.Search(ctx, hctx.GetDb(ctx), "echo", 0)
_, err := lib.Search(ctx, hctx.GetDb(ctx), "echo", 100)
require.NoError(b, err)
b.StopTimer()
}
Expand Down
2 changes: 1 addition & 1 deletion client/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type HistoryEntry struct {
CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"`
HomeDirectory string `json:"home_directory" gorm:"uniqueIndex:compositeindex"`
ExitCode int `json:"exit_code" gorm:"uniqueIndex:compositeindex"`
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex,index:start_time_index"`
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex,index:end_time_index"`
DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"`
EntryId string `json:"entry_id" gorm:"uniqueIndex:compositeindex,uniqueIndex:entry_id_index"`
Expand Down
1 change: 1 addition & 0 deletions client/hctx/hctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
}
db.AutoMigrate(&data.HistoryEntry{})
db.Exec("PRAGMA journal_mode = WAL")
db.Exec("CREATE INDEX IF NOT EXISTS start_time_index ON history_entries(start_time)")
db.Exec("CREATE INDEX IF NOT EXISTS end_time_index ON history_entries(end_time)")
db.Exec("CREATE INDEX IF NOT EXISTS entry_id_index ON history_entries(entry_id)")
return db, nil
Expand Down

0 comments on commit ca90ab6

Please sign in to comment.