diff --git a/statistics/fmsketch.go b/statistics/fmsketch.go index 9de2c1bd4dbdf..f640500bae5bf 100644 --- a/statistics/fmsketch.go +++ b/statistics/fmsketch.go @@ -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 } diff --git a/statistics/handle/handle.go b/statistics/handle/handle.go index 28c3400e6efa7..8949e147b8745 100644 --- a/statistics/handle/handle.go +++ b/statistics/handle/handle.go @@ -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 diff --git a/statistics/handle/handle_test.go b/statistics/handle/handle_test.go index 6be91341afb3b..5a8064aaeb32c 100644 --- a/statistics/handle/handle_test.go +++ b/statistics/handle/handle_test.go @@ -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)