Skip to content

Commit

Permalink
statistics: add more tests to check accurateness of global-stats (#22948
Browse files Browse the repository at this point in the history
)
  • Loading branch information
qw4990 authored Mar 1, 2021
1 parent febac51 commit b1b7730
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions statistics/fmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func DecodeFMSketch(data []byte) (*FMSketch, error) {
return nil, errors.Trace(err)
}
fm := FMSketchFromProto(p)
fm.maxSize = 10000 // TODO: add this attribute to PB and persist it instead of using a fixed number(executor.maxSketchSize)
return fm, nil
}

Expand Down
10 changes: 9 additions & 1 deletion statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,15 @@ func (h *Handle) TableStatsFromStorage(tableInfo *model.TableInfo, physicalID in
table = table.Copy()
}
table.Pseudo = false
rows, _, err := reader.read("select table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, flag, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %?", physicalID)

rows, _, err := reader.read("select modify_count, count from mysql.stats_meta where table_id = %?", physicalID)
if err != nil || len(rows) == 0 {
return nil, err
}
table.ModifyCount = rows[0].GetInt64(0)
table.Count = rows[0].GetInt64(1)

rows, _, err = reader.read("select table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, flag, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %?", physicalID)
// Check deleted table.
if err != nil || len(rows) == 0 {
return nil, nil
Expand Down
36 changes: 36 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,42 @@ func (s *testStatsSuite) TestBuildGlobalLevelStats(c *C) {
c.Assert(len(result.Rows()), Equals, 20)
}

func (s *testStatsSuite) TestGlobalStatsData(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec(`
create table t (
a int,
key(a)
)
partition by range (a) (
partition p0 values less than (10),
partition p1 values less than (20)
)`)
tk.MustExec("set @@tidb_partition_prune_mode='dynamic-only'")
tk.MustExec("insert into t values (1), (5), (null), (11), (15)")
c.Assert(s.do.StatsHandle().DumpStatsDeltaToKV(handle.DumpAll), IsNil)
tk.MustExec("analyze table t")

tk.MustQuery("select modify_count, count from mysql.stats_meta order by table_id asc").Check(
testkit.Rows("0 5", "0 3", "0 2")) // global row-count = sum(partition row-count)

// distinct, null_count, tot_col_size should be the sum of their values in partition-stats, and correlation should be 0
tk.MustQuery("select distinct_count, null_count, tot_col_size, correlation from mysql.stats_histograms where is_index=0 order by table_id asc").Check(
testkit.Rows("4 1 4 0", "2 1 2 1", "2 0 2 1"))

tk.MustQuery("show stats_buckets where is_index=0").Check(
testkit.Rows("test t global a 0 0 2 1 1 5 0", "test t global a 0 1 4 1 5 15 0",
"test t p0 a 0 0 1 1 1 1 0", "test t p0 a 0 1 2 1 5 5 0",
"test t p1 a 0 0 1 1 11 11 0", "test t p1 a 0 1 2 1 15 15 0"))
tk.MustQuery("show stats_buckets where is_index=1").Check(
testkit.Rows("test t global a 1 0 2 1 1 5 0", "test t global a 1 1 4 1 5 15 0",
"test t p0 a 1 0 1 1 1 1 0", "test t p0 a 1 1 2 1 5 5 0",
"test t p1 a 1 0 1 1 11 11 0", "test t p1 a 1 1 2 1 15 15 0"))
}

func (s *testStatsSuite) TestExtendedStatsDefaultSwitch(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
Expand Down

0 comments on commit b1b7730

Please sign in to comment.