Skip to content

Commit 043986d

Browse files
authored
statistics: wrong NDV when to merge global stats with index (#57269) (#57318)
close #57264
1 parent 3f20732 commit 043986d

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

statistics/handle/handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table
12891289
if histID != idxInfo.ID {
12901290
continue
12911291
}
1292-
if idx == nil || idx.LastUpdateVersion < histVer {
1292+
if idx == nil || idx.LastUpdateVersion < histVer || loadAll {
12931293
hg, err := h.histogramFromStorage(reader, table.PhysicalID, histID, types.NewFieldType(mysql.TypeBlob), distinct, 1, histVer, nullCount, 0, 0)
12941294
if err != nil {
12951295
return errors.Trace(err)

statistics/handle/handle_hist_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package handle_test
1616

1717
import (
18+
"fmt"
1819
"testing"
1920
"time"
2021

@@ -331,3 +332,66 @@ func TestRetry(t *testing.T) {
331332
}
332333
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/statistics/handle/mockReadStatsForOneFail"))
333334
}
335+
336+
func TestMergeGlobalStatsIndex(t *testing.T) {
337+
store := testkit.CreateMockStore(t)
338+
tk := testkit.NewTestKit(t, store)
339+
tk.MustExec("use test")
340+
tk.MustExec(`CREATE TABLE employees3 (
341+
emp_id int(11) NOT NULL,
342+
emp_name varchar(25) NOT NULL,
343+
salary int(11) NOT NULL,
344+
dept_id int(11) NOT NULL,
345+
PRIMARY KEY (emp_id) /*T![clustered_index] NONCLUSTERED */
346+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
347+
PARTITION BY RANGE (emp_id)
348+
(
349+
PARTITION p0 VALUES LESS THAN (1000),
350+
PARTITION p1 VALUES LESS THAN (4000),
351+
PARTITION p2 VALUES LESS THAN (12000),
352+
PARTITION p3 VALUES LESS THAN (16000),
353+
PARTITION p4 VALUES LESS THAN (20000),
354+
PARTITION p5 VALUES LESS THAN (25000),
355+
PARTITION p6 VALUES LESS THAN (30000),
356+
PARTITION p7 VALUES LESS THAN (35000),
357+
PARTITION p8 VALUES LESS THAN (40000),
358+
PARTITION p9 VALUES LESS THAN (45000),
359+
PARTITION p10 VALUES LESS THAN (50000),
360+
PARTITION p11 VALUES LESS THAN (55000),
361+
PARTITION p12 VALUES LESS THAN (65000),
362+
PARTITION p13 VALUES LESS THAN (75000),
363+
PARTITION p14 VALUES LESS THAN (85000),
364+
PARTITION p15 VALUES LESS THAN (95000),
365+
PARTITION p16 VALUES LESS THAN (105000),
366+
PARTITION p17 VALUES LESS THAN (115000),
367+
PARTITION p18 VALUES LESS THAN (125000),
368+
PARTITION pmax VALUES LESS THAN (MAXVALUE)
369+
);`)
370+
tk.MustExec(`
371+
SET cte_max_recursion_depth = 1000000000;
372+
INSERT INTO employees3
373+
WITH RECURSIVE EmployeeGenerator AS (
374+
SELECT
375+
101 AS emp_id,
376+
'Emp00001' AS emp_name,
377+
FLOOR(RAND() * (150000 - 50000) + 50000) AS salary,
378+
FLOOR(RAND() * 3 + 1) AS dept_id
379+
UNION ALL
380+
SELECT
381+
emp_id + 1,
382+
CONCAT('Emp', LPAD(CAST(emp_id - 100 AS CHAR), 5, '0')),
383+
FLOOR(RAND() * (150000 - 50000) + 50000),
384+
FLOOR(RAND() * 3 + 1)
385+
FROM
386+
EmployeeGenerator
387+
WHERE
388+
emp_id < 20100
389+
)
390+
SELECT * FROM EmployeeGenerator;
391+
`)
392+
tk.MustExec("analyze table employees3")
393+
for i := 0; i <= 12; i++ {
394+
tk.MustExec(fmt.Sprintf("analyze table employees3 partition p%d", i))
395+
tk.MustQuery("show stats_histograms where table_name='employees3' and Column_name='PRIMARY' and Partition_name='global'").CheckContain("19958")
396+
}
397+
}

0 commit comments

Comments
 (0)