statistics: avoid copy twice when to decode the topn (#47563)

ref pingcap/tidb#47275
This commit is contained in:
Weizhen Wang
2023-10-12 20:01:57 +08:00
committed by GitHub
parent 9353c68c7a
commit 6842efdb98
2 changed files with 6 additions and 8 deletions

View File

@ -463,17 +463,15 @@ func DecodeCMSketchAndTopN(data []byte, topNRows []chunk.Row) (*CMSketch, *TopN,
}
// DecodeTopN decodes a TopN from the given byte slice.
func DecodeTopN(topNRows []chunk.Row) (*TopN, error) {
pbTopN := make([]*tipb.CMSketchTopN, 0, len(topNRows))
func DecodeTopN(topNRows []chunk.Row) *TopN {
topN := NewTopN(len(topNRows))
for _, row := range topNRows {
data := make([]byte, len(row.GetBytes(0)))
copy(data, row.GetBytes(0))
pbTopN = append(pbTopN, &tipb.CMSketchTopN{
Data: data,
Count: row.GetUint64(1),
})
topN.AppendTopN(data, row.GetUint64(1))
}
return TopNFromProto(pbTopN), nil
topN.Sort()
return topN
}
// DecodeCMSketch encodes the given CMSketch to byte slice.

View File

@ -134,7 +134,7 @@ func TopNFromStorage(sctx sessionctx.Context, tblID int64, isIndex int, histID i
if err != nil || len(rows) == 0 {
return nil, err
}
return statistics.DecodeTopN(rows)
return statistics.DecodeTopN(rows), nil
}
// FMSketchFromStorage reads FMSketch from storage