Skip to content

Commit a4d4582

Browse files
authored
stmtsummary: reduce the memory consumption by information_schema.statements_summary_evicted (#56722)
close #56629
1 parent 3ebfad0 commit a4d4582

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

pkg/infoschema/test/clustertablestest/tables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ func TestStmtSummaryEvictedPointGet(t *testing.T) {
11571157
tk.MustExec(fmt.Sprintf("select p from th%v where p=2333;", i%6))
11581158
}
11591159
tk.MustQuery("select EVICTED_COUNT from information_schema.statements_summary_evicted;").
1160-
Check(testkit.Rows("7"))
1160+
Check(testkit.Rows("996"))
11611161

11621162
tk.MustExec("set @@global.tidb_enable_stmt_summary=0;")
11631163
tk.MustQuery("select count(*) from information_schema.statements_summary_evicted;").

pkg/util/stmtsummary/evicted.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type stmtSummaryByDigestEvictedElement struct {
3838
beginTime int64
3939
// endTime is the end time of current interval
4040
endTime int64
41-
// digestKeyMap contains *Kinds* of digest being evicted
42-
digestKeyMap map[string]struct{}
41+
// count is the number of digest being evicted
42+
count int64
4343
// otherSummary contains summed up information of evicted elements
4444
otherSummary *stmtSummaryByDigestElement
4545
}
@@ -54,9 +54,8 @@ func newStmtSummaryByDigestEvicted() *stmtSummaryByDigestEvicted {
5454
// spawn a new pointer to stmtSummaryByDigestEvictedElement
5555
func newStmtSummaryByDigestEvictedElement(beginTime int64, endTime int64) *stmtSummaryByDigestEvictedElement {
5656
return &stmtSummaryByDigestEvictedElement{
57-
beginTime: beginTime,
58-
endTime: endTime,
59-
digestKeyMap: make(map[string]struct{}),
57+
beginTime: beginTime,
58+
endTime: endTime,
6059
otherSummary: &stmtSummaryByDigestElement{
6160
beginTime: beginTime,
6261
endTime: endTime,
@@ -153,7 +152,7 @@ func (ssbde *stmtSummaryByDigestEvicted) Clear() {
153152
// add an evicted record to stmtSummaryByDigestEvictedElement
154153
func (seElement *stmtSummaryByDigestEvictedElement) addEvicted(digestKey *stmtSummaryByDigestKey, digestValue *stmtSummaryByDigestElement) {
155154
if digestKey != nil {
156-
seElement.digestKeyMap[string(digestKey.Hash())] = struct{}{}
155+
seElement.count++
157156
addInfo(seElement.otherSummary, digestValue)
158157
}
159158
}
@@ -199,7 +198,7 @@ func (seElement *stmtSummaryByDigestEvictedElement) toEvictedCountDatum() []type
199198
datum := types.MakeDatums(
200199
types.NewTime(types.FromGoTime(time.Unix(seElement.beginTime, 0)), mysql.TypeTimestamp, 0),
201200
types.NewTime(types.FromGoTime(time.Unix(seElement.endTime, 0)), mysql.TypeTimestamp, 0),
202-
int64(len(seElement.digestKeyMap)),
201+
seElement.count,
203202
)
204203
return datum
205204
}

pkg/util/stmtsummary/evicted_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,19 @@ func TestSimpleStmtSummaryByDigestEvicted(t *testing.T) {
166166
require.Equal(t, "{begin: 1, end: 2, count: 1}", getAllEvicted(ssbde))
167167
// test insert same *kind* of digest
168168
ssbde.AddEvicted(evictedKey, evictedValue, 1)
169-
require.Equal(t, "{begin: 1, end: 2, count: 1}", getAllEvicted(ssbde))
169+
require.Equal(t, "{begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
170170

171171
evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("b", 1, 2)
172172
ssbde.AddEvicted(evictedKey, evictedValue, 1)
173-
require.Equal(t, "{begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
173+
require.Equal(t, "{begin: 1, end: 2, count: 3}", getAllEvicted(ssbde))
174174

175175
evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("b", 5, 6)
176176
ssbde.AddEvicted(evictedKey, evictedValue, 2)
177-
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
177+
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 1, end: 2, count: 3}", getAllEvicted(ssbde))
178178

179179
evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("b", 3, 4)
180180
ssbde.AddEvicted(evictedKey, evictedValue, 3)
181-
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 3, end: 4, count: 1}, {begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
181+
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 3, end: 4, count: 1}, {begin: 1, end: 2, count: 3}", getAllEvicted(ssbde))
182182

183183
// test evicted element with multi-time range value.
184184
ssbde = newStmtSummaryByDigestEvicted()
@@ -235,13 +235,13 @@ func TestStmtSummaryByDigestEvictedElement(t *testing.T) {
235235

236236
// test add same *kind* of values.
237237
record.addEvicted(evictedKey, digestValue)
238-
require.Equal(t, "{begin: 0, end: 1, count: 1}", getEvicted(record))
238+
require.Equal(t, "{begin: 0, end: 1, count: 2}", getEvicted(record))
239239

240240
// test add different *kind* of values.
241241
evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("bravo", 0, 1)
242242
digestValue = evictedValue.history.Back().Value.(*stmtSummaryByDigestElement)
243243
record.addEvicted(evictedKey, digestValue)
244-
require.Equal(t, "{begin: 0, end: 1, count: 2}", getEvicted(record))
244+
require.Equal(t, "{begin: 0, end: 1, count: 3}", getEvicted(record))
245245
}
246246

247247
// test stmtSummaryByDigestEvicted.addEvicted
@@ -315,7 +315,7 @@ func TestNewStmtSummaryByDigestEvictedElement(t *testing.T) {
315315
stmtEvictedElement := newStmtSummaryByDigestEvictedElement(now, end)
316316
require.Equal(t, now, stmtEvictedElement.beginTime)
317317
require.Equal(t, end, stmtEvictedElement.endTime)
318-
require.Equal(t, 0, len(stmtEvictedElement.digestKeyMap))
318+
require.Equal(t, int64(0), stmtEvictedElement.count)
319319
}
320320

321321
func TestStmtSummaryByDigestEvicted(t *testing.T) {
@@ -624,13 +624,13 @@ func getAllEvicted(ssdbe *stmtSummaryByDigestEvicted) string {
624624
buf.WriteString(", ")
625625
}
626626
val := e.Value.(*stmtSummaryByDigestEvictedElement)
627-
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", val.beginTime, val.endTime, len(val.digestKeyMap)))
627+
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", val.beginTime, val.endTime, val.count))
628628
}
629629
return buf.String()
630630
}
631631

632632
func getEvicted(ssbdee *stmtSummaryByDigestEvictedElement) string {
633633
buf := bytes.NewBuffer(nil)
634-
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", ssbdee.beginTime, ssbdee.endTime, len(ssbdee.digestKeyMap)))
634+
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", ssbdee.beginTime, ssbdee.endTime, ssbdee.count))
635635
return buf.String()
636636
}

0 commit comments

Comments
 (0)