Add index of start time so that queries with a LIMIT clause can avoid a full table scan (for #202)

This commit is contained in:
David Dworken 2024-04-14 10:19:46 -07:00
parent 5290f6dfb8
commit ca90ab65c9
No known key found for this signature in database
3 changed files with 3 additions and 2 deletions

View File

@ -2996,7 +2996,7 @@ func BenchmarkQuery(b *testing.B) {
// Benchmarked code: // Benchmarked code:
b.StartTimer() b.StartTimer()
ctx := hctx.MakeContext() 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) require.NoError(b, err)
b.StopTimer() b.StopTimer()
} }

View File

@ -35,7 +35,7 @@ type HistoryEntry struct {
CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"` CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"`
HomeDirectory string `json:"home_directory" gorm:"uniqueIndex:compositeindex"` HomeDirectory string `json:"home_directory" gorm:"uniqueIndex:compositeindex"`
ExitCode int `json:"exit_code" 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"` EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex,index:end_time_index"`
DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"` DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"`
EntryId string `json:"entry_id" gorm:"uniqueIndex:compositeindex,uniqueIndex:entry_id_index"` EntryId string `json:"entry_id" gorm:"uniqueIndex:compositeindex,uniqueIndex:entry_id_index"`

View File

@ -103,6 +103,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
} }
db.AutoMigrate(&data.HistoryEntry{}) db.AutoMigrate(&data.HistoryEntry{})
db.Exec("PRAGMA journal_mode = WAL") 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 end_time_index ON history_entries(end_time)")
db.Exec("CREATE INDEX IF NOT EXISTS entry_id_index ON history_entries(entry_id)") db.Exec("CREATE INDEX IF NOT EXISTS entry_id_index ON history_entries(entry_id)")
return db, nil return db, nil