diff --git a/statistics/update.go b/statistics/update.go index d6f43db493a8a..d79ef778ff4e8 100644 --- a/statistics/update.go +++ b/statistics/update.go @@ -17,6 +17,7 @@ import ( "fmt" "math" "strconv" + "strings" "sync" "time" @@ -344,17 +345,16 @@ func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) ( err = finishTransaction(ctx, exec, err) }() version := h.mu.ctx.Txn().StartTS() - - for key, val := range delta.ColSize { - if val == 0 { + values := make([]string, 0, len(delta.ColSize)) + for histID, deltaColSize := range delta.ColSize { + if deltaColSize == 0 { continue } - sql := fmt.Sprintf("update mysql.stats_histograms set version = %d, tot_col_size = tot_col_size + %d where hist_id = %d and table_id = %d and is_index = 0", version, val, key, id) - _, err = exec.Execute(ctx, sql) - if err != nil { - return - } + values = append(values, fmt.Sprintf("(%d, 0, %d, 0, %d, %d)", id, histID, deltaColSize, version)) } + sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size, version) "+ + "values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size), version = values(version)", strings.Join(values, ",")) + _, err = exec.Execute(ctx, sql) return }