From 9278c8fac3e332ee03821feaffd23bbfb3a6d475 Mon Sep 17 00:00:00 2001 From: YenchangChan Date: Thu, 1 Feb 2024 09:52:20 +0800 Subject: [PATCH] perf: set alter_sync = 0 when alter and truncate table --- controller/clickhouse.go | 2 +- service/clickhouse/clickhouse_service.go | 12 ++++++------ service/clickhouse/rebalance.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/controller/clickhouse.go b/controller/clickhouse.go index f9e7edca..7c4c10d8 100644 --- a/controller/clickhouse.go +++ b/controller/clickhouse.go @@ -548,7 +548,7 @@ func (controller *ClickHouseController) TruncateTable(c *gin.Context) { } database := c.Query("database") table := c.Query("tableName") - query := fmt.Sprintf("TRUNCATE TABLE IF EXISTS `%s`.`%s`", database, table) + query := fmt.Sprintf("TRUNCATE TABLE IF EXISTS `%s`.`%s` SETTINGS alter_sync = 0", database, table) var lastErr error var wg sync.WaitGroup for _, host := range hosts { diff --git a/service/clickhouse/clickhouse_service.go b/service/clickhouse/clickhouse_service.go index 13089038..14999c52 100644 --- a/service/clickhouse/clickhouse_service.go +++ b/service/clickhouse/clickhouse_service.go @@ -447,7 +447,7 @@ func (ck *CkService) AlterTable(params *model.AlterCkTableParams) error { rows.Close() } - modify := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` MODIFY COLUMN IF EXISTS `%s` %s %s", + modify := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` MODIFY COLUMN IF EXISTS `%s` %s %s SETTINGS alter_sync = 0", params.DB, local, params.Cluster, value.Name, value.Type, strings.Join(value.Options, " ")) log.Logger.Debugf(modify) if err := ck.Conn.Exec(modify); err != nil { @@ -457,7 +457,7 @@ func (ck *CkService) AlterTable(params *model.AlterCkTableParams) error { // delete column for _, value := range params.Drop { - drop := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` DROP COLUMN IF EXISTS `%s`", + drop := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` DROP COLUMN IF EXISTS `%s` SETTINGS alter_sync = 0", params.DB, local, params.Cluster, value) log.Logger.Debugf(drop) if err := ck.Conn.Exec(drop); err != nil { @@ -470,7 +470,7 @@ func (ck *CkService) AlterTable(params *model.AlterCkTableParams) error { if value.From == "" || value.To == "" { return errors.Errorf("form %s or to %s must not be empty", value.From, value.To) } - rename := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` RENAME COLUMN IF EXISTS `%s` TO `%s`", + rename := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` RENAME COLUMN IF EXISTS `%s` TO `%s` SETTINGS alter_sync = 0", params.DB, local, params.Cluster, value.From, value.To) log.Logger.Debugf(rename) @@ -576,14 +576,14 @@ func (ck *CkService) AlterTableTTL(req *model.AlterTblsTTLReq) error { if req.TTLType != "" { if req.TTLType == model.TTLTypeModify { if req.TTLExpr != "" { - ttl := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` MODIFY TTL %s", table.Database, local, ck.Config.Cluster, req.TTLExpr) + ttl := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` MODIFY TTL %s SETTINGS alter_sync = 0", table.Database, local, ck.Config.Cluster, req.TTLExpr) log.Logger.Debugf(ttl) if err := ck.Conn.Exec(ttl); err != nil { return errors.Wrap(err, "") } } } else if req.TTLType == model.TTLTypeRemove { - ttl := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` REMOVE TTL", table.Database, local, ck.Config.Cluster) + ttl := fmt.Sprintf("ALTER TABLE `%s`.`%s` ON CLUSTER `%s` REMOVE TTL SETTINGS alter_sync = 0", table.Database, local, ck.Config.Cluster) log.Logger.Debugf(ttl) if err := ck.Conn.Exec(ttl); err != nil { return errors.Wrap(err, "") @@ -1795,7 +1795,7 @@ func MoveExceptToOthers(conf *model.CKManClickHouseConfig, except, target, datab if err != nil { return err } - query = fmt.Sprintf("TRUNCATE TABLE `%s`.`%s`", database, table) + query = fmt.Sprintf("TRUNCATE TABLE `%s`.`%s` SETTINGS alter_sync = 0", database, table) log.Logger.Debugf("[%s] %s", except, query) conn = common.GetConnection(except) err = conn.Exec(query) diff --git a/service/clickhouse/rebalance.go b/service/clickhouse/rebalance.go index 7976202c..1df9df5f 100644 --- a/service/clickhouse/rebalance.go +++ b/service/clickhouse/rebalance.go @@ -426,7 +426,7 @@ func (r *CKRebalance) MoveBack() error { defer wg.Done() // truncate and detach ori table tableName := fmt.Sprintf("tmp_%s", r.Table) - query := fmt.Sprintf("TRUNCATE TABLE `%s`.`%s`", r.Database, r.Table) + query := fmt.Sprintf("TRUNCATE TABLE `%s`.`%s` SETTINGS alter_sync = 0", r.Database, r.Table) conn := common.GetConnection(host) log.Logger.Debugf("[%s]%s", host, query) if err = conn.Exec(query); err != nil {