diff --git a/sqle/api/controller/v1/task.go b/sqle/api/controller/v1/task.go index c39a319fe..4af23db0b 100644 --- a/sqle/api/controller/v1/task.go +++ b/sqle/api/controller/v1/task.go @@ -1113,7 +1113,7 @@ func AuditTaskGroupV1(c echo.Context) error { } } - if err := s.Save(taskGroup.Tasks); err != nil { + if err := s.SaveTasksWithoutAssociationsAndBatchCreateSQLs(taskGroup.Tasks, 50); err != nil { return controller.JSONBaseErrorReq(c, err) } diff --git a/sqle/model/task.go b/sqle/model/task.go index 39930418a..708b358c2 100644 --- a/sqle/model/task.go +++ b/sqle/model/task.go @@ -787,6 +787,31 @@ type TaskGroup struct { Tasks []*Task `json:"tasks" gorm:"foreignkey:GroupId"` } +func (s *Storage) SaveTasksWithoutAssociationsAndBatchCreateSQLs(tasks []*Task, sqlBatchSize int) error { + if sqlBatchSize <= 0 { + sqlBatchSize = 50 + } + + return s.Tx(func(txDB *gorm.DB) error { + for _, task := range tasks { + if err := txDB.Session(&gorm.Session{FullSaveAssociations: true}).Omit("ExecuteSQLs").Save(task).Error; err != nil { + return err + } + + if len(task.ExecuteSQLs) == 0 { + continue + } + for _, executeSQL := range task.ExecuteSQLs { + executeSQL.TaskId = task.ID + } + if err := txDB.CreateInBatches(task.ExecuteSQLs, sqlBatchSize).Error; err != nil { + return err + } + } + return nil + }) +} + func (s *Storage) GetTaskGroupByGroupId(groupId uint) (*TaskGroup, error) { taskGroup := &TaskGroup{} err := s.db.Preload("Tasks").