@@ -30,6 +30,9 @@ import (
30
30
func mergeGlobalStatsTopN (gp * gp.Pool , sc sessionctx.Context , wrapper * StatsWrapper ,
31
31
timeZone * time.Location , version int , n uint32 , isIndex bool ) (* statistics.TopN ,
32
32
[]statistics.TopNMeta , []* statistics.Histogram , error ) {
33
+ if statistics .CheckEmptyTopNs (wrapper .AllTopN ) {
34
+ return nil , nil , wrapper .AllHg , nil
35
+ }
33
36
mergeConcurrency := sc .GetSessionVars ().AnalyzePartitionMergeConcurrency
34
37
killed := & sc .GetSessionVars ().Killed
35
38
// use original method if concurrency equals 1 or for version1
@@ -69,12 +72,12 @@ func MergeGlobalStatsTopNByConcurrency(gp *gp.Pool, mergeConcurrency, mergeBatch
69
72
taskNum := len (tasks )
70
73
taskCh := make (chan * TopnStatsMergeTask , taskNum )
71
74
respCh := make (chan * TopnStatsMergeResponse , taskNum )
75
+ worker := NewTopnStatsMergeWorker (taskCh , respCh , wrapper , killed )
72
76
for i := 0 ; i < mergeConcurrency ; i ++ {
73
- worker := NewTopnStatsMergeWorker (taskCh , respCh , wrapper , killed )
74
77
wg .Add (1 )
75
78
gp .Go (func () {
76
79
defer wg .Done ()
77
- worker .Run (timeZone , isIndex , n , version )
80
+ worker .Run (timeZone , isIndex , version )
78
81
})
79
82
}
80
83
for _ , task := range tasks {
@@ -83,8 +86,6 @@ func MergeGlobalStatsTopNByConcurrency(gp *gp.Pool, mergeConcurrency, mergeBatch
83
86
close (taskCh )
84
87
wg .Wait ()
85
88
close (respCh )
86
- resps := make ([]* TopnStatsMergeResponse , 0 )
87
-
88
89
// handle Error
89
90
hasErr := false
90
91
errMsg := make ([]string , 0 )
@@ -93,27 +94,21 @@ func MergeGlobalStatsTopNByConcurrency(gp *gp.Pool, mergeConcurrency, mergeBatch
93
94
hasErr = true
94
95
errMsg = append (errMsg , resp .Err .Error ())
95
96
}
96
- resps = append (resps , resp )
97
97
}
98
98
if hasErr {
99
99
return nil , nil , nil , errors .New (strings .Join (errMsg , "," ))
100
100
}
101
101
102
102
// fetch the response from each worker and merge them into global topn stats
103
- sorted := make ([]statistics.TopNMeta , 0 , mergeConcurrency )
104
- leftTopn := make ([]statistics.TopNMeta , 0 )
105
- for _ , resp := range resps {
106
- if resp .TopN != nil {
107
- sorted = append (sorted , resp .TopN .TopN ... )
108
- }
109
- leftTopn = append (leftTopn , resp .PopedTopn ... )
103
+ counter := worker .Result ()
104
+ numTop := len (counter )
105
+ sorted := make ([]statistics.TopNMeta , 0 , numTop )
106
+ for value , cnt := range counter {
107
+ data := hack .Slice (string (value ))
108
+ sorted = append (sorted , statistics.TopNMeta {Encoded : data , Count : uint64 (cnt )})
110
109
}
111
-
112
110
globalTopN , popedTopn := statistics .GetMergedTopNFromSortedSlice (sorted , n )
113
-
114
- result := append (leftTopn , popedTopn ... )
115
- statistics .SortTopnMeta (result )
116
- return globalTopN , result , wrapper .AllHg , nil
111
+ return globalTopN , popedTopn , wrapper .AllHg , nil
117
112
}
118
113
119
114
// MergePartTopN2GlobalTopN is used to merge the partition-level topN to global-level topN.
@@ -124,13 +119,19 @@ func MergeGlobalStatsTopNByConcurrency(gp *gp.Pool, mergeConcurrency, mergeBatch
124
119
//
125
120
// The output parameters:
126
121
// 1. `*TopN` is the final global-level topN.
127
- // 2. `[]TopNMeta` is the left topN value from the partition-level TopNs, but is not placed to global-level TopN. We should put them back to histogram latter.
128
- // 3. `[]*Histogram` are the partition-level histograms which just delete some values when we merge the global-level topN.
129
- func MergePartTopN2GlobalTopN (loc * time.Location , version int , topNs []* statistics.TopN , n uint32 , hists []* statistics.Histogram ,
130
- isIndex bool , killed * uint32 ) (* statistics.TopN , []statistics.TopNMeta , []* statistics.Histogram , error ) {
131
- if statistics .CheckEmptyTopNs (topNs ) {
132
- return nil , nil , hists , nil
133
- }
122
+ // 2. `[]TopNMeta` is the left topN value from the partition-level TopNs,
123
+ // but is not placed to global-level TopN. We should put them back to histogram latter.
124
+ // 3. `[]*Histogram` are the partition-level histograms which
125
+ // just delete some values when we merge the global-level topN.
126
+ func MergePartTopN2GlobalTopN (
127
+ loc * time.Location ,
128
+ version int ,
129
+ topNs []* statistics.TopN ,
130
+ n uint32 ,
131
+ hists []* statistics.Histogram ,
132
+ isIndex bool ,
133
+ killed * uint32 ,
134
+ ) (* statistics.TopN , []statistics.TopNMeta , []* statistics.Histogram , error ) {
134
135
partNum := len (topNs )
135
136
// Different TopN structures may hold the same value, we have to merge them.
136
137
counter := make (map [hack.MutableString ]float64 )
0 commit comments