@@ -18,6 +18,7 @@ import (
18
18
"encoding/json"
19
19
"fmt"
20
20
"strconv"
21
+ "sync/atomic"
21
22
"time"
22
23
23
24
"github.com/pingcap/errors"
@@ -35,6 +36,7 @@ import (
35
36
"github.com/pingcap/tidb/util/chunk"
36
37
"github.com/pingcap/tidb/util/logutil"
37
38
"github.com/pingcap/tidb/util/mathutil"
39
+ "github.com/pingcap/tidb/util/memory"
38
40
"github.com/pingcap/tidb/util/sqlexec"
39
41
"go.uber.org/zap"
40
42
)
@@ -220,7 +222,7 @@ func ExtendedStatsFromStorage(sctx sessionctx.Context, table *statistics.Table,
220
222
return table , nil
221
223
}
222
224
223
- func indexStatsFromStorage (sctx sessionctx.Context , row chunk.Row , table * statistics.Table , tableInfo * model.TableInfo , loadAll bool , lease time.Duration ) error {
225
+ func indexStatsFromStorage (sctx sessionctx.Context , row chunk.Row , table * statistics.Table , tableInfo * model.TableInfo , loadAll bool , lease time.Duration , tracker * memory. Tracker ) error {
224
226
histID := row .GetInt64 (2 )
225
227
distinct := row .GetInt64 (3 )
226
228
histVer := row .GetUint64 (4 )
@@ -293,14 +295,17 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
293
295
break
294
296
}
295
297
if idx != nil {
298
+ if tracker != nil {
299
+ tracker .Consume (idx .MemoryUsage ().TotalMemoryUsage ())
300
+ }
296
301
table .Indices [histID ] = idx
297
302
} else {
298
303
logutil .BgLogger ().Debug ("we cannot find index id in table info. It may be deleted." , zap .Int64 ("indexID" , histID ), zap .String ("table" , tableInfo .Name .O ))
299
304
}
300
305
return nil
301
306
}
302
307
303
- func columnStatsFromStorage (sctx sessionctx.Context , row chunk.Row , table * statistics.Table , tableInfo * model.TableInfo , loadAll bool , lease time.Duration ) error {
308
+ func columnStatsFromStorage (sctx sessionctx.Context , row chunk.Row , table * statistics.Table , tableInfo * model.TableInfo , loadAll bool , lease time.Duration , tracker * memory. Tracker ) error {
304
309
histID := row .GetInt64 (2 )
305
310
distinct := row .GetInt64 (3 )
306
311
histVer := row .GetUint64 (4 )
@@ -396,6 +401,9 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
396
401
break
397
402
}
398
403
if col != nil {
404
+ if tracker != nil {
405
+ tracker .Consume (col .MemoryUsage ().TotalMemoryUsage ())
406
+ }
399
407
table .Columns [col .ID ] = col
400
408
} else {
401
409
// If we didn't find a Column or Index in tableInfo, we won't load the histogram for it.
@@ -408,6 +416,9 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
408
416
409
417
// TableStatsFromStorage loads table stats info from storage.
410
418
func TableStatsFromStorage (sctx sessionctx.Context , snapshot uint64 , tableInfo * model.TableInfo , tableID int64 , loadAll bool , lease time.Duration , table * statistics.Table ) (_ * statistics.Table , err error ) {
419
+ tracker := memory .NewTracker (memory .LabelForAnalyzeMemory , - 1 )
420
+ tracker .AttachTo (sctx .GetSessionVars ().MemTracker )
421
+ defer tracker .Detach ()
411
422
// If table stats is pseudo, we also need to copy it, since we will use the column stats when
412
423
// the average error rate of it is small.
413
424
if table == nil || snapshot > 0 {
@@ -439,10 +450,13 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
439
450
return nil , nil
440
451
}
441
452
for _ , row := range rows {
453
+ if atomic .LoadUint32 (& sctx .GetSessionVars ().Killed ) == 1 {
454
+ return nil , errors .Trace (statistics .ErrQueryInterrupted )
455
+ }
442
456
if row .GetInt64 (1 ) > 0 {
443
- err = indexStatsFromStorage (sctx , row , table , tableInfo , loadAll , lease )
457
+ err = indexStatsFromStorage (sctx , row , table , tableInfo , loadAll , lease , tracker )
444
458
} else {
445
- err = columnStatsFromStorage (sctx , row , table , tableInfo , loadAll , lease )
459
+ err = columnStatsFromStorage (sctx , row , table , tableInfo , loadAll , lease , tracker )
446
460
}
447
461
if err != nil {
448
462
return nil , err
0 commit comments