diff --git a/statistics/handle/ddl.go b/statistics/handle/ddl.go index 0b071df4f03f1..852208a3976c7 100644 --- a/statistics/handle/ddl.go +++ b/statistics/handle/ddl.go @@ -353,7 +353,7 @@ func (h *Handle) insertColStats2KV(physicalID int64, colInfos []*model.ColumnInf } } else { // If this stats exists, we insert histogram meta first, the distinct_count will always be one. - if _, err := exec.ExecuteInternal(ctx, "insert into mysql.stats_histograms (version, table_id, is_index, hist_id, distinct_count, tot_col_size) values (%?, %?, 0, %?, 1, %?)", startTS, physicalID, colInfo.ID, int64(len(value.GetBytes()))*count); err != nil { + if _, err := exec.ExecuteInternal(ctx, "insert into mysql.stats_histograms (version, table_id, is_index, hist_id, distinct_count, tot_col_size) values (%?, %?, 0, %?, 1, GREATEST(%?, 0))", startTS, physicalID, colInfo.ID, int64(len(value.GetBytes()))*count); err != nil { return err } value, err = value.ConvertTo(h.mu.ctx.GetSessionVars().StmtCtx, types.NewFieldType(mysql.TypeBlob)) diff --git a/statistics/handle/handle.go b/statistics/handle/handle.go index fbf42ef8a9517..42fbcad57d0a9 100644 --- a/statistics/handle/handle.go +++ b/statistics/handle/handle.go @@ -1765,7 +1765,7 @@ func SaveTableStatsToStorage(sctx sessionctx.Context, results *statistics.Analyz return err } } - if _, err = exec.ExecuteInternal(ctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, %?, %?, %?, %?)", + if _, err = exec.ExecuteInternal(ctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, GREATEST(%?, 0), %?, %?, %?)", tableID, result.IsIndex, hg.ID, hg.NDV, version, hg.NullCount, cmSketch, hg.TotColSize, results.StatsVer, statistics.AnalyzeFlag, hg.Correlation); err != nil { return err } @@ -1872,7 +1872,7 @@ func (h *Handle) SaveStatsToStorage(tableID int64, count, modifyCount int64, isI if isAnalyzed == 1 { flag = statistics.AnalyzeFlag } - if _, err = exec.ExecuteInternal(ctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, %?, %?, %?, %?)", + if _, err = exec.ExecuteInternal(ctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, GREATEST(%?, 0), %?, %?, %?)", tableID, isIndex, hg.ID, hg.NDV, version, hg.NullCount, cmSketch, hg.TotColSize, statsVersion, flag, hg.Correlation); err != nil { return err } diff --git a/statistics/handle/update.go b/statistics/handle/update.go index 2dc48e60c82a2..8e473eac217f9 100644 --- a/statistics/handle/update.go +++ b/statistics/handle/update.go @@ -621,7 +621,7 @@ func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) e } ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats) sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size) "+ - "values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size)", strings.Join(values, ",")) + "values %s on duplicate key update tot_col_size = GREATEST(0, tot_col_size + values(tot_col_size))", strings.Join(values, ",")) _, _, err := h.execRestrictedSQL(ctx, sql) return errors.Trace(err) }