@@ -117,6 +117,16 @@ func (h *Handle) initStatsMeta(is infoschema.InfoSchema) (util.StatsCache, error
117
117
return tables , nil
118
118
}
119
119
120
+ // initStatsHistogramsSQLGen generates the SQL to load all stats_histograms records.
121
+ func initStatsHistogramsSQLGen (isPaging bool ) string {
122
+ selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_histograms,tbl) */ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms"
123
+ orderSuffix := " order by table_id"
124
+ if ! isPaging {
125
+ return selectPrefix + orderSuffix
126
+ }
127
+ return selectPrefix + " where table_id >= %? and table_id < %?" + orderSuffix
128
+ }
129
+
120
130
func (h * Handle ) initStatsHistograms4ChunkLite (is infoschema.InfoSchema , cache util.StatsCache , iter * chunk.Iterator4Chunk ) {
121
131
var table * statistics.Table
122
132
for row := iter .Begin (); row != iter .End (); row = iter .Next () {
@@ -137,9 +147,9 @@ func (h *Handle) initStatsHistograms4ChunkLite(is infoschema.InfoSchema, cache u
137
147
ndv := row .GetInt64 (3 )
138
148
version := row .GetUint64 (4 )
139
149
nullCount := row .GetInt64 (5 )
140
- statsVer := row .GetInt64 (7 )
141
- flag := row .GetInt64 (9 )
142
- lastAnalyzePos := row .GetDatum (10 , types .NewFieldType (mysql .TypeBlob ))
150
+ statsVer := row .GetInt64 (8 )
151
+ flag := row .GetInt64 (10 )
152
+ lastAnalyzePos := row .GetDatum (11 , types .NewFieldType (mysql .TypeBlob ))
143
153
tbl , _ := h .TableInfoByID (is , table .PhysicalID )
144
154
if isIndex > 0 {
145
155
var idxInfo * model.IndexInfo
@@ -176,7 +186,7 @@ func (h *Handle) initStatsHistograms4ChunkLite(is infoschema.InfoSchema, cache u
176
186
if colInfo == nil {
177
187
continue
178
188
}
179
- hist := statistics .NewHistogram (id , ndv , nullCount , version , & colInfo .FieldType , 0 , row .GetInt64 (6 ))
189
+ hist := statistics .NewHistogram (id , ndv , nullCount , version , & colInfo .FieldType , 0 , row .GetInt64 (7 ))
180
190
hist .Correlation = row .GetFloat64 (8 )
181
191
col := & statistics.Column {
182
192
Histogram : * hist ,
@@ -288,7 +298,9 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache util.
288
298
StatsVer : statsVer ,
289
299
}
290
300
// primary key column has no stats info, because primary key's is_index is false. so it cannot load the topn
291
- col .StatsLoadedStatus = statistics .NewStatsAllEvictedStatus ()
301
+ if col .StatsAvailable () {
302
+ col .StatsLoadedStatus = statistics .NewStatsAllEvictedStatus ()
303
+ }
292
304
lastAnalyzePos .Copy (& col .LastAnalyzePos )
293
305
table .Columns [hist .ID ] = col
294
306
}
@@ -299,7 +311,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache util.
299
311
}
300
312
301
313
func (h * Handle ) initStatsHistogramsLite (is infoschema.InfoSchema , cache util.StatsCache ) error {
302
- sql := "select /*+ ORDER_INDEX(mysql.stats_histograms,tbl)*/ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms order by table_id"
314
+ sql := initStatsHistogramsSQLGen ( false )
303
315
rc , err := util .Exec (h .initStatsCtx , sql )
304
316
if err != nil {
305
317
return errors .Trace (err )
@@ -322,7 +334,7 @@ func (h *Handle) initStatsHistogramsLite(is infoschema.InfoSchema, cache util.St
322
334
}
323
335
324
336
func (h * Handle ) initStatsHistograms (is infoschema.InfoSchema , cache util.StatsCache ) error {
325
- sql := "select /*+ ORDER_INDEX(mysql.stats_histograms,tbl)*/ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms order by table_id"
337
+ sql := initStatsHistogramsSQLGen ( false )
326
338
rc , err := util .Exec (h .initStatsCtx , sql )
327
339
if err != nil {
328
340
return errors .Trace (err )
@@ -359,10 +371,7 @@ func (h *Handle) initStatsHistogramsByPaging(is infoschema.InfoSchema, cache uti
359
371
}()
360
372
361
373
sctx := se .(sessionctx.Context )
362
- // Why do we need to add `is_index=1` in the SQL?
363
- // because it is aligned to the `initStatsTopN` function, which only loads the topn of the index too.
364
- // the other will be loaded by sync load.
365
- sql := "select HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms where table_id >= %? and table_id < %? and is_index=1"
374
+ sql := initStatsHistogramsSQLGen (true )
366
375
rc , err := util .Exec (sctx , sql , task .StartTid , task .EndTid )
367
376
if err != nil {
368
377
return errors .Trace (err )
@@ -676,7 +685,7 @@ func (h *Handle) initStatsBuckets(cache util.StatsCache, totalMemory uint64) err
676
685
return errors .Trace (err )
677
686
}
678
687
} else {
679
- sql := "select /*+ ORDER_INDEX(mysql.stats_buckets,tbl)*/ HIGH_PRIORITY table_id, is_index, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets order by table_id, is_index, hist_id, bucket_id"
688
+ sql := "select /*+ ORDER_INDEX(mysql.stats_buckets,tbl)*/ HIGH_PRIORITY table_id, is_index, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets where is_index=1 order by table_id, is_index, hist_id, bucket_id"
680
689
rc , err := util .Exec (h .initStatsCtx , sql )
681
690
if err != nil {
682
691
return errors .Trace (err )
@@ -729,7 +738,7 @@ func (h *Handle) initStatsBucketsByPaging(cache util.StatsCache, task initstats.
729
738
}
730
739
}()
731
740
sctx := se .(sessionctx.Context )
732
- sql := "select HIGH_PRIORITY table_id, is_index, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets where table_id >= %? and table_id < %? order by table_id, is_index, hist_id, bucket_id"
741
+ sql := "select HIGH_PRIORITY table_id, is_index, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets where is_index = 1 and table_id >= %? and table_id < %? order by table_id, is_index, hist_id, bucket_id"
733
742
rc , err := util .Exec (sctx , sql , task .StartTid , task .EndTid )
734
743
if err != nil {
735
744
return errors .Trace (err )
0 commit comments