Skip to content

Commit 2214bd0

Browse files
authored
statistics: Remove the ineffective dirty IDs from the row count cache (#56287)
close #55803
1 parent ec18512 commit 2214bd0

File tree

10 files changed

+13
-70
lines changed

10 files changed

+13
-70
lines changed

pkg/executor/infoschema_reader.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,14 @@ func (e *memtableRetriever) setDataFromOneTable(
682682

683683
var rowCount, avgRowLength, dataLength, indexLength uint64
684684
if useStatsCache {
685-
if table.GetPartitionInfo() == nil {
686-
err := cache.TableRowStatsCache.UpdateByID(sctx, table.ID)
687-
if err != nil {
688-
return rows, err
689-
}
690-
} else {
685+
// Even for partitioned tables, we must update the stats cache for the main table itself.
686+
// This is necessary because the global index length from the table also needs to be included.
687+
// For further details, see: https://github.com/pingcap/tidb/issues/54173
688+
err := cache.TableRowStatsCache.UpdateByID(sctx, table.ID)
689+
if err != nil {
690+
return rows, err
691+
}
692+
if table.GetPartitionInfo() != nil {
691693
// needs to update all partitions for partition table.
692694
for _, pi := range table.GetPartitionInfo().Definitions {
693695
err := cache.TableRowStatsCache.UpdateByID(sctx, pi.ID)

pkg/statistics/handle/cache/stats_table_row_cache.go

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,9 @@ type tableHistID struct {
4545

4646
// StatsTableRowCache is used to cache the count of table rows.
4747
type StatsTableRowCache struct {
48-
modifyTime time.Time
49-
tableRows map[int64]uint64
50-
colLength map[tableHistID]uint64
51-
dirtyIDs []int64
52-
mu syncutil.RWMutex
53-
}
54-
55-
// Invalidate invalidates the cache of the table with id.
56-
func (c *StatsTableRowCache) Invalidate(tblID int64) {
57-
c.mu.Lock()
58-
defer c.mu.Unlock()
59-
// To prevent the cache from becoming too large,
60-
// we only record the latest 100 dirty tables that have been modified.
61-
if len(c.dirtyIDs) < 100 {
62-
c.dirtyIDs = append(c.dirtyIDs, tblID)
63-
}
48+
tableRows map[int64]uint64
49+
colLength map[tableHistID]uint64
50+
mu syncutil.RWMutex
6451
}
6552

6653
// GetTableRows gets the count of table rows.
@@ -77,34 +64,8 @@ func (c *StatsTableRowCache) GetColLength(id tableHistID) uint64 {
7764
return c.colLength[id]
7865
}
7966

80-
func (c *StatsTableRowCache) updateDirtyIDs(sctx sessionctx.Context) error {
81-
if len(c.dirtyIDs) > 0 {
82-
tableRows, err := getRowCountTables(sctx, c.dirtyIDs...)
83-
if err != nil {
84-
return err
85-
}
86-
for id, tr := range tableRows {
87-
c.tableRows[id] = tr
88-
}
89-
colLength, err := getColLengthTables(sctx, c.dirtyIDs...)
90-
if err != nil {
91-
return err
92-
}
93-
for id, cl := range colLength {
94-
c.colLength[id] = cl
95-
}
96-
c.dirtyIDs = c.dirtyIDs[:0]
97-
}
98-
return nil
99-
}
100-
10167
// UpdateByID tries to update the cache by table ID.
10268
func (c *StatsTableRowCache) UpdateByID(sctx sessionctx.Context, id int64) error {
103-
c.mu.Lock()
104-
defer c.mu.Unlock()
105-
if time.Since(c.modifyTime) < tableStatsCacheExpiry {
106-
return c.updateDirtyIDs(sctx)
107-
}
10869
tableRows, err := getRowCountTables(sctx, id)
10970
if err != nil {
11071
return err
@@ -113,11 +74,12 @@ func (c *StatsTableRowCache) UpdateByID(sctx sessionctx.Context, id int64) error
11374
if err != nil {
11475
return err
11576
}
77+
c.mu.Lock()
78+
defer c.mu.Unlock()
11679
c.tableRows[id] = tableRows[id]
11780
for k, v := range colLength {
11881
c.colLength[k] = v
11982
}
120-
c.modifyTime = time.Now()
12183
return nil
12284
}
12385

pkg/statistics/handle/history/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ go_library(
88
deps = [
99
"//pkg/meta/model",
1010
"//pkg/sessionctx",
11-
"//pkg/statistics/handle/cache",
1211
"//pkg/statistics/handle/logutil",
1312
"//pkg/statistics/handle/storage",
1413
"//pkg/statistics/handle/types",

pkg/statistics/handle/history/history_stats.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/pingcap/errors"
2222
"github.com/pingcap/tidb/pkg/meta/model"
2323
"github.com/pingcap/tidb/pkg/sessionctx"
24-
"github.com/pingcap/tidb/pkg/statistics/handle/cache"
2524
statslogutil "github.com/pingcap/tidb/pkg/statistics/handle/logutil"
2625
"github.com/pingcap/tidb/pkg/statistics/handle/storage"
2726
"github.com/pingcap/tidb/pkg/statistics/handle/types"
@@ -172,7 +171,6 @@ func RecordHistoricalStatsMeta(
172171
); err != nil {
173172
return errors.Trace(err)
174173
}
175-
cache.TableRowStatsCache.Invalidate(tableID)
176174

177175
return nil
178176
}

pkg/statistics/handle/lockstats/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ go_library(
1111
visibility = ["//visibility:public"],
1212
deps = [
1313
"//pkg/sessionctx",
14-
"//pkg/statistics/handle/cache",
1514
"//pkg/statistics/handle/logutil",
1615
"//pkg/statistics/handle/types",
1716
"//pkg/statistics/handle/util",

pkg/statistics/handle/lockstats/unlock_stats.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/pingcap/errors"
1919
"github.com/pingcap/failpoint"
2020
"github.com/pingcap/tidb/pkg/sessionctx"
21-
"github.com/pingcap/tidb/pkg/statistics/handle/cache"
2221
statslogutil "github.com/pingcap/tidb/pkg/statistics/handle/logutil"
2322
"github.com/pingcap/tidb/pkg/statistics/handle/types"
2423
"github.com/pingcap/tidb/pkg/statistics/handle/util"
@@ -169,7 +168,6 @@ func updateStatsAndUnlockTable(sctx sessionctx.Context, tid int64) error {
169168
if err := updateDelta(sctx, count, modifyCount, tid); err != nil {
170169
return err
171170
}
172-
cache.TableRowStatsCache.Invalidate(tid)
173171

174172
_, _, err = util.ExecRows(
175173
sctx,
@@ -188,11 +186,9 @@ func updateStatsAndUnlockPartition(sctx sessionctx.Context, partitionID int64, t
188186
if err := updateDelta(sctx, count, modifyCount, partitionID); err != nil {
189187
return err
190188
}
191-
cache.TableRowStatsCache.Invalidate(partitionID)
192189
if err := updateDelta(sctx, count, modifyCount, tid); err != nil {
193190
return err
194191
}
195-
cache.TableRowStatsCache.Invalidate(tid)
196192

197193
_, _, err = util.ExecRows(
198194
sctx,

pkg/statistics/handle/storage/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ go_library(
2525
"//pkg/sessionctx/variable",
2626
"//pkg/statistics",
2727
"//pkg/statistics/asyncload",
28-
"//pkg/statistics/handle/cache",
2928
"//pkg/statistics/handle/lockstats",
3029
"//pkg/statistics/handle/logutil",
3130
"//pkg/statistics/handle/metrics",

pkg/statistics/handle/storage/gc.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/pingcap/tidb/pkg/sessionctx"
2929
"github.com/pingcap/tidb/pkg/sessionctx/vardef"
3030
"github.com/pingcap/tidb/pkg/statistics"
31-
"github.com/pingcap/tidb/pkg/statistics/handle/cache"
3231
"github.com/pingcap/tidb/pkg/statistics/handle/lockstats"
3332
"github.com/pingcap/tidb/pkg/statistics/handle/types"
3433
"github.com/pingcap/tidb/pkg/statistics/handle/util"
@@ -308,7 +307,6 @@ func gcTableStats(sctx sessionctx.Context,
308307
if err != nil {
309308
return errors.Trace(err)
310309
}
311-
cache.TableRowStatsCache.Invalidate(physicalID)
312310
return nil
313311
}
314312

pkg/statistics/handle/storage/save.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/pingcap/tidb/pkg/parser/terror"
2828
"github.com/pingcap/tidb/pkg/sessionctx"
2929
"github.com/pingcap/tidb/pkg/statistics"
30-
"github.com/pingcap/tidb/pkg/statistics/handle/cache"
3130
statslogutil "github.com/pingcap/tidb/pkg/statistics/handle/logutil"
3231
"github.com/pingcap/tidb/pkg/statistics/handle/util"
3332
"github.com/pingcap/tidb/pkg/types"
@@ -236,7 +235,6 @@ func SaveTableStatsToStorage(sctx sessionctx.Context,
236235
}
237236
statsVer = version
238237
}
239-
cache.TableRowStatsCache.Invalidate(tableID)
240238
// 2. Save histograms.
241239
for _, result := range results.Ars {
242240
for i, hg := range result.Hist {
@@ -338,7 +336,6 @@ func SaveStatsToStorage(
338336
// If the count is less than 0, then we do not want to update the modify count and count.
339337
if count >= 0 {
340338
_, err = util.Exec(sctx, "replace into mysql.stats_meta (version, table_id, count, modify_count) values (%?, %?, %?, %?)", version, tableID, count, modifyCount)
341-
cache.TableRowStatsCache.Invalidate(tableID)
342339
} else {
343340
_, err = util.Exec(sctx, "update mysql.stats_meta set version = %? where table_id = %?", version, tableID)
344341
}
@@ -389,7 +386,6 @@ func SaveMetaToStorage(
389386
}
390387
_, err = util.Exec(sctx, "replace into mysql.stats_meta (version, table_id, count, modify_count) values (%?, %?, %?, %?)", version, tableID, count, modifyCount)
391388
statsVer = version
392-
cache.TableRowStatsCache.Invalidate(tableID)
393389
return
394390
}
395391

pkg/statistics/handle/storage/update.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/pingcap/tidb/pkg/sessionctx"
2727
"github.com/pingcap/tidb/pkg/sessionctx/variable"
2828
"github.com/pingcap/tidb/pkg/statistics"
29-
"github.com/pingcap/tidb/pkg/statistics/handle/cache"
3029
"github.com/pingcap/tidb/pkg/statistics/handle/types"
3130
statsutil "github.com/pingcap/tidb/pkg/statistics/handle/util"
3231
)
@@ -159,11 +158,6 @@ func UpdateStatsMeta(
159158
}
160159
}
161160

162-
// Invalidate cache for all unlocked tables
163-
for _, id := range cacheInvalidateIDs {
164-
cache.TableRowStatsCache.Invalidate(id)
165-
}
166-
167161
return nil
168162
}
169163

0 commit comments

Comments
 (0)