Skip to content

Commit ac6bc7a

Browse files
authored
statistics: avoid oom when to gc large stats_history (#48430) (#48489)
close #48431
1 parent 52c6053 commit ac6bc7a

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

executor/historical_stats_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ func TestAssertHistoricalStatsAfterAlterTable(t *testing.T) {
237237
}
238238

239239
func TestGCOutdatedHistoryStats(t *testing.T) {
240-
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
241-
defer failpoint.Disable("github.com/pingcap/tidb/domain/sendHistoricalStats")
240+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)"))
241+
defer func() {
242+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/domain/sendHistoricalStats"))
243+
}()
242244
store, dom := testkit.CreateMockStoreAndDomain(t)
243245
tk := testkit.NewTestKit(t, store)
244246
tk.MustExec("set global tidb_enable_historical_stats = 1")

statistics/handle/gc.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ func (h *Handle) gcTableStats(is infoschema.InfoSchema, physicalID int64) error
151151
return nil
152152
}
153153

154+
func forCount(total int64, batch int64) int64 {
155+
result := total / batch
156+
if total%batch > 0 {
157+
result++
158+
}
159+
return result
160+
}
161+
154162
// ClearOutdatedHistoryStats clear outdated historical stats
155163
func (h *Handle) ClearOutdatedHistoryStats() error {
156164
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
@@ -172,15 +180,19 @@ func (h *Handle) ClearOutdatedHistoryStats() error {
172180
}
173181
count := rows[0].GetInt64(0)
174182
if count > 0 {
175-
sql = "delete from mysql.stats_meta_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND"
176-
_, err = exec.ExecuteInternal(ctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
177-
if err != nil {
183+
for n := int64(0); n < forCount(count, int64(1000)); n++ {
184+
sql = "delete from mysql.stats_meta_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND limit 1000 "
185+
_, err = exec.ExecuteInternal(ctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
186+
if err != nil {
187+
return err
188+
}
189+
}
190+
for n := int64(0); n < forCount(count, int64(50)); n++ {
191+
sql = "delete from mysql.stats_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND limit 50 "
192+
_, err = exec.ExecuteInternal(ctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
178193
return err
179194
}
180-
sql = "delete from mysql.stats_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND"
181-
_, err = exec.ExecuteInternal(ctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
182195
logutil.BgLogger().Info("clear outdated historical stats")
183-
return err
184196
}
185197
return nil
186198
}

0 commit comments

Comments
 (0)