@@ -282,9 +282,9 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
282
282
}
283
283
}
284
284
285
- // initStatsHistogramsSQLGen generates the SQL to load all stats_histograms records.
285
+ // genInitStatsHistogramsSQL generates the SQL to load all stats_histograms records.
286
286
// We need to read all the records since we need to do initialization of table.ColAndIdxExistenceMap.
287
- func initStatsHistogramsSQLGen (isPaging bool ) string {
287
+ func genInitStatsHistogramsSQL (isPaging bool ) string {
288
288
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"
289
289
orderSuffix := " order by table_id"
290
290
if ! isPaging {
@@ -294,7 +294,7 @@ func initStatsHistogramsSQLGen(isPaging bool) string {
294
294
}
295
295
296
296
func (h * Handle ) initStatsHistogramsLite (ctx context.Context , cache statstypes.StatsCache ) error {
297
- sql := initStatsHistogramsSQLGen (false )
297
+ sql := genInitStatsHistogramsSQL (false )
298
298
rc , err := util .Exec (h .initStatsCtx , sql )
299
299
if err != nil {
300
300
return errors .Trace (err )
@@ -317,7 +317,7 @@ func (h *Handle) initStatsHistogramsLite(ctx context.Context, cache statstypes.S
317
317
}
318
318
319
319
func (h * Handle ) initStatsHistograms (is infoschema.InfoSchema , cache statstypes.StatsCache ) error {
320
- sql := initStatsHistogramsSQLGen (false )
320
+ sql := genInitStatsHistogramsSQL (false )
321
321
rc , err := util .Exec (h .initStatsCtx , sql )
322
322
if err != nil {
323
323
return errors .Trace (err )
@@ -351,7 +351,7 @@ func (h *Handle) initStatsHistogramsByPaging(is infoschema.InfoSchema, cache sta
351
351
}()
352
352
353
353
sctx := se .(sessionctx.Context )
354
- sql := initStatsHistogramsSQLGen (true )
354
+ sql := genInitStatsHistogramsSQL (true )
355
355
rc , err := util .Exec (sctx , sql , task .StartTid , task .EndTid )
356
356
if err != nil {
357
357
return errors .Trace (err )
@@ -430,10 +430,10 @@ func (*Handle) initStatsTopN4Chunk(cache statstypes.StatsCache, iter *chunk.Iter
430
430
}
431
431
}
432
432
433
- // initStatsTopNSQLGen generates the SQL to load all stats_top_n records.
433
+ // genInitStatsTopNSQLForIndexes generates the SQL to load all stats_top_n records for indexes .
434
434
// We only need to load the indexes' since we only record the existence of columns in ColAndIdxExistenceMap.
435
435
// The stats of the column is not loaded during the bootstrap process.
436
- func initStatsTopNSQLGen (isPaging bool ) string {
436
+ func genInitStatsTopNSQLForIndexes (isPaging bool ) string {
437
437
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_top_n,tbl) */ HIGH_PRIORITY table_id, hist_id, value, count from mysql.stats_top_n where is_index = 1"
438
438
orderSuffix := " order by table_id"
439
439
if ! isPaging {
@@ -443,7 +443,7 @@ func initStatsTopNSQLGen(isPaging bool) string {
443
443
}
444
444
445
445
func (h * Handle ) initStatsTopN (cache statstypes.StatsCache , totalMemory uint64 ) error {
446
- sql := initStatsTopNSQLGen (false )
446
+ sql := genInitStatsTopNSQLForIndexes (false )
447
447
rc , err := util .Exec (h .initStatsCtx , sql )
448
448
if err != nil {
449
449
return errors .Trace (err )
@@ -476,7 +476,7 @@ func (h *Handle) initStatsTopNByPaging(cache statstypes.StatsCache, task initsta
476
476
}
477
477
}()
478
478
sctx := se .(sessionctx.Context )
479
- sql := initStatsTopNSQLGen (true )
479
+ sql := genInitStatsTopNSQLForIndexes (true )
480
480
rc , err := util .Exec (sctx , sql , task .StartTid , task .EndTid )
481
481
if err != nil {
482
482
return errors .Trace (err )
@@ -577,14 +577,13 @@ func (h *Handle) initStatsFMSketch(cache statstypes.StatsCache) error {
577
577
578
578
func (* Handle ) initStatsBuckets4Chunk (cache statstypes.StatsCache , iter * chunk.Iterator4Chunk ) {
579
579
var table * statistics.Table
580
- unspecifiedLengthTp := types .NewFieldType (mysql .TypeBlob )
581
580
var (
582
581
hasErr bool
583
582
failedTableID int64
584
583
failedHistID int64
585
584
)
586
585
for row := iter .Begin (); row != iter .End (); row = iter .Next () {
587
- tableID , isIndex , histID := row .GetInt64 (0 ), row .GetInt64 (1 ), row . GetInt64 ( 2 )
586
+ tableID , histID := row .GetInt64 (0 ), row .GetInt64 (1 )
588
587
if table == nil || table .PhysicalID != tableID {
589
588
if table != nil {
590
589
table .SetAllIndexFullLoadForBootstrap ()
@@ -599,58 +598,13 @@ func (*Handle) initStatsBuckets4Chunk(cache statstypes.StatsCache, iter *chunk.I
599
598
}
600
599
var lower , upper types.Datum
601
600
var hist * statistics.Histogram
602
- if isIndex > 0 {
603
- index := table .GetIdx (histID )
604
- if index == nil {
605
- continue
606
- }
607
- hist = & index .Histogram
608
- lower , upper = types .NewBytesDatum (row .GetBytes (5 )), types .NewBytesDatum (row .GetBytes (6 ))
609
- } else {
610
- column := table .GetCol (histID )
611
- if column == nil {
612
- continue
613
- }
614
- if ! mysql .HasPriKeyFlag (column .Info .GetFlag ()) {
615
- continue
616
- }
617
- hist = & column .Histogram
618
- d := types .NewBytesDatum (row .GetBytes (5 ))
619
- var err error
620
- if column .Info .FieldType .EvalType () == types .ETString && column .Info .FieldType .GetType () != mysql .TypeEnum && column .Info .FieldType .GetType () != mysql .TypeSet {
621
- // For new collation data, when storing the bounds of the histogram, we store the collate key instead of the
622
- // original value.
623
- // But there's additional conversion logic for new collation data, and the collate key might be longer than
624
- // the FieldType.flen.
625
- // If we use the original FieldType here, there might be errors like "Invalid utf8mb4 character string"
626
- // or "Data too long".
627
- // So we change it to TypeBlob to bypass those logics here.
628
- lower , err = d .ConvertTo (statistics .UTCWithAllowInvalidDateCtx , unspecifiedLengthTp )
629
- } else {
630
- lower , err = d .ConvertTo (statistics .UTCWithAllowInvalidDateCtx , & column .Info .FieldType )
631
- }
632
- if err != nil {
633
- hasErr = true
634
- failedTableID = tableID
635
- failedHistID = histID
636
- table .DelCol (histID )
637
- continue
638
- }
639
- d = types .NewBytesDatum (row .GetBytes (6 ))
640
- if column .Info .FieldType .EvalType () == types .ETString && column .Info .FieldType .GetType () != mysql .TypeEnum && column .Info .FieldType .GetType () != mysql .TypeSet {
641
- upper , err = d .ConvertTo (statistics .UTCWithAllowInvalidDateCtx , unspecifiedLengthTp )
642
- } else {
643
- upper , err = d .ConvertTo (statistics .UTCWithAllowInvalidDateCtx , & column .Info .FieldType )
644
- }
645
- if err != nil {
646
- hasErr = true
647
- failedTableID = tableID
648
- failedHistID = histID
649
- table .DelCol (histID )
650
- continue
651
- }
601
+ index := table .GetIdx (histID )
602
+ if index == nil {
603
+ continue
652
604
}
653
- hist .AppendBucketWithNDV (& lower , & upper , row .GetInt64 (3 ), row .GetInt64 (4 ), row .GetInt64 (7 ))
605
+ hist = & index .Histogram
606
+ lower , upper = types .NewBytesDatum (row .GetBytes (4 ) /*lower_bound*/ ), types .NewBytesDatum (row .GetBytes (5 ) /*upper_bound*/ )
607
+ hist .AppendBucketWithNDV (& lower , & upper , row .GetInt64 (2 ) /*count*/ , row .GetInt64 (3 ) /*repeats*/ , row .GetInt64 (6 ) /*ndv*/ )
654
608
}
655
609
if table != nil {
656
610
cache .Put (table .PhysicalID , table ) // put this table in the cache because all statstics of the table have been read.
@@ -660,11 +614,11 @@ func (*Handle) initStatsBuckets4Chunk(cache statstypes.StatsCache, iter *chunk.I
660
614
}
661
615
}
662
616
663
- // initStatsBucketsSQLGen generates the SQL to load all stats_top_n records.
617
+ // genInitStatsBucketsSQLForIndexes generates the SQL to load all stats_buckets records for indexes .
664
618
// We only need to load the indexes' since we only record the existence of columns in ColAndIdxExistenceMap.
665
619
// The stats of the column is not loaded during the bootstrap process.
666
- func initStatsBucketsSQLGen (isPaging bool ) string {
667
- selectPrefix := "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"
620
+ func genInitStatsBucketsSQLForIndexes (isPaging bool ) string {
621
+ selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_buckets,tbl) */ HIGH_PRIORITY table_id, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets where is_index=1"
668
622
orderSuffix := " order by table_id"
669
623
if ! isPaging {
670
624
return selectPrefix + orderSuffix
@@ -682,7 +636,7 @@ func (h *Handle) initStatsBuckets(cache statstypes.StatsCache, totalMemory uint6
682
636
return errors .Trace (err )
683
637
}
684
638
} else {
685
- sql := initStatsBucketsSQLGen (false )
639
+ sql := genInitStatsBucketsSQLForIndexes (false )
686
640
rc , err := util .Exec (h .initStatsCtx , sql )
687
641
if err != nil {
688
642
return errors .Trace (err )
@@ -721,7 +675,7 @@ func (h *Handle) initStatsBucketsByPaging(cache statstypes.StatsCache, task init
721
675
}
722
676
}()
723
677
sctx := se .(sessionctx.Context )
724
- sql := initStatsBucketsSQLGen (true )
678
+ sql := genInitStatsBucketsSQLForIndexes (true )
725
679
rc , err := util .Exec (sctx , sql , task .StartTid , task .EndTid )
726
680
if err != nil {
727
681
return errors .Trace (err )
0 commit comments