@@ -68,21 +68,26 @@ func (*Handle) initStatsMeta4Chunk(cache statstypes.StatsCache, iter *chunk.Iter
68
68
physicalID = row .GetInt64 (1 )
69
69
maxPhysicalID = max (physicalID , maxPhysicalID )
70
70
newHistColl := * statistics .NewHistColl (physicalID , row .GetInt64 (3 ), row .GetInt64 (2 ), 4 , 4 )
71
+ // During the initialization phase, we need to initialize LastAnalyzeVersion with the snapshot,
72
+ // which ensures that we don't duplicate the auto-analyze of a particular type of table.
73
+ // When the predicate columns feature is turned on, if a table has neither predicate columns nor indexes,
74
+ // then auto-analyze will only analyze the _row_id and refresh stats_meta,
75
+ // but since we don't have any histograms or topn's created for _row_id at the moment.
76
+ // So if we don't initialize LastAnalyzeVersion with the snapshot here,
77
+ // it will stay at 0 and auto-analyze won't be able to detect that the table has been analyzed.
78
+ // But in the future, we maybe will create some records for _row_id, see:
79
+ // https://github.com/pingcap/tidb/issues/51098
71
80
snapshot := row .GetUint64 (4 )
81
+ lastAnalyzeVersion , lastStatsHistUpdateVersion := snapshot , snapshot
82
+ if ! row .IsNull (5 ) {
83
+ lastStatsHistUpdateVersion = max (lastStatsHistUpdateVersion , row .GetUint64 (5 ))
84
+ }
72
85
tbl := & statistics.Table {
73
86
HistColl : newHistColl ,
74
87
Version : row .GetUint64 (0 ),
75
88
ColAndIdxExistenceMap : statistics .NewColAndIndexExistenceMapWithoutSize (),
76
- // During the initialization phase, we need to initialize LastAnalyzeVersion with the snapshot,
77
- // which ensures that we don't duplicate the auto-analyze of a particular type of table.
78
- // When the predicate columns feature is turned on, if a table has neither predicate columns nor indexes,
79
- // then auto-analyze will only analyze the _row_id and refresh stats_meta,
80
- // but since we don't have any histograms or topn's created for _row_id at the moment.
81
- // So if we don't initialize LastAnalyzeVersion with the snapshot here,
82
- // it will stay at 0 and auto-analyze won't be able to detect that the table has been analyzed.
83
- // But in the future, we maybe will create some records for _row_id, see:
84
- // https://github.com/pingcap/tidb/issues/51098
85
- LastAnalyzeVersion : snapshot ,
89
+ LastAnalyzeVersion : lastAnalyzeVersion ,
90
+ LastStatsHistVersion : lastStatsHistUpdateVersion ,
86
91
}
87
92
cache .Put (physicalID , tbl ) // put this table again since it is updated
88
93
}
@@ -95,7 +100,7 @@ func (*Handle) initStatsMeta4Chunk(cache statstypes.StatsCache, iter *chunk.Iter
95
100
96
101
func (h * Handle ) initStatsMeta (ctx context.Context ) (statstypes.StatsCache , error ) {
97
102
ctx = kv .WithInternalSourceType (ctx , kv .InternalTxnStats )
98
- sql := "select HIGH_PRIORITY version, table_id, modify_count, count, snapshot from mysql.stats_meta"
103
+ sql := "select HIGH_PRIORITY version, table_id, modify_count, count, snapshot, last_stats_histograms_version from mysql.stats_meta"
99
104
rc , err := util .Exec (h .initStatsCtx , sql )
100
105
if err != nil {
101
106
return nil , errors .Trace (err )
0 commit comments