@@ -151,6 +151,14 @@ func (h *Handle) gcTableStats(is infoschema.InfoSchema, physicalID int64) error
151
151
return nil
152
152
}
153
153
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
+
154
162
// ClearOutdatedHistoryStats clear outdated historical stats
155
163
func (h * Handle ) ClearOutdatedHistoryStats () error {
156
164
ctx := kv .WithInternalSourceType (context .Background (), kv .InternalTxnStats )
@@ -172,15 +180,19 @@ func (h *Handle) ClearOutdatedHistoryStats() error {
172
180
}
173
181
count := rows [0 ].GetInt64 (0 )
174
182
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 ())
178
193
return err
179
194
}
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 ())
182
195
logutil .BgLogger ().Info ("clear outdated historical stats" )
183
- return err
184
196
}
185
197
return nil
186
198
}
0 commit comments