Skip to content

Commit

Permalink
perf: set alter_sync = 0 when alter and truncate table
Browse files Browse the repository at this point in the history
  • Loading branch information
YenchangChan committed Feb 1, 2024
1 parent aa0120d commit 9278c8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion controller/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions service/clickhouse/clickhouse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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, "")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion service/clickhouse/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9278c8f

Please sign in to comment.