From 01c0eb34e019c9167beee607d35692851b8371de Mon Sep 17 00:00:00 2001 From: Yang Keao Date: Sat, 11 May 2024 14:07:07 +0800 Subject: [PATCH] optimize performance of IndexUsage Signed-off-by: Yang Keao --- pkg/executor/executor.go | 9 ++++++- pkg/planner/core/stats.go | 11 +++++++- .../handle/usage/indexusage/collector.go | 26 ++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index d6484600f1cef..ea1fde9c6b85a 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -2036,7 +2036,14 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { sc.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(reuseObj) // also enable index usage collector - sc.IndexUsageCollector = ctx.NewStmtIndexUsageCollector() + if sc.IndexUsageCollector == nil { + sc.IndexUsageCollector = ctx.NewStmtIndexUsageCollector() + } else { + sc.IndexUsageCollector.Reset() + } + } else { + // turn off the index usage collector + sc.IndexUsageCollector = nil } sc.SetForcePlanCache(fixcontrol.GetBoolWithDefault(vars.OptimizerFixControl, fixcontrol.Fix49736, false)) diff --git a/pkg/planner/core/stats.go b/pkg/planner/core/stats.go index d8f944f80afa1..6ca6585d85103 100644 --- a/pkg/planner/core/stats.go +++ b/pkg/planner/core/stats.go @@ -1084,7 +1084,16 @@ func loadTableStats(ctx sessionctx.Context, tblInfo *model.TableInfo, pid int64) pctx := ctx.GetPlanCtx() tableStats := getStatsTable(pctx, tblInfo, pid) - name, _ := getTblInfoForUsedStatsByPhysicalID(pctx, pid) + + name := tblInfo.Name.O + partInfo := tblInfo.GetPartitionInfo() + if partInfo != nil { + for _, p := range partInfo.Definitions { + if p.ID == pid { + name += " " + p.Name.O + } + } + } usedStats := &stmtctx.UsedStatsInfoForTable{ Name: name, TblInfo: tblInfo, diff --git a/pkg/statistics/handle/usage/indexusage/collector.go b/pkg/statistics/handle/usage/indexusage/collector.go index 02063a76cc1fe..3bb381f4db1a3 100644 --- a/pkg/statistics/handle/usage/indexusage/collector.go +++ b/pkg/statistics/handle/usage/indexusage/collector.go @@ -88,6 +88,12 @@ func NewSample(queryTotal uint64, kvReqTotal uint64, rowAccess uint64, tableTota type indexUsage map[GlobalIndexID]Sample +var indexUsagePool = sync.Pool{ + New: func() any { + return make(indexUsage) + }, +} + func (m indexUsage) updateByKey(id GlobalIndexID, sample Sample) { item := m[id] item.QueryTotal += sample.QueryTotal @@ -127,7 +133,7 @@ type Collector struct { // NewCollector create an index usage collector func NewCollector() *Collector { iuc := &Collector{ - indexUsage: make(indexUsage), + indexUsage: indexUsagePool.Get().(indexUsage), } iuc.collector = collector.NewGlobalCollector[indexUsage](iuc.merge) @@ -154,12 +160,16 @@ func (c *Collector) merge(delta indexUsage) { defer c.Unlock() c.indexUsage.merge(delta) + + // return the `delta` to the pool + clear(delta) + indexUsagePool.Put(delta) } // SpawnSessionCollector creates a new session collector attached to this global collector func (c *Collector) SpawnSessionCollector() *SessionIndexUsageCollector { return &SessionIndexUsageCollector{ - indexUsage: make(indexUsage), + indexUsage: indexUsagePool.Get().(indexUsage), collector: c.collector.SpawnSession(), } } @@ -219,7 +229,7 @@ func (s *SessionIndexUsageCollector) Report() { return } if s.collector.SendDelta(s.indexUsage) { - s.indexUsage = make(indexUsage) + s.indexUsage = indexUsagePool.Get().(indexUsage) } } @@ -230,7 +240,7 @@ func (s *SessionIndexUsageCollector) Flush() { return } s.collector.SendDeltaSync(s.indexUsage) - s.indexUsage = make(indexUsage) + s.indexUsage = indexUsagePool.Get().(indexUsage) } // StmtIndexUsageCollector removes the duplicates index for recording `QueryTotal` in session collector @@ -269,3 +279,11 @@ func (s *StmtIndexUsageCollector) Update(tableID int64, indexID int64, sample Sa s.sessionCollector.Update(tableID, indexID, sample) } + +// Reset resets the recorded index in the collector to avoid re-allocating for each statement. +func (s *StmtIndexUsageCollector) Reset() { + s.Lock() + defer s.Unlock() + + clear(s.recordedIndex) +}