Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: fix wrong NDV in the global stats when to disable async-merge-global-stats (#53762) #53803

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,11 @@ func (h *Handle) mergePartitionStats2GlobalStats(sc sessionctx.Context,
// Update NDV of global-level stats
globalStats.Fms[i] = allFms[i][0].Copy()
for j := 1; j < partitionNum; j++ {
globalStats.Fms[i].MergeFMSketch(allFms[i][j])
if globalStats.Fms[i] == nil {
globalStats.Fms[i] = allFms[i][j].Copy()
} else {
globalStats.Fms[i].MergeFMSketch(allFms[i][j])
}
}

// update the NDV
Expand Down
35 changes: 35 additions & 0 deletions tests/realtikvtest/statisticstest/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,38 @@ func TestNewCollationStatsWithPrefixIndex(t *testing.T) {
"1 3 15 0 2 0",
))
}

func TestBlockMergeFMSketch(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
checkFMSketch(tk)
}

func checkFMSketch(tk *testkit.TestKit) {
tk.MustExec(`CREATE TABLE employees (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,fname VARCHAR(25) NOT NULL,lname VARCHAR(25) NOT NULL,store_id INT NOT NULL,department_id INT NOT NULL
) PARTITION BY RANGE(id) (
PARTITION p0 VALUES LESS THAN (5),
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (15),
PARTITION p3 VALUES LESS THAN MAXVALUE
);`)
tk.MustExec(`INSERT INTO employees(FNAME,LNAME,STORE_ID,DEPARTMENT_ID) VALUES
('Bob', 'Taylor', 3, 2), ('Frank', 'Williams', 1, 2),
('Ellen', 'Johnson', 3, 4), ('Jim', 'Smith', 2, 4),
('Mary', 'Jones', 1, 1), ('Linda', 'Black', 2, 3),
('Ed', 'Jones', 2, 1), ('June', 'Wilson', 3, 1),
('Andy', 'Smith', 1, 3), ('Lou', 'Waters', 2, 4),
('Jill', 'Stone', 1, 4), ('Roger', 'White', 3, 2),
('Howard', 'Andrews', 1, 2), ('Fred', 'Goldberg', 3, 3),
('Barbara', 'Brown', 2, 3), ('Alice', 'Rogers', 2, 2),
('Mark', 'Morgan', 3, 3), ('Karen', 'Cole', 3, 2);`)
tk.MustExec("ANALYZE TABLE employees;")
tk.MustExec("select * from employees;")
tk.MustExec("alter table employees truncate partition p0;")
tk.MustExec("select * from employees;")
tk.MustExec("analyze table employees partition p3;")
tk.MustExec("select * from employees;")
tk.MustQuery(`SHOW STATS_HISTOGRAMS WHERE TABLE_NAME='employees' and partition_name="global" and column_name="id"`).CheckAt([]int{6}, [][]any{
{"14"}})
}
Loading