Skip to content

Commit

Permalink
add statsInCache stats analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
qingxinhome committed Dec 18, 2024
1 parent 9b1b161 commit 5217ca3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/frontend/compiler_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,11 +976,15 @@ func (tcc *TxnCompilerContext) UpdateStatsInCache(tid uint64, s *pb.StatsInfo) {
// statsInCache get the *pb.StatsInfo from session cache. If the info is nil, just return nil and false,
// else, check if the info needs to be updated.
func (tcc *TxnCompilerContext) statsInCache(ctx context.Context, dbName string, table engine.Relation, snapshot *plan2.Snapshot) (*pb.StatsInfo, bool) {
start1 := time.Now()
stats := statistic.StatsInfoFromContext(tcc.execCtx.reqCtx)
s := tcc.GetStatsCache().GetStatsInfo(table.GetTableID(ctx), true)
if s == nil {
return nil, false
}
stats.AddLogic1InPhase3Duration(time.Since(start1))

start2 := time.Now()
var partitionInfo *plan2.PartitionByDef
engineDefs, err := table.TableDefs(ctx)
if err != nil {
Expand All @@ -998,6 +1002,7 @@ func (tcc *TxnCompilerContext) statsInCache(ctx context.Context, dbName string,
}
}
}
stats.AddLogic2InPhase3Duration(time.Since(start2))

second := time.Now().Unix()
var diff int64 = 3
Expand All @@ -1010,6 +1015,7 @@ func (tcc *TxnCompilerContext) statsInCache(ctx context.Context, dbName string,
}
s.TimeSecond = second

start3 := time.Now()
approxNumObjects := 0
if partitionInfo != nil {
for _, PartitionTableName := range partitionInfo.PartitionTableNames {
Expand All @@ -1022,6 +1028,7 @@ func (tcc *TxnCompilerContext) statsInCache(ctx context.Context, dbName string,
} else {
approxNumObjects = table.ApproxObjectsNum(ctx)
}
stats.AddLogic3InPhase3Duration(time.Since(start3))
if approxNumObjects == 0 {
return nil, false
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/compile/analyze_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ func explainResourceOverview(queryResult *util.RunResult, statsInfo *statistic.S
buffer.WriteString(fmt.Sprintf("\t\t- Calc StatsPhase4 Duration: %dns \n", statsInfo.PlanStage.StatsCalcPhase4Duration))
buffer.WriteString(fmt.Sprintf("\t\t- Calc StatsPhase5 Duration: %dns \n", statsInfo.PlanStage.StatsCalcPhase5Duration))
buffer.WriteString(fmt.Sprintf("\t\t- Calc StatsPhase6 Duration: %dns \n", statsInfo.PlanStage.StatsCalcPhase6Duration))
buffer.WriteString(fmt.Sprintf("\t\t- Logic1 In Phase3 Duration: %dns \n", statsInfo.PlanStage.Logic1InPhase3Duration))
buffer.WriteString(fmt.Sprintf("\t\t- Logic2 In Phase3 Duration: %dns \n", statsInfo.PlanStage.Logic2InPhase3Duration))
buffer.WriteString(fmt.Sprintf("\t\t- Logic3 In Phase3 Duration: %dns \n", statsInfo.PlanStage.Logic3InPhase3Duration))

//-------------------------------------------------------------------------------------------------------
buffer.WriteString("\tQuery Compile Stage:\n")
Expand Down
27 changes: 26 additions & 1 deletion pkg/util/trace/impl/motrace/statistic/stats_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,12 @@ type StatsInfo struct {
StatsCalcPhase1Duration int64 `json:"StatsCalcPhase1Duration"` // unit: ns
StatsCalcPhase2Duration int64 `json:"StatsCalcPhase2Duration"` // unit: ns
StatsCalcPhase3Duration int64 `json:"StatsCalcPhase3Duration"` // unit: ns
StatsCalcPhase4Duration int64 `json:"StatsCalcPhase4Duration"` // unit: ns

Logic1InPhase3Duration int64 `json:"Logic1InPhase3Duration"` // unit: ns
Logic2InPhase3Duration int64 `json:"Logic2InPhase3Duration"` // unit: ns
Logic3InPhase3Duration int64 `json:"Logic3InPhase3Duration"` // unit: ns

StatsCalcPhase4Duration int64 `json:"StatsCalcPhase4Duration"` // unit: ns
StatsCalcPhase5Duration int64 `json:"StatsCalcPhase5Duration"` // unit: ns
StatsCalcPhase6Duration int64 `json:"StatsCalcPhase6Duration"` // unit: ns
}
Expand Down Expand Up @@ -551,6 +555,27 @@ func (stats *StatsInfo) AddStatsCalcPhase3Duration(d time.Duration) {
atomic.AddInt64(&stats.PlanStage.StatsCalcPhase3Duration, int64(d))
}

func (stats *StatsInfo) AddLogic1InPhase3Duration(d time.Duration) {
if stats == nil {
return
}
atomic.AddInt64(&stats.PlanStage.Logic1InPhase3Duration, int64(d))
}

func (stats *StatsInfo) AddLogic2InPhase3Duration(d time.Duration) {
if stats == nil {
return
}
atomic.AddInt64(&stats.PlanStage.Logic2InPhase3Duration, int64(d))
}

func (stats *StatsInfo) AddLogic3InPhase3Duration(d time.Duration) {
if stats == nil {
return
}
atomic.AddInt64(&stats.PlanStage.Logic3InPhase3Duration, int64(d))
}

func (stats *StatsInfo) AddStatsCalcPhase4Duration(d time.Duration) {
if stats == nil {
return
Expand Down

0 comments on commit 5217ca3

Please sign in to comment.