Skip to content

Commit

Permalink
executor: skip empty partition table to merge global stats
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
  • Loading branch information
hawkingrei committed May 13, 2024
1 parent aa2bd18 commit 9483842
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/executor/test/analyzetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 48,
shard_count = 49,
deps = [
"//pkg/config",
"//pkg/domain",
Expand Down
21 changes: 12 additions & 9 deletions pkg/statistics/handle/globalstats/global_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,22 @@ partition by range (a) (
tk.MustExec("set @@tidb_analyze_version=2")
tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec("analyze table t")
checkModifyAndCount(0, 0, 0, 0, 0, 0)
checkHealthy(100, 100, 100)
rs := tk.MustQuery("show stats_meta").Rows()
require.Equal(t, "0", rs[0][4].(string)) // p0.modify_count
require.Equal(t, "0", rs[0][5].(string)) // p0.row_count
require.Equal(t, "0", rs[1][4].(string)) // p1.modify_count
require.Equal(t, "0", rs[1][5].(string)) // p1.row_count
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t p0 100", "test t p1 100"))

tk.MustExec("insert into t values (1), (2)") // update p0
require.NoError(t, dom.StatsHandle().DumpStatsDeltaToKV(true))
require.NoError(t, dom.StatsHandle().Update(dom.InfoSchema()))
checkModifyAndCount(2, 2, 2, 2, 0, 0)
checkHealthy(0, 0, 100)
tk.MustExec("analyze table t")
checkModifyAndCount(0, 2, 0, 2, 0, 0)
checkHealthy(100, 100, 100)

tk.MustExec("insert into t values (11), (12), (13), (14)") // update p1
require.NoError(t, dom.StatsHandle().DumpStatsDeltaToKV(true))
require.NoError(t, dom.StatsHandle().Update(dom.InfoSchema()))
checkModifyAndCount(6, 6, 2, 2, 4, 4)
checkModifyAndCount(6, 8, 2, 4, 4, 4)
checkHealthy(0, 0, 0)

tk.MustExec("analyze table t")
Expand Down Expand Up @@ -626,14 +629,14 @@ func TestGlobalStatsNDV(t *testing.T) {
checkNDV := func(ndvs ...int) { // g, p0, ..., p3
tk.MustExec("analyze table t")
rs := tk.MustQuery(`show stats_histograms where is_index=1`).Rows()
require.Len(t, rs, 5)
require.Len(t, rs, len(ndvs))
for i, ndv := range ndvs {
require.Equal(t, fmt.Sprintf("%v", ndv), rs[i][6].(string))
}
}

// all partitions are empty
checkNDV(0, 0, 0, 0, 0)
checkNDV(0, 0, 0, 0)

// p0 has data while others are empty
tk.MustExec("insert into t values (1), (2), (3)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func TestUnlockSomePartitionsWouldUpdateGlobalCountCorrectly(t *testing.T) {
tk.MustExec("insert into t(a, b) values(2,'b')")
tk.MustExec("analyze table test.t partition p0, p1")
tblStats := h.GetTableStats(tbl)
require.Equal(t, int64(0), tblStats.RealtimeCount)
require.Equal(t, int64(10000), tblStats.RealtimeCount)

// Dump stats delta to KV.
require.Nil(t, h.DumpStatsDeltaToKV(true))
Expand All @@ -499,11 +499,12 @@ func TestUnlockSomePartitionsWouldUpdateGlobalCountCorrectly(t *testing.T) {

// Unlock partition p0 and p1.
tk.MustExec("unlock stats t partition p0, p1")
tk.MustExec("analyze table test.t partition p0, p1")
// Check the global count is updated correctly.
rows = tk.MustQuery(fmt.Sprint("select count, modify_count, table_id from mysql.stats_meta where table_id = ", tbl.ID)).Rows()
require.Len(t, rows, 1)
require.Equal(t, "2", rows[0][0])
require.Equal(t, "2", rows[0][1])
require.Equal(t, "0", rows[0][1])
}

func setupTestEnvironmentWithPartitionedTableT(t *testing.T) (kv.Storage, *domain.Domain, *testkit.TestKit, *model.TableInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestUnlockPartitionedTableWouldUpdateGlobalCountCorrectly(t *testing.T) {
tk.MustExec("insert into t(a, b) values(2,'b')")
tk.MustExec("analyze table test.t")
tblStats := h.GetTableStats(tbl)
require.Equal(t, int64(0), tblStats.RealtimeCount)
require.Equal(t, int64(10000), tblStats.RealtimeCount)

// Dump stats delta to KV.
require.Nil(t, h.DumpStatsDeltaToKV(true))
Expand All @@ -297,11 +297,12 @@ func TestUnlockPartitionedTableWouldUpdateGlobalCountCorrectly(t *testing.T) {

// Unlock the table.
tk.MustExec("unlock stats t")
tk.MustExec("analyze table test.t")
// Check the global count is updated correctly.
rows = tk.MustQuery(fmt.Sprint("select count, modify_count from mysql.stats_meta where table_id = ", tbl.ID)).Rows()
require.Len(t, rows, 1)
require.Equal(t, "2", rows[0][0])
require.Equal(t, "2", rows[0][1])
require.Equal(t, "0", rows[0][1])
}

func TestDeltaInLockInfoCanBeNegative(t *testing.T) {
Expand Down

0 comments on commit 9483842

Please sign in to comment.