Skip to content

Commit

Permalink
*: not load unnecessay data during the non-lite-init-stats (#53399)
Browse files Browse the repository at this point in the history
close #53425
  • Loading branch information
hawkingrei authored May 22, 2024
1 parent a6b4fca commit e954bcf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
5 changes: 2 additions & 3 deletions pkg/planner/cardinality/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,12 +1338,11 @@ func TestBuiltinInEstWithoutStats(t *testing.T) {
"└─Selection 0.08 cop[tikv] in(test.t.a, 1, 2, 3, 4, 5, 6, 7, 8)",
" └─TableFullScan 10.00 cop[tikv] table:t keep order:false, stats:pseudo",
))

h.Clear()
require.NoError(t, h.InitStats(is))
tk.MustQuery("explain format='brief' select * from t where a in (1, 2, 3, 4, 5, 6, 7, 8)").Check(testkit.Rows(
"TableReader 0.08 root data:Selection",
"└─Selection 0.08 cop[tikv] in(test.t.a, 1, 2, 3, 4, 5, 6, 7, 8)",
"TableReader 8.00 root data:Selection",
"└─Selection 8.00 cop[tikv] in(test.t.a, 1, 2, 3, 4, 5, 6, 7, 8)",
" └─TableFullScan 10.00 cop[tikv] table:t keep order:false, stats:pseudo",
))
}
5 changes: 4 additions & 1 deletion pkg/statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,10 @@ func (h *Handle) initStatsHistogramsByPaging(is infoschema.InfoSchema, cache sta
}
}()
sctx := se.(sessionctx.Context)
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 < %?"
// Why do we need to add `is_index=1` in the SQL?
// because it is aligned to the `initStatsTopN` function, which only loads the topn of the index too.
// the other will be loaded by sync load.
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"
rc, err := util.Exec(sctx, sql, task.StartTid, task.EndTid)
if err != nil {
return errors.Trace(err)
Expand Down
66 changes: 34 additions & 32 deletions pkg/statistics/handle/handletest/statstest/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,12 @@ func TestInitStats(t *testing.T) {
h.Clear()
require.NoError(t, h.InitStats(is))
table0 := h.GetTableStats(tbl.Meta())
cols := table0.Columns
require.Equal(t, uint8(0x36), cols[1].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x37), cols[2].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x38), cols[3].LastAnalyzePos.GetBytes()[0])
idx := table0.Indices
require.Equal(t, uint8(0x3), idx[1].LastAnalyzePos.GetBytes()[0])
h.Clear()
require.NoError(t, h.Update(is))
// Index and pk are loaded.
needed := fmt.Sprintf(`Table:%v RealtimeCount:6
column:1 ndv:6 totColSize:0
num: 1 lower_bound: 1 upper_bound: 1 repeats: 1 ndv: 0
num: 1 lower_bound: 2 upper_bound: 2 repeats: 1 ndv: 0
num: 1 lower_bound: 3 upper_bound: 3 repeats: 1 ndv: 0
num: 1 lower_bound: 4 upper_bound: 4 repeats: 1 ndv: 0
num: 1 lower_bound: 5 upper_bound: 5 repeats: 1 ndv: 0
num: 1 lower_bound: 6 upper_bound: 6 repeats: 1 ndv: 0
column:2 ndv:6 totColSize:6
column:3 ndv:6 totColSize:6
index:1 ndv:6
num: 1 lower_bound: 1 upper_bound: 1 repeats: 1 ndv: 0
num: 1 lower_bound: 2 upper_bound: 2 repeats: 1 ndv: 0
Expand Down Expand Up @@ -328,26 +317,30 @@ func TestInitStats51358(t *testing.T) {
}

func TestInitStatsVer2(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
config.GetGlobalConfig().Performance.LiteInitStats = false
config.GetGlobalConfig().Performance.ConcurrentlyInitStats = false
})
initStatsVer2(t)
originValue := config.GetGlobalConfig().Performance.LiteInitStats
concurrentlyInitStatsValue := config.GetGlobalConfig().Performance.ConcurrentlyInitStats
defer func() {
config.GetGlobalConfig().Performance.LiteInitStats = originValue
config.GetGlobalConfig().Performance.ConcurrentlyInitStats = concurrentlyInitStatsValue
}()
config.GetGlobalConfig().Performance.LiteInitStats = false
config.GetGlobalConfig().Performance.ConcurrentlyInitStats = false
initStatsVer2(t, false)
}

func TestInitStatsVer2Concurrency(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
config.GetGlobalConfig().Performance.LiteInitStats = false
config.GetGlobalConfig().Performance.ConcurrentlyInitStats = true
})
initStatsVer2(t)
originValue := config.GetGlobalConfig().Performance.LiteInitStats
concurrentlyInitStatsValue := config.GetGlobalConfig().Performance.ConcurrentlyInitStats
defer func() {
config.GetGlobalConfig().Performance.LiteInitStats = originValue
config.GetGlobalConfig().Performance.ConcurrentlyInitStats = concurrentlyInitStatsValue
}()
config.GetGlobalConfig().Performance.LiteInitStats = false
config.GetGlobalConfig().Performance.ConcurrentlyInitStats = true
initStatsVer2(t, true)
}

func initStatsVer2(t *testing.T) {
func initStatsVer2(t *testing.T, isConcurrency bool) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -366,10 +359,19 @@ func initStatsVer2(t *testing.T) {
h.Clear()
require.NoError(t, h.InitStats(is))
table0 := h.GetTableStats(tbl.Meta())
cols := table0.Columns
require.Equal(t, uint8(0x33), cols[1].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x33), cols[2].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x33), cols[3].LastAnalyzePos.GetBytes()[0])
if isConcurrency {
idx := table0.Indices
require.Equal(t, uint8(0x3), idx[1].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x3), idx[2].LastAnalyzePos.GetBytes()[0])
} else {
cols := table0.Columns
require.Equal(t, uint8(0x33), cols[1].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x33), cols[2].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x33), cols[3].LastAnalyzePos.GetBytes()[0])
idx := table0.Indices
require.Equal(t, uint8(0x3), idx[1].LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x3), idx[2].LastAnalyzePos.GetBytes()[0])
}
h.Clear()
require.NoError(t, h.InitStats(is))
table1 := h.GetTableStats(tbl.Meta())
Expand Down

0 comments on commit e954bcf

Please sign in to comment.