From a7d1658ff402fffd3c65dcb4556d5e3bbea3670a Mon Sep 17 00:00:00 2001 From: Pablo Casares Crespo Date: Fri, 9 Feb 2024 11:23:41 +0100 Subject: [PATCH] Add composite index for executions Signed-off-by: Pablo Casares Crespo --- flyteadmin/pkg/repositories/config/migrations.go | 10 ++++++++++ flyteadmin/pkg/repositories/models/execution.go | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/flyteadmin/pkg/repositories/config/migrations.go b/flyteadmin/pkg/repositories/config/migrations.go index 891a80db1d..1df3063434 100644 --- a/flyteadmin/pkg/repositories/config/migrations.go +++ b/flyteadmin/pkg/repositories/config/migrations.go @@ -1180,6 +1180,16 @@ var NoopMigrations = []*gormigrate.Migration{ return tx.AutoMigrate(&Execution{}) }, }, + //Add composite index for executions table on execution_project, execution_domain, phase, created_at and state + { + ID: "2024-02-09-executions-composite-idx", + Migrate: func(tx *gorm.DB) error { + return tx.AutoMigrate(&models.Execution{}) + }, + Rollback: func(tx *gorm.DB) error { + return tx.Migrator().DropIndex("execution_project_domain_phase_created_at_state_idx") + }, + }, } var Migrations = append(LegacyMigrations, NoopMigrations...) diff --git a/flyteadmin/pkg/repositories/models/execution.go b/flyteadmin/pkg/repositories/models/execution.go index 4c236e093d..0b977d84cd 100644 --- a/flyteadmin/pkg/repositories/models/execution.go +++ b/flyteadmin/pkg/repositories/models/execution.go @@ -14,8 +14,8 @@ import ( // Execution primary key type ExecutionKey struct { - Project string `gorm:"primary_key;column:execution_project" valid:"length(0|255)"` - Domain string `gorm:"primary_key;column:execution_domain" valid:"length(0|255)"` + Project string `gorm:"primary_key;column:execution_project;index:execution_project_domain_phase_created_at_state_idx,priority:1" valid:"length(0|255)"` + Domain string `gorm:"primary_key;column:execution_domain;index:execution_project_domain_phase_created_at_state_idx,priority:2" valid:"length(0|255)"` Name string `gorm:"primary_key;column:execution_name" valid:"length(0|255)"` } @@ -26,13 +26,13 @@ type Execution struct { LaunchPlanID uint `gorm:"index"` WorkflowID uint `gorm:"index"` TaskID uint `gorm:"index"` - Phase string `valid:"length(0|255)"` + Phase string `gorm:"index:execution_project_domain_phase_created_at_state_idx,priority:3" valid:"length(0|255)"` Closure []byte Spec []byte `gorm:"not null"` StartedAt *time.Time // Corresponds to the CreatedAt field in the Execution closure. // Prefixed with Execution to avoid clashes with gorm.Model CreatedAt - ExecutionCreatedAt *time.Time `gorm:"index:idx_executions_created_at"` + ExecutionCreatedAt *time.Time `gorm:"index:idx_executions_created_at;index:execution_project_domain_phase_created_at_state_idx,priority:4"` // Corresponds to the UpdatedAt field in the Execution closure // Prefixed with Execution to avoid clashes with gorm.Model UpdatedAt ExecutionUpdatedAt *time.Time @@ -60,7 +60,7 @@ type Execution struct { // This is also stored in the spec but promoted as a column for filtering. User string `gorm:"index" valid:"length(0|255)"` // GORM doesn't save the zero value for ints, so we use a pointer for the State field - State *int32 `gorm:"index;default:0"` + State *int32 `gorm:"index;default:0;index:execution_project_domain_phase_created_at_state_idx,priority:5"` // The resource type of the entity used to launch the execution, one of 'launch_plan' or 'task' LaunchEntity string // Tags associated with the execution